shlink/module/Core/test/Action/QrCodeActionTest.php

130 lines
4.8 KiB
PHP
Raw Normal View History

2016-08-09 07:18:20 -05:00
<?php
2019-10-05 10:26:10 -05:00
2017-10-12 03:13:20 -05:00
declare(strict_types=1);
2016-08-09 07:18:20 -05:00
namespace ShlinkioTest\Shlink\Core\Action;
2020-01-01 14:11:53 -06:00
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory;
2020-01-01 14:11:53 -06:00
use Mezzio\Router\RouterInterface;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
2016-08-09 07:18:20 -05:00
use Prophecy\Argument;
2020-11-02 04:50:19 -06:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-08-09 07:18:20 -05:00
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
2018-03-26 12:02:41 -05:00
use Psr\Http\Server\RequestHandlerInterface;
2016-08-09 07:18:20 -05:00
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\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
2016-08-09 07:18:20 -05:00
use function getimagesizefromstring;
2016-08-09 07:18:20 -05:00
class QrCodeActionTest extends TestCase
{
2020-11-02 04:50:19 -06:00
use ProphecyTrait;
2019-12-29 15:27:00 -06:00
private QrCodeAction $action;
private ObjectProphecy $urlResolver;
2016-08-09 07:18:20 -05:00
2019-02-16 03:53:45 -06:00
public function setUp(): void
2016-08-09 07:18:20 -05:00
{
$router = $this->prophesize(RouterInterface::class);
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
2016-08-09 07:18:20 -05:00
$this->action = new QrCodeAction($this->urlResolver->reveal(), ['domain' => 'doma.in']);
2016-08-09 07:18:20 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
2019-10-22 12:52:28 -05:00
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
2016-08-09 07:18:20 -05:00
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
->willThrow(ShortUrlNotFoundException::class)
->shouldBeCalledOnce();
2018-03-26 12:02:41 -05:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->handle(Argument::any())->willReturn(new Response());
2016-08-09 07:18:20 -05:00
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
2018-11-11 06:18:21 -06:00
$process->shouldHaveBeenCalledOnce();
2016-08-09 07:18:20 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
2019-10-22 12:52:28 -05:00
public function aCorrectRequestReturnsTheQrCodeResponse(): void
2016-08-09 07:18:20 -05:00
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
->willReturn(new ShortUrl(''))
->shouldBeCalledOnce();
2018-03-26 12:02:41 -05:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
2016-08-09 07:18:20 -05:00
$resp = $this->action->process(
(new ServerRequest())->withAttribute('shortCode', $shortCode),
2020-01-01 13:48:31 -06:00
$delegate->reveal(),
2016-08-09 07:18:20 -05:00
);
2020-10-03 17:35:14 -05:00
self::assertInstanceOf(QrCodeResponse::class, $resp);
self::assertEquals(200, $resp->getStatusCode());
2018-03-26 12:02:41 -05:00
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
2016-08-09 07:18:20 -05:00
}
2020-09-21 15:54:05 -05:00
/**
* @test
* @dataProvider provideQueries
*/
public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
array $query,
string $expectedContentType
): void {
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(new ShortUrl(''));
$delegate = $this->prophesize(RequestHandlerInterface::class);
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
$resp = $this->action->process($req, $delegate->reveal());
2020-10-03 17:35:14 -05:00
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
2020-09-21 15:54:05 -05:00
}
public function provideQueries(): iterable
{
yield 'no format' => [[], 'image/png'];
yield 'png format' => [['format' => 'png'], 'image/png'];
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,
];
}
2016-08-09 07:18:20 -05:00
}