UrlValidatorTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\Core\Util;
  4. use Fig\Http\Message\RequestMethodInterface;
  5. use GuzzleHttp\ClientInterface;
  6. use GuzzleHttp\Exception\ClientException;
  7. use GuzzleHttp\RequestOptions;
  8. use Laminas\Diactoros\Response;
  9. use PHPUnit\Framework\TestCase;
  10. use Prophecy\Argument;
  11. use Prophecy\Prophecy\ObjectProphecy;
  12. use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
  13. use Shlinkio\Shlink\Core\Util\UrlValidator;
  14. class UrlValidatorTest extends TestCase
  15. {
  16. private UrlValidator $urlValidator;
  17. private ObjectProphecy $httpClient;
  18. public function setUp(): void
  19. {
  20. $this->httpClient = $this->prophesize(ClientInterface::class);
  21. $this->urlValidator = new UrlValidator($this->httpClient->reveal());
  22. }
  23. /** @test */
  24. public function exceptionIsThrownWhenUrlIsInvalid(): void
  25. {
  26. $request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
  27. $request->shouldBeCalledOnce();
  28. $this->expectException(InvalidUrlException::class);
  29. $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
  30. }
  31. /** @test */
  32. public function expectedUrlIsCalledWhenTryingToVerify(): void
  33. {
  34. $expectedUrl = 'http://foobar.com';
  35. $request = $this->httpClient->request(
  36. RequestMethodInterface::METHOD_GET,
  37. $expectedUrl,
  38. [RequestOptions::ALLOW_REDIRECTS => ['max' => 15]],
  39. )->willReturn(new Response());
  40. $this->urlValidator->validateUrl($expectedUrl);
  41. $request->shouldHaveBeenCalledOnce();
  42. }
  43. }