From 4f1ab977a1cd6a6c91be1a1bede5a06efc6672b1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 27 Nov 2020 17:42:33 +0100 Subject: [PATCH] #917 Added tests covering the different ways to provide sizes to the QR codes --- .../src/Action/AbstractTrackingAction.php | 2 +- module/Core/src/Action/QrCodeAction.php | 2 +- module/Core/test/Action/QrCodeActionTest.php | 46 +++++++++++++------ 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/module/Core/src/Action/AbstractTrackingAction.php b/module/Core/src/Action/AbstractTrackingAction.php index b121ae3a..86eb197b 100644 --- a/module/Core/src/Action/AbstractTrackingAction.php +++ b/module/Core/src/Action/AbstractTrackingAction.php @@ -41,7 +41,7 @@ abstract class AbstractTrackingAction implements MiddlewareInterface, RequestMet $this->urlResolver = $urlResolver; $this->visitTracker = $visitTracker; $this->appOptions = $appOptions; - $this->logger = $logger ?: new NullLogger(); + $this->logger = $logger ?? new NullLogger(); } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface diff --git a/module/Core/src/Action/QrCodeAction.php b/module/Core/src/Action/QrCodeAction.php index 4cdecf97..919682d5 100644 --- a/module/Core/src/Action/QrCodeAction.php +++ b/module/Core/src/Action/QrCodeAction.php @@ -34,7 +34,7 @@ class QrCodeAction implements MiddlewareInterface ) { $this->urlResolver = $urlResolver; $this->domainConfig = $domainConfig; - $this->logger = $logger ?: new NullLogger(); + $this->logger = $logger ?? new NullLogger(); } public function process(Request $request, RequestHandlerInterface $handler): Response diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php index 1237585c..76daa406 100644 --- a/module/Core/test/Action/QrCodeActionTest.php +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -6,11 +6,13 @@ namespace ShlinkioTest\Shlink\Core\Action; use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequest; +use Laminas\Diactoros\ServerRequestFactory; use Mezzio\Router\RouterInterface; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Shlinkio\Shlink\Common\Response\QrCodeResponse; use Shlinkio\Shlink\Core\Action\QrCodeAction; @@ -19,6 +21,8 @@ use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface; +use function getimagesizefromstring; + class QrCodeActionTest extends TestCase { use ProphecyTrait; @@ -51,21 +55,6 @@ class QrCodeActionTest extends TestCase $process->shouldHaveBeenCalledOnce(); } - /** @test */ - public function anInvalidShortCodeWillReturnNotFoundResponse(): void - { - $shortCode = 'abc123'; - $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($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 { @@ -110,4 +99,31 @@ class QrCodeActionTest extends TestCase yield 'svg format' => [['format' => 'svg'], 'image/svg+xml']; yield 'unsupported format' => [['format' => 'jpg'], 'image/png']; } + + /** + * @test + * @dataProvider provideRequestsWithSize + */ + public function imageIsReturnedWithExpectedSize(ServerRequestInterface $req, int $expectedSize): void + { + $code = 'abc123'; + $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(new ShortUrl('')); + $delegate = $this->prophesize(RequestHandlerInterface::class); + + $resp = $this->action->process($req->withAttribute('shortCode', $code), $delegate->reveal()); + [$size] = getimagesizefromstring((string) $resp->getBody()); + + self::assertEquals($expectedSize, $size); + } + + public function provideRequestsWithSize(): iterable + { + yield 'no size' => [ServerRequestFactory::fromGlobals(), 300]; + yield 'size in attr' => [ServerRequestFactory::fromGlobals()->withAttribute('size', '400'), 400]; + yield 'size in query' => [ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']), 123]; + yield 'size in query and attr' => [ + ServerRequestFactory::fromGlobals()->withAttribute('size', '350')->withQueryParams(['size' => '123']), + 350, + ]; + } }