Added test checking if auto margin is added to QR codes

This commit is contained in:
Alejandro Celaya 2021-12-06 18:06:29 +01:00
parent 1a75bd87d8
commit 813ae71aad
2 changed files with 46 additions and 2 deletions

View File

@ -97,7 +97,9 @@ final class QrCodeParams
private static function resolveRoundBlockSize(array $query, QrCodeOptions $defaults): RoundBlockSizeModeInterface
{
$doNotRoundBlockSize = ($query['roundBlockSize'] ?? null) === 'false' || ! $defaults->roundBlockSize();
$doNotRoundBlockSize = isset($query['roundBlockSize'])
? $query['roundBlockSize'] === 'false'
: ! $defaults->roundBlockSize();
return $doNotRoundBlockSize ? new RoundBlockSizeModeNone() : new RoundBlockSizeModeMargin();
}

View File

@ -25,11 +25,16 @@ use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use function getimagesizefromstring;
use function imagecolorat;
use function imagecreatefromstring;
class QrCodeActionTest extends TestCase
{
use ProphecyTrait;
private const WHITE = 0xFFFFFF;
private const BLACK = 0x0;
private QrCodeAction $action;
private ObjectProphecy $urlResolver;
private QrCodeOptions $options;
@ -135,7 +140,7 @@ class QrCodeActionTest extends TestCase
$delegate = $this->prophesize(RequestHandlerInterface::class);
$resp = $this->action->process($req->withAttribute('shortCode', $code), $delegate->reveal());
[$size] = getimagesizefromstring((string) $resp->getBody());
[$size] = getimagesizefromstring($resp->getBody()->__toString());
self::assertEquals($expectedSize, $size);
}
@ -199,4 +204,41 @@ class QrCodeActionTest extends TestCase
538,
];
}
/**
* @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);
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(
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];
}
}