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;
|
2020-11-27 10:42:33 -06:00
|
|
|
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;
|
2020-11-27 10:42:33 -06:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-03-26 12:02:41 -05:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2021-07-13 06:45:46 -05:00
|
|
|
use Psr\Log\NullLogger;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
|
|
|
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
2018-08-11 03:40:44 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-24 16:11:25 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2020-02-01 04:46:54 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2021-09-26 06:25:02 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
|
2020-01-26 12:21:51 -06:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2020-11-27 10:42:33 -06:00
|
|
|
use function getimagesizefromstring;
|
2021-12-06 11:06:29 -06:00
|
|
|
use function imagecolorat;
|
|
|
|
use function imagecreatefromstring;
|
2020-11-27 10:42:33 -06:00
|
|
|
|
2016-08-09 07:18:20 -05:00
|
|
|
class QrCodeActionTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2021-12-06 11:06:29 -06:00
|
|
|
private const WHITE = 0xFFFFFF;
|
|
|
|
private const BLACK = 0x0;
|
|
|
|
|
2019-12-29 15:27:00 -06:00
|
|
|
private QrCodeAction $action;
|
2020-01-26 12:21:51 -06:00
|
|
|
private ObjectProphecy $urlResolver;
|
2021-09-26 06:25:02 -05:00
|
|
|
private QrCodeOptions $options;
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$router = $this->prophesize(RouterInterface::class);
|
|
|
|
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
|
|
|
|
|
2020-01-26 12:21:51 -06:00
|
|
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
2021-09-26 06:25:02 -05:00
|
|
|
$this->options = new QrCodeOptions();
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2021-02-01 15:55:52 -06:00
|
|
|
$this->action = new QrCodeAction(
|
|
|
|
$this->urlResolver->reveal(),
|
|
|
|
new ShortUrlStringifier(['domain' => 'doma.in']),
|
2021-07-13 06:45:46 -05:00
|
|
|
new NullLogger(),
|
2021-09-26 06:25:02 -05:00
|
|
|
$this->options,
|
2021-02-01 15:55:52 -06:00
|
|
|
);
|
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';
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
2020-02-01 04:46:54 -06:00
|
|
|
->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
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
2017-03-24 17:19:42 -05:00
|
|
|
|
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';
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
|
2021-01-30 07:18:44 -06:00
|
|
|
->willReturn(ShortUrl::createEmpty())
|
2020-02-01 04:46:54 -06:00
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2017-03-24 17:19:42 -05:00
|
|
|
$resp = $this->action->process(
|
2018-12-25 16:01:30 -06:00
|
|
|
(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(
|
2021-09-26 06:25:02 -05:00
|
|
|
string $defaultFormat,
|
2020-09-21 15:54:05 -05:00
|
|
|
array $query,
|
2021-05-23 05:31:10 -05:00
|
|
|
string $expectedContentType,
|
2020-09-21 15:54:05 -05:00
|
|
|
): void {
|
2021-09-26 06:25:02 -05:00
|
|
|
$this->options->setFromArray(['format' => $defaultFormat]);
|
2020-09-21 15:54:05 -05:00
|
|
|
$code = 'abc123';
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
2021-01-30 07:18:44 -06:00
|
|
|
ShortUrl::createEmpty(),
|
|
|
|
);
|
2020-09-21 15:54:05 -05:00
|
|
|
$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
|
|
|
|
{
|
2021-09-26 06:25:02 -05:00
|
|
|
yield 'no format, png default' => ['png', [], 'image/png'];
|
|
|
|
yield 'no format, svg default' => ['svg', [], 'image/svg+xml'];
|
|
|
|
yield 'png format, png default' => ['png', ['format' => 'png'], 'image/png'];
|
|
|
|
yield 'png format, svg default' => ['svg', ['format' => 'png'], 'image/png'];
|
|
|
|
yield 'svg format, png default' => ['png', ['format' => 'svg'], 'image/svg+xml'];
|
|
|
|
yield 'svg format, svg default' => ['svg', ['format' => 'svg'], 'image/svg+xml'];
|
|
|
|
yield 'unsupported format, png default' => ['png', ['format' => 'jpg'], 'image/png'];
|
|
|
|
yield 'unsupported format, svg default' => ['svg', ['format' => 'jpg'], 'image/svg+xml'];
|
2020-09-21 15:54:05 -05:00
|
|
|
}
|
2020-11-27 10:42:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRequestsWithSize
|
|
|
|
*/
|
2021-09-26 06:25:02 -05:00
|
|
|
public function imageIsReturnedWithExpectedSize(
|
|
|
|
array $defaults,
|
|
|
|
ServerRequestInterface $req,
|
|
|
|
int $expectedSize,
|
|
|
|
): void {
|
|
|
|
$this->options->setFromArray($defaults);
|
2020-11-27 10:42:33 -06:00
|
|
|
$code = 'abc123';
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
2021-01-30 07:18:44 -06:00
|
|
|
ShortUrl::createEmpty(),
|
|
|
|
);
|
2020-11-27 10:42:33 -06:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
|
|
|
|
$resp = $this->action->process($req->withAttribute('shortCode', $code), $delegate->reveal());
|
2021-12-06 11:06:29 -06:00
|
|
|
[$size] = getimagesizefromstring($resp->getBody()->__toString());
|
2020-11-27 10:42:33 -06:00
|
|
|
|
|
|
|
self::assertEquals($expectedSize, $size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRequestsWithSize(): iterable
|
|
|
|
{
|
2021-09-26 06:25:02 -05:00
|
|
|
yield 'different margin and size defaults' => [
|
|
|
|
['size' => 660, 'margin' => 40],
|
|
|
|
ServerRequestFactory::fromGlobals(),
|
|
|
|
740,
|
|
|
|
];
|
|
|
|
yield 'no size' => [[], ServerRequestFactory::fromGlobals(), 300];
|
|
|
|
yield 'no size, different default' => [['size' => 500], ServerRequestFactory::fromGlobals(), 500];
|
|
|
|
yield 'size in query' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']), 123];
|
|
|
|
yield 'size in query, default margin' => [
|
|
|
|
['margin' => 25],
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']),
|
|
|
|
173,
|
|
|
|
];
|
|
|
|
yield 'margin' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
|
|
|
|
yield 'margin and different default' => [
|
|
|
|
['size' => 400],
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']),
|
|
|
|
470,
|
|
|
|
];
|
2021-02-07 01:32:12 -06:00
|
|
|
yield 'margin and size' => [
|
2021-09-26 06:25:02 -05:00
|
|
|
[],
|
2021-02-07 01:32:12 -06:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '100', 'size' => '200']),
|
|
|
|
400,
|
|
|
|
];
|
2021-09-26 06:25:02 -05:00
|
|
|
yield 'negative margin' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']), 300];
|
|
|
|
yield 'negative margin, default margin' => [
|
|
|
|
['margin' => 10],
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']),
|
|
|
|
300,
|
|
|
|
];
|
|
|
|
yield 'non-numeric margin' => [
|
|
|
|
[],
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']),
|
|
|
|
300,
|
|
|
|
];
|
2021-02-07 01:32:12 -06:00
|
|
|
yield 'negative margin and size' => [
|
2021-09-26 06:25:02 -05:00
|
|
|
[],
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
|
|
|
|
150,
|
|
|
|
];
|
|
|
|
yield 'negative margin and size, default margin' => [
|
|
|
|
['margin' => 5],
|
2021-02-07 01:32:12 -06:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
|
|
|
|
150,
|
|
|
|
];
|
|
|
|
yield 'non-numeric margin and size' => [
|
2021-09-26 06:25:02 -05:00
|
|
|
[],
|
2021-02-07 01:32:12 -06:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo', 'size' => '538']),
|
|
|
|
538,
|
|
|
|
];
|
2020-11-27 10:42:33 -06:00
|
|
|
}
|
2021-12-06 11:06:29 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRoundBlockSize
|
|
|
|
*/
|
|
|
|
public function imageCanRemoveExtraMarginWhenBlockRoundIsDisabled(
|
|
|
|
array $defaults,
|
|
|
|
?string $roundBlockSize,
|
|
|
|
int $expectedColor,
|
|
|
|
): void {
|
|
|
|
$this->options->setFromArray($defaults);
|
|
|
|
$code = 'abc123';
|
|
|
|
$req = ServerRequestFactory::fromGlobals()
|
|
|
|
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
|
|
|
|
->withAttribute('shortCode', $code);
|
|
|
|
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
|
2021-12-06 11:06:29 -06:00
|
|
|
ShortUrl::withLongUrl('https://shlink.io'),
|
|
|
|
);
|
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
|
|
|
|
$resp = $this->action->process($req, $delegate->reveal());
|
|
|
|
$image = imagecreatefromstring($resp->getBody()->__toString());
|
|
|
|
$color = imagecolorat($image, 1, 1);
|
|
|
|
|
|
|
|
self::assertEquals($color, $expectedColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRoundBlockSize(): iterable
|
|
|
|
{
|
|
|
|
yield 'no round block param' => [[], null, self::WHITE];
|
|
|
|
yield 'no round block param, but disabled by default' => [['round_block_size' => false], null, self::BLACK];
|
|
|
|
yield 'round block: "true"' => [[], 'true', self::WHITE];
|
|
|
|
yield 'round block: "true", but disabled by default' => [['round_block_size' => false], 'true', self::WHITE];
|
|
|
|
yield 'round block: "false"' => [[], 'false', self::BLACK];
|
|
|
|
yield 'round block: "false", but enabled by default' => [['round_block_size' => true], 'false', self::BLACK];
|
|
|
|
}
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|