Added tests for UrlValidator

This commit is contained in:
Alejandro Celaya 2019-11-16 10:19:25 +01:00
parent 264b8c2a9e
commit 19c1b29f59
3 changed files with 77 additions and 15 deletions

View File

@ -16,7 +16,7 @@ use function idn_to_ascii;
use const IDNA_DEFAULT;
use const INTL_IDNA_VARIANT_UTS46;
class UrlValidator implements UrlValidatorInterface
class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
{
private const MAX_REDIRECTS = 15;
@ -43,7 +43,7 @@ class UrlValidator implements UrlValidatorInterface
}
try {
$this->httpClient->request(RequestMethodInterface::METHOD_GET, (string) $uri, [
$this->httpClient->request(self::METHOD_GET, (string) $uri, [
RequestOptions::ALLOW_REDIRECTS => ['max' => self::MAX_REDIRECTS],
]);
} catch (GuzzleException $e) {

View File

@ -9,21 +9,18 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
use Zend\Diactoros\Uri;
use function array_map;
@ -35,11 +32,11 @@ class UrlShortenerTest extends TestCase
/** @var ObjectProphecy */
private $em;
/** @var ObjectProphecy */
private $httpClient;
private $urlValidator;
public function setUp(): void
{
$this->httpClient = $this->prophesize(ClientInterface::class);
$this->urlValidator = $this->prophesize(UrlValidatorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$conn = $this->prophesize(Connection::class);
@ -63,7 +60,7 @@ class UrlShortenerTest extends TestCase
private function setUrlShortener(bool $urlValidationEnabled): void
{
$this->urlShortener = new UrlShortener(
$this->httpClient->reveal(),
$this->urlValidator->reveal(),
$this->em->reveal(),
new UrlShortenerOptions(['validate_url' => $urlValidationEnabled])
);
@ -127,20 +124,19 @@ class UrlShortenerTest extends TestCase
}
/** @test */
public function exceptionIsThrownWhenUrlDoesNotExist(): void
public function validatorIsCalledWhenUrlValidationIsEnabled(): void
{
$this->setUrlShortener(true);
$validateUrl = $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar')->will(function () {
});
$this->httpClient->request(Argument::cetera())->willThrow(
new ClientException('', $this->prophesize(Request::class)->reveal())
);
$this->expectException(InvalidUrlException::class);
$this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createEmpty()
);
$validateUrl->shouldHaveBeenCalledOnce();
}
/** @test */

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Util;
use Fig\Http\Message\RequestMethodInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Util\UrlValidator;
use Zend\Diactoros\Request;
class UrlValidatorTest extends TestCase
{
/** @var UrlValidator */
private $urlValidator;
/** @var ObjectProphecy */
private $httpClient;
public function setUp(): void
{
$this->httpClient = $this->prophesize(ClientInterface::class);
$this->urlValidator = new UrlValidator($this->httpClient->reveal());
}
/** @test */
public function exceptionIsThrownWhenUrlIsInvalid(): void
{
$request = $this->httpClient->request(Argument::cetera())->willThrow(
new ClientException('', $this->prophesize(Request::class)->reveal())
);
$this->expectException(InvalidUrlException::class);
$request->shouldBeCalledOnce();
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
}
/**
* @test
* @dataProvider provideUrls
*/
public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void
{
$request = $this->httpClient->request(
RequestMethodInterface::METHOD_GET,
$expectedUrl,
Argument::cetera()
)->will(function () {
});
$this->urlValidator->validateUrl($providedUrl);
$request->shouldHaveBeenCalledOnce();
}
public function provideUrls(): iterable
{
yield 'regular domain' => ['http://foobar.com', 'http://foobar.com'];
yield 'IDN' => ['https://cédric.laubacher.io/', 'https://xn--cdric-bsa.laubacher.io/'];
}
}