-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💊 Add Is Valid Url Constraint (#7)
- Loading branch information
Showing
13 changed files
with
199 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.