2016-08-09 15:18:20 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-09 15:18:20 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Action;
|
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2020-11-27 19:42:33 +03:00
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2022-10-21 19:39:22 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-11-27 19:42:33 +03:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2021-07-13 14:45:46 +03:00
|
|
|
use Psr\Log\NullLogger;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
|
|
|
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
2019-11-25 01:11:25 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2021-09-26 14:25:02 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2021-02-02 00:55:52 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2020-11-27 19:42:33 +03:00
|
|
|
use function getimagesizefromstring;
|
2021-12-06 20:06:29 +03:00
|
|
|
use function imagecolorat;
|
|
|
|
use function imagecreatefromstring;
|
2020-11-27 19:42:33 +03:00
|
|
|
|
2016-08-09 15:18:20 +03:00
|
|
|
class QrCodeActionTest extends TestCase
|
|
|
|
{
|
2021-12-06 20:06:29 +03:00
|
|
|
private const WHITE = 0xFFFFFF;
|
|
|
|
private const BLACK = 0x0;
|
|
|
|
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & ShortUrlResolverInterface $urlResolver;
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-22 20:52:28 +03:00
|
|
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
|
2022-10-21 19:39:22 +03:00
|
|
|
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
|
|
|
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
|
2017-03-25 01:19:42 +03:00
|
|
|
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-22 20:52:28 +03:00
|
|
|
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
|
2022-10-21 19:39:22 +03:00
|
|
|
)->willReturn(ShortUrl::createEmpty());
|
|
|
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$delegate->expects($this->never())->method('handle');
|
|
|
|
|
|
|
|
$resp = $this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertInstanceOf(QrCodeResponse::class, $resp);
|
|
|
|
self::assertEquals(200, $resp->getStatusCode());
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
2020-09-21 23:54:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideQueries
|
|
|
|
*/
|
|
|
|
public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
|
2021-09-26 14:25:02 +03:00
|
|
|
string $defaultFormat,
|
2020-09-21 23:54:05 +03:00
|
|
|
array $query,
|
2021-05-23 13:31:10 +03:00
|
|
|
string $expectedContentType,
|
2020-09-21 23:54:05 +03:00
|
|
|
): void {
|
|
|
|
$code = 'abc123';
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
|
2022-10-21 19:39:22 +03:00
|
|
|
)->willReturn(ShortUrl::createEmpty());
|
|
|
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
2020-09-21 23:54:05 +03:00
|
|
|
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
|
|
|
|
|
2022-10-21 19:39:22 +03:00
|
|
|
$resp = $this->action(new QrCodeOptions(format: $defaultFormat))->process($req, $delegate);
|
2020-09-21 23:54:05 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
|
2020-09-21 23:54:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideQueries(): iterable
|
|
|
|
{
|
2021-09-26 14:25:02 +03: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 23:54:05 +03:00
|
|
|
}
|
2020-11-27 19:42:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRequestsWithSize
|
|
|
|
*/
|
2021-09-26 14:25:02 +03:00
|
|
|
public function imageIsReturnedWithExpectedSize(
|
2022-09-17 14:45:09 +03:00
|
|
|
QrCodeOptions $defaultOptions,
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestInterface $req,
|
|
|
|
int $expectedSize,
|
|
|
|
): void {
|
2020-11-27 19:42:33 +03:00
|
|
|
$code = 'abc123';
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
|
2022-10-21 19:39:22 +03:00
|
|
|
)->willReturn(ShortUrl::createEmpty());
|
|
|
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
2020-11-27 19:42:33 +03:00
|
|
|
|
2022-10-21 19:39:22 +03:00
|
|
|
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate);
|
2022-10-24 21:11:25 +03:00
|
|
|
$result = getimagesizefromstring($resp->getBody()->__toString());
|
|
|
|
self::assertNotFalse($result);
|
2020-11-27 19:42:33 +03:00
|
|
|
|
2022-10-24 21:11:25 +03:00
|
|
|
[$size] = $result;
|
2020-11-27 19:42:33 +03:00
|
|
|
self::assertEquals($expectedSize, $size);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRequestsWithSize(): iterable
|
|
|
|
{
|
2021-09-26 14:25:02 +03:00
|
|
|
yield 'different margin and size defaults' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(size: 660, margin: 40),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals(),
|
|
|
|
740,
|
|
|
|
];
|
2022-09-17 14:45:09 +03:00
|
|
|
yield 'no size' => [new QrCodeOptions(), ServerRequestFactory::fromGlobals(), 300];
|
|
|
|
yield 'no size, different default' => [new QrCodeOptions(size: 500), ServerRequestFactory::fromGlobals(), 500];
|
|
|
|
yield 'size in query' => [
|
|
|
|
new QrCodeOptions(),
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']),
|
|
|
|
123,
|
|
|
|
];
|
2021-09-26 14:25:02 +03:00
|
|
|
yield 'size in query, default margin' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(margin: 25),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']),
|
|
|
|
173,
|
|
|
|
];
|
2022-09-17 14:45:09 +03:00
|
|
|
yield 'margin' => [
|
|
|
|
new QrCodeOptions(),
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']),
|
|
|
|
370,
|
|
|
|
];
|
2021-09-26 14:25:02 +03:00
|
|
|
yield 'margin and different default' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(size: 400),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']),
|
|
|
|
470,
|
|
|
|
];
|
2021-02-07 10:32:12 +03:00
|
|
|
yield 'margin and size' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(),
|
2021-02-07 10:32:12 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '100', 'size' => '200']),
|
|
|
|
400,
|
|
|
|
];
|
2022-09-17 14:45:09 +03:00
|
|
|
yield 'negative margin' => [
|
|
|
|
new QrCodeOptions(),
|
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']),
|
|
|
|
300,
|
|
|
|
];
|
2021-09-26 14:25:02 +03:00
|
|
|
yield 'negative margin, default margin' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(margin: 10),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']),
|
|
|
|
300,
|
|
|
|
];
|
|
|
|
yield 'non-numeric margin' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']),
|
|
|
|
300,
|
|
|
|
];
|
2021-02-07 10:32:12 +03:00
|
|
|
yield 'negative margin and size' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(),
|
2021-09-26 14:25:02 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
|
|
|
|
150,
|
|
|
|
];
|
|
|
|
yield 'negative margin and size, default margin' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(margin: 5),
|
2021-02-07 10:32:12 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
|
|
|
|
150,
|
|
|
|
];
|
|
|
|
yield 'non-numeric margin and size' => [
|
2022-09-17 14:45:09 +03:00
|
|
|
new QrCodeOptions(),
|
2021-02-07 10:32:12 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo', 'size' => '538']),
|
|
|
|
538,
|
|
|
|
];
|
2020-11-27 19:42:33 +03:00
|
|
|
}
|
2021-12-06 20:06:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRoundBlockSize
|
|
|
|
*/
|
|
|
|
public function imageCanRemoveExtraMarginWhenBlockRoundIsDisabled(
|
2022-09-17 14:45:09 +03:00
|
|
|
QrCodeOptions $defaultOptions,
|
2021-12-06 20:06:29 +03:00
|
|
|
?string $roundBlockSize,
|
|
|
|
int $expectedColor,
|
|
|
|
): void {
|
|
|
|
$code = 'abc123';
|
|
|
|
$req = ServerRequestFactory::fromGlobals()
|
|
|
|
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
|
|
|
|
->withAttribute('shortCode', $code);
|
|
|
|
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($code, ''),
|
2022-10-21 19:39:22 +03:00
|
|
|
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
|
|
|
|
$delegate = $this->createMock(RequestHandlerInterface::class);
|
2021-12-06 20:06:29 +03:00
|
|
|
|
2022-10-21 19:39:22 +03:00
|
|
|
$resp = $this->action($defaultOptions)->process($req, $delegate);
|
2021-12-06 20:06:29 +03:00
|
|
|
$image = imagecreatefromstring($resp->getBody()->__toString());
|
2022-10-24 21:11:25 +03:00
|
|
|
self::assertNotFalse($image);
|
2021-12-06 20:06:29 +03:00
|
|
|
|
2022-10-24 21:11:25 +03:00
|
|
|
$color = imagecolorat($image, 1, 1);
|
2021-12-06 20:06:29 +03:00
|
|
|
self::assertEquals($color, $expectedColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRoundBlockSize(): iterable
|
|
|
|
{
|
2022-09-17 14:45:09 +03:00
|
|
|
yield 'no round block param' => [new QrCodeOptions(), null, self::WHITE];
|
|
|
|
yield 'no round block param, but disabled by default' => [
|
|
|
|
new QrCodeOptions(roundBlockSize: false),
|
|
|
|
null,
|
|
|
|
self::BLACK,
|
|
|
|
];
|
|
|
|
yield 'round block: "true"' => [new QrCodeOptions(), 'true', self::WHITE];
|
|
|
|
yield 'round block: "true", but disabled by default' => [
|
|
|
|
new QrCodeOptions(roundBlockSize: false),
|
|
|
|
'true',
|
|
|
|
self::WHITE,
|
|
|
|
];
|
|
|
|
yield 'round block: "false"' => [new QrCodeOptions(), 'false', self::BLACK];
|
|
|
|
yield 'round block: "false", but enabled by default' => [
|
|
|
|
new QrCodeOptions(roundBlockSize: true),
|
|
|
|
'false',
|
|
|
|
self::BLACK,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function action(?QrCodeOptions $options = null): QrCodeAction
|
|
|
|
{
|
|
|
|
return new QrCodeAction(
|
2022-10-21 19:39:22 +03:00
|
|
|
$this->urlResolver,
|
2022-09-17 14:45:09 +03:00
|
|
|
new ShortUrlStringifier(['domain' => 'doma.in']),
|
|
|
|
new NullLogger(),
|
|
|
|
$options ?? new QrCodeOptions(),
|
|
|
|
);
|
2021-12-06 20:06:29 +03:00
|
|
|
}
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|