Skip to content

Commit

Permalink
💊 Add Is Valid Url Constraint (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerg4l committed Apr 27, 2019
1 parent 950467c commit c5aace5
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 57 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
39 changes: 0 additions & 39 deletions tests/Concerns/FollowsRedirects.php

This file was deleted.

54 changes: 54 additions & 0 deletions tests/Concerns/HasUrlAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace LaraCrafts\UrlShortener\Tests\Concerns;

use LaraCrafts\UrlShortener\Tests\Constraints\IsValidUrl;
use LaraCrafts\UrlShortener\Tests\Constraints\RedirectsTo;

trait HasUrlAssertions
{
/**
* Assert that the given value redirects to the expected value.
*
* @param \Psr\Http\Message\UriInterface|string $expected
* @param \Psr\Http\Message\UriInterface|string $actual
* @param int $redirects
* @return void
*/
public static function assertRedirectsTo($expected, $actual, int $redirects = 1)
{
static::assertThat($actual, static::redirectsTo($expected, $redirects));
}

/**
* Assert that the given value is valid URL.
*
* @param mixed $actual
*/
public static function assertValidUrl($actual)
{
static::assertThat($actual, static::isValidUrl());
}

/**
* Create a IsValidUrl instance
*
* @return IsValidUrl
*/
public static function isValidUrl(): IsValidUrl
{
return new IsValidUrl;
}

/**
* Create a RedirectsTo instance
*
* @param $expected
* @param int $redirects
* @return RedirectsTo
*/
public static function redirectsTo($expected, int $redirects = 1): RedirectsTo
{
return new RedirectsTo($expected, $redirects);
}
}
24 changes: 24 additions & 0 deletions tests/Constraints/IsValidUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LaraCrafts\UrlShortener\Tests\Constraints;

use PHPUnit\Framework\Constraint\Constraint;

class IsValidUrl extends Constraint
{
/**
* {@inheritDoc}
*/
protected function matches($other): bool
{
return filter_var((string)$other, FILTER_VALIDATE_URL);
}

/**
* {@inheritDoc}
*/
public function toString(): string
{
return 'is a valid URL';
}
}
69 changes: 69 additions & 0 deletions tests/Constraints/RedirectsTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace LaraCrafts\UrlShortener\Tests\Constraints;

use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use PHPUnit\Framework\Constraint\Constraint;

class RedirectsTo extends Constraint
{
protected $client;
protected $destination;
protected $redirects;

/**
* Create a new RedirectsTo constraint.
*
* @param \Psr\Http\Message\UriInterface|string $destination
* @param int $redirects
* @return void
*/
public function __construct($destination, int $redirects = 1)
{
$this->client = new Client();
$this->destination = rtrim($destination, '/');
$this->redirects = $redirects;
}

/**
* {@inheritDoc}
*/
public function evaluate($other, $description = '', $returnResult = false)
{
$stack = [];

$this->client->get($other, [
'allow_redirects' => [
'max' => max($this->redirects, 5),
],
'on_stats' => function (TransferStats $stats) use (&$stack) {
$stack[] = (string)$stats->getEffectiveUri();
},
]);

if (($actualRedirects = count($stack) - 1) < $this->redirects) {
$this->fail($other, "Expected $this->redirects redirects, received $actualRedirects");
}

if (!$this->matches($actual = $stack[$this->redirects])) {
$this->fail($actual, "Expected $this->destination, received $actual");
}
}

/**
* {@inheritDoc}
*/
public function matches($other): bool
{
return rtrim($other, '/') === $this->destination;
}

/**
* {@inheritDoc}
*/
public function toString(): string
{
return 'redirects to ' . $this->destination;
}
}
9 changes: 6 additions & 3 deletions tests/Integration/BitLyShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use LaraCrafts\UrlShortener\Http\BitLyShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\FollowsRedirects;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class BitLyShortenerTest extends TestCase
{
use FollowsRedirects;
use HasUrlAssertions;

/**
* @var \LaraCrafts\UrlShortener\Http\BitLyShortener
Expand Down Expand Up @@ -39,6 +39,8 @@ public function setUp(): void
public function testShorten()
{
$shortUrl = $this->shortener->shorten('https://google.com');

$this->assertValidUrl($shortUrl);
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}

Expand All @@ -52,6 +54,7 @@ public function testShortenAsync()
$promise = $this->shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertRedirectsTo('https://google.com', $promise->wait(), 1);
$this->assertValidUrl($shortUrl = $promise->wait());
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}
}
9 changes: 7 additions & 2 deletions tests/Integration/FirebaseShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use LaraCrafts\UrlShortener\Http\FirebaseShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class FirebaseShortenerTest extends TestCase
{
use HasUrlAssertions;

/**
* @var string
*/
Expand Down Expand Up @@ -63,7 +66,8 @@ public function testShorten(string $suffix)
$shortener = new FirebaseShortener(new Client, $this->token, $this->prefix, $suffix);
$shortUrl = $shortener->shorten('https://google.com');

$this->assertInternalType('string', $shortUrl);
$this->assertValidUrl($shortUrl);
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}

/**
Expand All @@ -79,6 +83,7 @@ public function testShortenAsync(string $suffix)
$promise = $shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertInternalType('string', $promise->wait());
$this->assertValidUrl($shortUrl = $promise->wait());
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}
}
10 changes: 7 additions & 3 deletions tests/Integration/IsGdShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use LaraCrafts\UrlShortener\Http\IsGdShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\FollowsRedirects;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class IsGdShortenerTest extends TestCase
{
use FollowsRedirects;
use HasUrlAssertions;

/**
* @var \LaraCrafts\UrlShortener\Http\IsGdShortener
Expand All @@ -34,6 +34,8 @@ public function setUp(): void
public function testShorten()
{
$shortUrl = $this->shortener->shorten('https://google.com');

$this->assertValidUrl($shortUrl);
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}

Expand All @@ -45,7 +47,9 @@ public function testShorten()
public function testShortenAsync()
{
$promise = $this->shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertRedirectsTo('https://google.com', $promise->wait(), 1);
$this->assertValidUrl($shortUrl = $promise->wait());
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}
}
8 changes: 6 additions & 2 deletions tests/Integration/OuoIoShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Str;
use LaraCrafts\UrlShortener\Http\OuoIoShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class OuoIoShortenerTest extends TestCase
{
use HasUrlAssertions;

/**
* @var \LaraCrafts\UrlShortener\Http\OuoIoShortener
*/
Expand Down Expand Up @@ -37,7 +40,8 @@ public function setUp(): void
public function testShorten()
{
$shortUrl = $this->shortener->shorten('https://google.com');
$this->assertInternalType('string', $shortUrl);

$this->assertValidUrl($shortUrl);
$this->assertTrue(Str::startsWith($shortUrl, 'https://ouo.io/'));
}

Expand All @@ -51,7 +55,7 @@ public function testShortenAsync()
$promise = $this->shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertInternalType('string', $shortUrl = $promise->wait());
$this->assertValidUrl($shortUrl = $promise->wait());
$this->assertTrue(Str::startsWith($shortUrl, 'https://ouo.io/'));
}
}
8 changes: 6 additions & 2 deletions tests/Integration/ShorteStShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use LaraCrafts\UrlShortener\Http\ShorteStShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class ShorteStShortenerTest extends TestCase
{
use HasUrlAssertions;

/**
* @var \LaraCrafts\UrlShortener\Http\ShorteStShortener
*/
Expand Down Expand Up @@ -36,7 +39,8 @@ public function setUp(): void
public function testShorten()
{
$shortUrl = $this->shortener->shorten('https://google.com');
$this->assertInternalType('string', $shortUrl);

$this->assertValidUrl($shortUrl);
}

/**
Expand All @@ -49,6 +53,6 @@ public function testShortenAsync()
$promise = $this->shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertInternalType('string', $promise->wait());
$this->assertValidUrl($shortUrl = $promise->wait());
}
}
10 changes: 7 additions & 3 deletions tests/Integration/TinyUrlShortenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use LaraCrafts\UrlShortener\Http\TinyUrlShortener;
use LaraCrafts\UrlShortener\Tests\Concerns\FollowsRedirects;
use LaraCrafts\UrlShortener\Tests\Concerns\HasUrlAssertions;
use Orchestra\Testbench\TestCase;

class TinyUrlShortenerTest extends TestCase
{
use FollowsRedirects;
use HasUrlAssertions;

/**
* @var \LaraCrafts\UrlShortener\Http\TinyUrlShortener
Expand All @@ -34,6 +34,8 @@ public function setUp(): void
public function testShorten()
{
$shortUrl = $this->shortener->shorten('https://google.com');

$this->assertValidUrl($shortUrl);
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}

Expand All @@ -45,7 +47,9 @@ public function testShorten()
public function testShortenAsync()
{
$promise = $this->shortener->shortenAsync('https://google.com');

$this->assertInstanceOf(PromiseInterface::class, $promise);
$this->assertRedirectsTo('https://google.com', $promise->wait(), 1);
$this->assertValidUrl($shortUrl = $promise->wait());
$this->assertRedirectsTo('https://google.com', $shortUrl, 1);
}
}
Loading

0 comments on commit c5aace5

Please sign in to comment.