QrCodeActionTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\Core\Action;
  4. use Laminas\Diactoros\Response;
  5. use Laminas\Diactoros\ServerRequest;
  6. use Mezzio\Router\RouterInterface;
  7. use PHPUnit\Framework\TestCase;
  8. use Prophecy\Argument;
  9. use Prophecy\Prophecy\ObjectProphecy;
  10. use Psr\Http\Server\RequestHandlerInterface;
  11. use Shlinkio\Shlink\Common\Response\QrCodeResponse;
  12. use Shlinkio\Shlink\Core\Action\QrCodeAction;
  13. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  14. use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
  15. use Shlinkio\Shlink\Core\Service\UrlShortener;
  16. class QrCodeActionTest extends TestCase
  17. {
  18. private QrCodeAction $action;
  19. private ObjectProphecy $urlShortener;
  20. public function setUp(): void
  21. {
  22. $router = $this->prophesize(RouterInterface::class);
  23. $router->generateUri(Argument::cetera())->willReturn('/foo/bar');
  24. $this->urlShortener = $this->prophesize(UrlShortener::class);
  25. $this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal());
  26. }
  27. /** @test */
  28. public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
  29. {
  30. $shortCode = 'abc123';
  31. $this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
  32. ->shouldBeCalledOnce();
  33. $delegate = $this->prophesize(RequestHandlerInterface::class);
  34. $process = $delegate->handle(Argument::any())->willReturn(new Response());
  35. $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
  36. $process->shouldHaveBeenCalledOnce();
  37. }
  38. /** @test */
  39. public function anInvalidShortCodeWillReturnNotFoundResponse(): void
  40. {
  41. $shortCode = 'abc123';
  42. $this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
  43. ->shouldBeCalledOnce();
  44. $delegate = $this->prophesize(RequestHandlerInterface::class);
  45. $process = $delegate->handle(Argument::any())->willReturn(new Response());
  46. $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
  47. $process->shouldHaveBeenCalledOnce();
  48. }
  49. /** @test */
  50. public function aCorrectRequestReturnsTheQrCodeResponse(): void
  51. {
  52. $shortCode = 'abc123';
  53. $this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn(new ShortUrl(''))
  54. ->shouldBeCalledOnce();
  55. $delegate = $this->prophesize(RequestHandlerInterface::class);
  56. $resp = $this->action->process(
  57. (new ServerRequest())->withAttribute('shortCode', $shortCode),
  58. $delegate->reveal(),
  59. );
  60. $this->assertInstanceOf(QrCodeResponse::class, $resp);
  61. $this->assertEquals(200, $resp->getStatusCode());
  62. $delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
  63. }
  64. }