Added new config to set custom defaults for QR codes

This commit is contained in:
Alejandro Celaya
2021-09-26 13:25:02 +02:00
parent cbec4a4e81
commit 6a1ee2b894
7 changed files with 174 additions and 31 deletions

View File

@@ -20,6 +20,7 @@ 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\Options\QrCodeOptions;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
@@ -31,6 +32,7 @@ class QrCodeActionTest extends TestCase
private QrCodeAction $action;
private ObjectProphecy $urlResolver;
private QrCodeOptions $options;
public function setUp(): void
{
@@ -38,11 +40,13 @@ class QrCodeActionTest extends TestCase
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->options = new QrCodeOptions();
$this->action = new QrCodeAction(
$this->urlResolver->reveal(),
new ShortUrlStringifier(['domain' => 'doma.in']),
new NullLogger(),
$this->options,
);
}
@@ -85,9 +89,11 @@ class QrCodeActionTest extends TestCase
* @dataProvider provideQueries
*/
public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
string $defaultFormat,
array $query,
string $expectedContentType,
): void {
$this->options->setFromArray(['format' => $defaultFormat]);
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(
ShortUrl::createEmpty(),
@@ -102,18 +108,26 @@ class QrCodeActionTest extends TestCase
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'];
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'];
}
/**
* @test
* @dataProvider provideRequestsWithSize
*/
public function imageIsReturnedWithExpectedSize(ServerRequestInterface $req, int $expectedSize): void
{
public function imageIsReturnedWithExpectedSize(
array $defaults,
ServerRequestInterface $req,
int $expectedSize,
): void {
$this->options->setFromArray($defaults);
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(
ShortUrl::createEmpty(),
@@ -128,25 +142,59 @@ class QrCodeActionTest extends TestCase
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 '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 attr' => [[], ServerRequestFactory::fromGlobals()->withAttribute('size', '400'), 400];
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 'size in query and attr' => [
[],
ServerRequestFactory::fromGlobals()->withAttribute('size', '350')->withQueryParams(['size' => '123']),
350,
];
yield 'margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
yield 'margin' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
yield 'margin and different default' => [
['size' => 400],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']),
470,
];
yield 'margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '100', 'size' => '200']),
400,
];
yield 'negative margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']), 300];
yield 'non-numeric margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']), 300];
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,
];
yield 'negative margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
150,
];
yield 'negative margin and size, default margin' => [
['margin' => 5],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
150,
];
yield 'non-numeric margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo', 'size' => '538']),
538,
];