diff --git a/module/Core/src/Action/Model/QrCodeParams.php b/module/Core/src/Action/Model/QrCodeParams.php index 47fb82d4..03643e4c 100644 --- a/module/Core/src/Action/Model/QrCodeParams.php +++ b/module/Core/src/Action/Model/QrCodeParams.php @@ -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(); } diff --git a/module/Core/test/Action/QrCodeActionTest.php b/module/Core/test/Action/QrCodeActionTest.php index 1fdc35ef..664a51a2 100644 --- a/module/Core/test/Action/QrCodeActionTest.php +++ b/module/Core/test/Action/QrCodeActionTest.php @@ -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]; + } }