1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- declare(strict_types=1);
- namespace ShlinkioTest\Shlink\Core\Action;
- use Laminas\Diactoros\Response;
- use Laminas\Diactoros\ServerRequest;
- use Mezzio\Router\RouterInterface;
- use PHPUnit\Framework\TestCase;
- use Prophecy\Argument;
- use Prophecy\Prophecy\ObjectProphecy;
- use Psr\Http\Server\RequestHandlerInterface;
- use Shlinkio\Shlink\Common\Response\QrCodeResponse;
- use Shlinkio\Shlink\Core\Action\QrCodeAction;
- use Shlinkio\Shlink\Core\Entity\ShortUrl;
- use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
- use Shlinkio\Shlink\Core\Service\UrlShortener;
- class QrCodeActionTest extends TestCase
- {
- private QrCodeAction $action;
- private ObjectProphecy $urlShortener;
- public function setUp(): void
- {
- $router = $this->prophesize(RouterInterface::class);
- $router->generateUri(Argument::cetera())->willReturn('/foo/bar');
- $this->urlShortener = $this->prophesize(UrlShortener::class);
- $this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal());
- }
- /** @test */
- public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
- {
- $shortCode = 'abc123';
- $this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
- ->shouldBeCalledOnce();
- $delegate = $this->prophesize(RequestHandlerInterface::class);
- $process = $delegate->handle(Argument::any())->willReturn(new Response());
- $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
- $process->shouldHaveBeenCalledOnce();
- }
- /** @test */
- public function anInvalidShortCodeWillReturnNotFoundResponse(): void
- {
- $shortCode = 'abc123';
- $this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
- ->shouldBeCalledOnce();
- $delegate = $this->prophesize(RequestHandlerInterface::class);
- $process = $delegate->handle(Argument::any())->willReturn(new Response());
- $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
- $process->shouldHaveBeenCalledOnce();
- }
- /** @test */
- public function aCorrectRequestReturnsTheQrCodeResponse(): void
- {
- $shortCode = 'abc123';
- $this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn(new ShortUrl(''))
- ->shouldBeCalledOnce();
- $delegate = $this->prophesize(RequestHandlerInterface::class);
- $resp = $this->action->process(
- (new ServerRequest())->withAttribute('shortCode', $shortCode),
- $delegate->reveal(),
- );
- $this->assertInstanceOf(QrCodeResponse::class, $resp);
- $this->assertEquals(200, $resp->getStatusCode());
- $delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
- }
- }
|