Skip to content

Commit

Permalink
Fix font handling + simplify Twig extension
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Oct 21, 2024
1 parent a7605f0 commit 379b3e7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/DependencyInjection/EndroidQrCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Label\Font\Font;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\Label\Font\OpenSans;
use Endroid\QrCode\Label\LabelAlignment;
use Endroid\QrCode\RoundBlockSizeMode;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -81,7 +81,7 @@ private function createBuilderDefinition(string $builderName, array $builderConf
$arguments['labelFont'] = new Definition(Font::class, [$value, $labelFontSize]);
break;
case 'labelFontSize':
$labelFontPath = $builderConfig['labelFontPath'] ?? (new NotoSans())->getPath();
$labelFontPath = $builderConfig['labelFontPath'] ?? (new OpenSans())->getPath();
$arguments['labelFont'] = new Definition(Font::class, [$labelFontPath, $value]);
break;
case 'labelAlignment':
Expand Down
11 changes: 3 additions & 8 deletions src/Twig/QrCodeRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ public function qrCodeResultFunction(string $data, string $builder = 'default',
{
$builder = $this->builderRegistry->get($builder);

foreach ($options as $option => $value) {
if (!method_exists($builder, $option)) {
throw new \Exception(sprintf('Builder option "%s" does not exist', $option));
}
$builder->$option($value);
}

if (!$builder instanceof Builder) {
throw new \Exception('This twig extension only handles Builder instances');
}

return $builder->build(data: $data);
$options['data'] = $data;

return $builder->build(...$options);
}
}
Binary file removed tests/application/assets/open_sans.ttf
Binary file not shown.
15 changes: 6 additions & 9 deletions tests/application/tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Builder\BuilderRegistryInterface;
use Endroid\QrCode\Writer\Result\SvgResult;
use PHPUnit\Framework\Attributes\TestDox;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class BuilderTest extends WebTestCase
{
/**
* @testdox The builder registry contains the configured builders
*/
#[TestDox('The builder registry contains the configured builders')]
public function testBuilderRegistry(): void
{
self::bootKernel();
Expand All @@ -27,16 +26,14 @@ public function testBuilderRegistry(): void
/** @var BuilderRegistryInterface $builderRegistry */
$builderRegistry = $container->get(BuilderRegistryInterface::class);

$defaultBuilder = $builderRegistry->getBuilder('default');
$defaultBuilder = $builderRegistry->get('default');
$this->assertInstanceOf(Builder::class, $defaultBuilder);

$customBuilder = $builderRegistry->getBuilder('custom');
$customBuilder = $builderRegistry->get('custom');
$this->assertInstanceOf(Builder::class, $customBuilder);
}

/**
* @testdox Builder can generate QR code
*/
#[TestDox('The builder can generate a QR code')]
public function testBuilderDefault()
{
self::bootKernel();
Expand All @@ -50,7 +47,7 @@ public function testBuilderDefault()
/** @var BuilderRegistryInterface $builderRegistry */
$builderRegistry = $container->get(BuilderRegistryInterface::class);

$customBuilder = $builderRegistry->getBuilder('custom');
$customBuilder = $builderRegistry->get('custom');
$result = $customBuilder->build();

$this->assertInstanceOf(SvgResult::class, $result);
Expand Down
5 changes: 2 additions & 3 deletions tests/application/tests/Controller/GenerateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

namespace Endroid\QrCodeBundle\Tests;

use PHPUnit\Framework\Attributes\TestDox;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class GenerateControllerTest extends WebTestCase
{
/**
* @testdox QR Codes can be generated via the url
*/
#[TestDox('QR Codes can be generated via the url')]
public function testGenerateController()
{
$client = static::createClient();
Expand Down

0 comments on commit 379b3e7

Please sign in to comment.