From bf6a4388e13159d0029763ab87486c9e3cf581ef Mon Sep 17 00:00:00 2001 From: Roshyo Date: Mon, 17 Jan 2022 12:20:28 +0100 Subject: [PATCH 1/9] Allow to define custom rendering options for wkhtmltopdf --- src/Form/Type/GiftCardConfigurationType.php | 40 ++++++++++++++ src/Generator/GiftCardPdfGenerator.php | 15 ++++-- src/Model/GiftCardConfiguration.php | 26 +++++++++ src/Model/GiftCardConfigurationInterface.php | 8 +++ src/Provider/PdfOrientationProvider.php | 16 ++++++ .../PdfOrientationProviderInterface.php | 13 +++++ src/Provider/PdfPageSizeProvider.php | 20 +++++++ src/Provider/PdfPageSizeProviderInterface.php | 17 ++++++ src/Provider/PdfRenderingOptionProvider.php | 26 +++++++++ .../PdfRenderingOptionProviderInterface.php | 12 +++++ .../model/GiftCardConfiguration.orm.xml | 2 + src/Resources/config/services/form.xml | 2 + src/Resources/config/services/misc.xml | 1 + src/Resources/config/services/provider.xml | 15 ++++++ .../GiftCardConfiguration/_form.html.twig | 2 + .../Generator/GiftCardPdfGeneratorTest.php | 11 +++- .../Unit/Model/GiftCardConfigurationTest.php | 6 +++ .../Provider/PdfOrientationProviderTest.php | 21 ++++++++ .../Unit/Provider/PdfPageSizeProviderTest.php | 21 ++++++++ .../PdfRenderingOptionProviderTest.php | 54 +++++++++++++++++++ 20 files changed, 323 insertions(+), 5 deletions(-) create mode 100644 src/Provider/PdfOrientationProvider.php create mode 100644 src/Provider/PdfOrientationProviderInterface.php create mode 100644 src/Provider/PdfPageSizeProvider.php create mode 100644 src/Provider/PdfPageSizeProviderInterface.php create mode 100644 src/Provider/PdfRenderingOptionProvider.php create mode 100644 src/Provider/PdfRenderingOptionProviderInterface.php create mode 100644 tests/Unit/Provider/PdfOrientationProviderTest.php create mode 100644 tests/Unit/Provider/PdfPageSizeProviderTest.php create mode 100644 tests/Unit/Provider/PdfRenderingOptionProviderTest.php diff --git a/src/Form/Type/GiftCardConfigurationType.php b/src/Form/Type/GiftCardConfigurationType.php index 81cf4c3d..acedd0b4 100644 --- a/src/Form/Type/GiftCardConfigurationType.php +++ b/src/Form/Type/GiftCardConfigurationType.php @@ -4,15 +4,37 @@ namespace Setono\SyliusGiftCardPlugin\Form\Type; +use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface; use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; final class GiftCardConfigurationType extends AbstractResourceType { + private PdfPageSizeProviderInterface $pdfFormatProvider; + + private PdfOrientationProviderInterface $pdfOrientationProvider; + + /** + * @psalm-param array $validationGroups + */ + public function __construct( + PdfPageSizeProviderInterface $pdfFormatProvider, + PdfOrientationProviderInterface $pdfOrientationProvider, + string $dataClass, + array $validationGroups + ) { + parent::__construct($dataClass, $validationGroups); + + $this->pdfFormatProvider = $pdfFormatProvider; + $this->pdfOrientationProvider = $pdfOrientationProvider; + } + public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('code', TextType::class, [ @@ -42,6 +64,24 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->add('defaultValidityPeriod', DatePeriodType::class, [ 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.default_validity_period', ]); + $builder->add('pageSize', ChoiceType::class, [ + 'choices' => $this->pdfFormatProvider->getAvailablePageSizes(), + 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.page_size', + 'choice_translation_domain' => false, + 'choice_label' => function (string $value) { + return $value; + }, + 'placeholder' => PdfPageSizeProviderInterface::FORMAT_A5, + 'empty_data' => PdfPageSizeProviderInterface::FORMAT_A5, + ]); + $builder->add('orientation', ChoiceType::class, [ + 'choices' => $this->pdfOrientationProvider->getAvailableOrientations(), + 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.orientation', + 'choice_translation_domain' => false, + 'choice_label' => function (string $value) { + return $value; + }, + ]); $builder->get('defaultValidityPeriod')->addModelTransformer( new CallbackTransformer( function (?string $period): array { diff --git a/src/Generator/GiftCardPdfGenerator.php b/src/Generator/GiftCardPdfGenerator.php index 106fd796..4bf23e5f 100644 --- a/src/Generator/GiftCardPdfGenerator.php +++ b/src/Generator/GiftCardPdfGenerator.php @@ -8,6 +8,7 @@ use Knp\Snappy\GeneratorInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProviderInterface; use Twig\Environment; class GiftCardPdfGenerator implements GiftCardPdfGeneratorInterface @@ -16,10 +17,16 @@ class GiftCardPdfGenerator implements GiftCardPdfGeneratorInterface private GeneratorInterface $snappy; - public function __construct(Environment $twig, GeneratorInterface $snappy) - { + private PdfRenderingOptionProviderInterface $renderingOptionProvider; + + public function __construct( + Environment $twig, + GeneratorInterface $snappy, + PdfRenderingOptionProviderInterface $renderingOptionProvider + ) { $this->twig = $twig; $this->snappy = $snappy; + $this->renderingOptionProvider = $renderingOptionProvider; } public function generatePdfResponse( @@ -31,6 +38,8 @@ public function generatePdfResponse( 'configuration' => $giftCardChannelConfiguration, ]); - return new PdfResponse($this->snappy->getOutputFromHtml($html), 'gift_card.pdf'); + $renderingOptions = $this->renderingOptionProvider->getRenderingOptions($giftCardChannelConfiguration); + + return new PdfResponse($this->snappy->getOutputFromHtml($html, $renderingOptions), 'gift_card.pdf'); } } diff --git a/src/Model/GiftCardConfiguration.php b/src/Model/GiftCardConfiguration.php index b6ed2f46..d516946b 100644 --- a/src/Model/GiftCardConfiguration.php +++ b/src/Model/GiftCardConfiguration.php @@ -6,6 +6,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface; use Sylius\Component\Core\Model\ImageInterface; use Sylius\Component\Resource\Model\TimestampableTrait; use Sylius\Component\Resource\Model\ToggleableTrait; @@ -38,6 +40,10 @@ class GiftCardConfiguration implements GiftCardConfigurationInterface protected ?string $defaultValidityPeriod = null; + protected ?string $pageSize = PdfPageSizeProviderInterface::FORMAT_A5; + + protected ?string $orientation = PdfOrientationProviderInterface::ORIENTATION_PORTRAIT; + public function __construct() { $this->images = new ArrayCollection(); @@ -177,4 +183,24 @@ public function setDefaultValidityPeriod(?string $defaultValidityPeriod): void { $this->defaultValidityPeriod = $defaultValidityPeriod; } + + public function getPageSize(): ?string + { + return $this->pageSize; + } + + public function setPageSize(?string $pageSize): void + { + $this->pageSize = $pageSize; + } + + public function getOrientation(): ?string + { + return $this->orientation; + } + + public function setOrientation(?string $orientation): void + { + $this->orientation = $orientation; + } } diff --git a/src/Model/GiftCardConfigurationInterface.php b/src/Model/GiftCardConfigurationInterface.php index 6bfeb32e..0dfe6cb7 100644 --- a/src/Model/GiftCardConfigurationInterface.php +++ b/src/Model/GiftCardConfigurationInterface.php @@ -42,4 +42,12 @@ public function setDefault(bool $default): void; public function getDefaultValidityPeriod(): ?string; public function setDefaultValidityPeriod(?string $defaultValidityPeriod): void; + + public function getPageSize(): ?string; + + public function setPageSize(?string $pageSize): void; + + public function getOrientation(): ?string; + + public function setOrientation(?string $orientation): void; } diff --git a/src/Provider/PdfOrientationProvider.php b/src/Provider/PdfOrientationProvider.php new file mode 100644 index 00000000..caa5761c --- /dev/null +++ b/src/Provider/PdfOrientationProvider.php @@ -0,0 +1,16 @@ +getPageSize(); + if (null !== $pageSize) { + $options['page-size'] = $pageSize; + } + + $orientation = $giftCardConfiguration->getOrientation(); + if (null !== $orientation) { + $options['orientation'] = $orientation; + } + + return $options; + } +} diff --git a/src/Provider/PdfRenderingOptionProviderInterface.php b/src/Provider/PdfRenderingOptionProviderInterface.php new file mode 100644 index 00000000..8d3b45a2 --- /dev/null +++ b/src/Provider/PdfRenderingOptionProviderInterface.php @@ -0,0 +1,12 @@ + + + diff --git a/src/Resources/config/services/form.xml b/src/Resources/config/services/form.xml index 406e0b6a..c2e6b6fb 100644 --- a/src/Resources/config/services/form.xml +++ b/src/Resources/config/services/form.xml @@ -79,6 +79,8 @@ + + %setono_sylius_gift_card.model.gift_card_configuration.class% %setono_sylius_gift_card.form.type.gift_card_configuration.validation_groups% diff --git a/src/Resources/config/services/misc.xml b/src/Resources/config/services/misc.xml index 22814a10..8de4e97f 100644 --- a/src/Resources/config/services/misc.xml +++ b/src/Resources/config/services/misc.xml @@ -14,6 +14,7 @@ class="Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGenerator"> + diff --git a/src/Resources/config/services/provider.xml b/src/Resources/config/services/provider.xml index d18d7b9e..3c6b945a 100644 --- a/src/Resources/config/services/provider.xml +++ b/src/Resources/config/services/provider.xml @@ -23,5 +23,20 @@ class="Setono\SyliusGiftCardPlugin\Provider\DatePeriodUnitProvider" /> + + + + + + + + + diff --git a/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig b/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig index 41c3b76d..1ccd3219 100644 --- a/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig +++ b/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig @@ -13,6 +13,8 @@ {{ form_row(form.backgroundImage) }} + {{ form_row(form.pageSize) }} + {{ form_row(form.orientation) }}
diff --git a/tests/Unit/Generator/GiftCardPdfGeneratorTest.php b/tests/Unit/Generator/GiftCardPdfGeneratorTest.php index 1e278765..6f14283a 100644 --- a/tests/Unit/Generator/GiftCardPdfGeneratorTest.php +++ b/tests/Unit/Generator/GiftCardPdfGeneratorTest.php @@ -10,6 +10,7 @@ use Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGenerator; use Setono\SyliusGiftCardPlugin\Model\GiftCard; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProviderInterface; use Twig\Environment; final class GiftCardPdfGeneratorTest extends TestCase @@ -26,14 +27,20 @@ public function it_generates_pdf_response_for_gift_card(): void $twig = $this->prophesize(Environment::class); $snappy = $this->prophesize(Pdf::class); + $renderingOptionProvider = $this->prophesize(PdfRenderingOptionProviderInterface::class); + $renderingOptionProvider->getRenderingOptions($giftCardChannelConfiguration)->willReturn([]); $twig->render('@SetonoSyliusGiftCardPlugin/Shop/GiftCard/pdf.html.twig', [ 'giftCard' => $giftCard, 'configuration' => $giftCardChannelConfiguration, ])->willReturn('super GiftCard template'); - $snappy->getOutputFromHtml('super GiftCard template')->willReturn('super GiftCard template'); + $snappy->getOutputFromHtml('super GiftCard template', [])->willReturn('super GiftCard template'); - $giftCardPdfGenerator = new GiftCardPdfGenerator($twig->reveal(), $snappy->reveal()); + $giftCardPdfGenerator = new GiftCardPdfGenerator( + $twig->reveal(), + $snappy->reveal(), + $renderingOptionProvider->reveal() + ); $response = $giftCardPdfGenerator->generatePdfResponse($giftCard, $giftCardChannelConfiguration); $this->assertEquals('application/pdf', $response->headers->get('Content-type')); diff --git a/tests/Unit/Model/GiftCardConfigurationTest.php b/tests/Unit/Model/GiftCardConfigurationTest.php index abc6e2fd..750a655e 100644 --- a/tests/Unit/Model/GiftCardConfigurationTest.php +++ b/tests/Unit/Model/GiftCardConfigurationTest.php @@ -24,5 +24,11 @@ public function it_has_properties(): void $giftCardConfiguration->setDefaultValidityPeriod('2 months'); $this->assertEquals('2 months', $giftCardConfiguration->getDefaultValidityPeriod()); + + $giftCardConfiguration->setPageSize('A7'); + $this->assertEquals('A7', $giftCardConfiguration->getPageSize()); + + $giftCardConfiguration->setOrientation('Landscape'); + $this->assertEquals('Landscape', $giftCardConfiguration->getOrientation()); } } diff --git a/tests/Unit/Provider/PdfOrientationProviderTest.php b/tests/Unit/Provider/PdfOrientationProviderTest.php new file mode 100644 index 00000000..144fcdb7 --- /dev/null +++ b/tests/Unit/Provider/PdfOrientationProviderTest.php @@ -0,0 +1,21 @@ +assertIsArray($provider->getAvailableOrientations()); + } +} diff --git a/tests/Unit/Provider/PdfPageSizeProviderTest.php b/tests/Unit/Provider/PdfPageSizeProviderTest.php new file mode 100644 index 00000000..af05e798 --- /dev/null +++ b/tests/Unit/Provider/PdfPageSizeProviderTest.php @@ -0,0 +1,21 @@ +assertIsArray($provider->getAvailablePageSizes()); + } +} diff --git a/tests/Unit/Provider/PdfRenderingOptionProviderTest.php b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php new file mode 100644 index 00000000..02a79287 --- /dev/null +++ b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php @@ -0,0 +1,54 @@ +getRenderingOptions($giftCardConfiguration); + $this->assertIsArray($renderingOptions); + } + + /** + * @test + */ + public function it_provides_page_size_if_not_null(): void + { + $provider = new PdfRenderingOptionProvider(); + $giftCardConfiguration = new GiftCardConfiguration(); + $giftCardConfiguration->setPageSize(PdfPageSizeProviderInterface::FORMAT_A8); + + $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); + $this->assertArrayHasKey('page-size', $renderingOptions); + $this->assertEquals(PdfPageSizeProviderInterface::FORMAT_A8, $renderingOptions['page-size']); + } + + /** + * @test + */ + public function it_provides_orientation_if_not_null(): void + { + $provider = new PdfRenderingOptionProvider(); + $giftCardConfiguration = new GiftCardConfiguration(); + $giftCardConfiguration->setOrientation(PdfOrientationProviderInterface::ORIENTATION_LANDSCAPE); + + $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); + $this->assertArrayHasKey('orientation', $renderingOptions); + $this->assertEquals(PdfOrientationProviderInterface::ORIENTATION_LANDSCAPE, $renderingOptions['orientation']); + } +} From afd65ca71334c27db1201548c46a839bd899da51 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Mon, 17 Jan 2022 12:26:19 +0100 Subject: [PATCH 2/9] Add blank line between constants --- src/Provider/PdfOrientationProviderInterface.php | 1 + src/Provider/PdfPageSizeProviderInterface.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/Provider/PdfOrientationProviderInterface.php b/src/Provider/PdfOrientationProviderInterface.php index e6548db1..d0a25256 100644 --- a/src/Provider/PdfOrientationProviderInterface.php +++ b/src/Provider/PdfOrientationProviderInterface.php @@ -7,6 +7,7 @@ interface PdfOrientationProviderInterface { public const ORIENTATION_PORTRAIT = 'Portrait'; + public const ORIENTATION_LANDSCAPE = 'Landscape'; public function getAvailableOrientations(): array; diff --git a/src/Provider/PdfPageSizeProviderInterface.php b/src/Provider/PdfPageSizeProviderInterface.php index f8e787ee..485147e0 100644 --- a/src/Provider/PdfPageSizeProviderInterface.php +++ b/src/Provider/PdfPageSizeProviderInterface.php @@ -7,10 +7,15 @@ interface PdfPageSizeProviderInterface { public const FORMAT_A3 = 'A3'; + public const FORMAT_A4 = 'A4'; + public const FORMAT_A5 = 'A5'; + public const FORMAT_A6 = 'A6'; + public const FORMAT_A7 = 'A7'; + public const FORMAT_A8 = 'A8'; public function getAvailablePageSizes(): array; From e3f59d615f26467f54b30b2f8d87ff82ef1300f6 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Mon, 24 Jan 2022 13:15:51 +0100 Subject: [PATCH 3/9] Refactor into Plugin configuration + validate input --- composer.json | 1 + psalm-baseline.xml | 124 ------------------ psalm.xml | 1 + src/DependencyInjection/Configuration.php | 49 +++++++ .../SetonoSyliusGiftCardExtension.php | 28 +++- src/Factory/GiftCardConfigurationFactory.php | 17 ++- src/Form/Type/GiftCardConfigurationType.php | 24 ++-- src/Model/GiftCardConfiguration.php | 6 +- src/Provider/PdfOrientationProvider.php | 16 --- .../PdfOrientationProviderInterface.php | 14 -- src/Provider/PdfPageSizeProvider.php | 20 --- src/Provider/PdfPageSizeProviderInterface.php | 22 ---- src/Resources/config/services/factory.xml | 2 + src/Resources/config/services/form.xml | 5 +- src/Resources/config/services/provider.xml | 10 -- src/Resources/config/services/validator.xml | 12 ++ .../validation/GiftCardConfiguration.xml | 10 ++ .../Constraints/Pdf/ValidOrientation.php | 12 ++ .../Pdf/ValidOrientationValidator.php | 35 +++++ .../Constraints/Pdf/ValidPageSize.php | 12 ++ .../Pdf/ValidPageSizeValidator.php | 35 +++++ .../packages/setono_sylius_gift_card.yaml | 3 - .../test/setono_sylius_gift_card.yaml | 9 ++ .../DependencyInjection/ConfigurationTest.php | 63 ++++++++- .../Provider/PdfOrientationProviderTest.php | 21 --- .../Unit/Provider/PdfPageSizeProviderTest.php | 21 --- .../PdfRenderingOptionProviderTest.php | 10 +- .../Pdf/ValidOrientationValidatorTest.php | 47 +++++++ .../Pdf/ValidPageSizeValidatorTest.php | 47 +++++++ 29 files changed, 394 insertions(+), 282 deletions(-) delete mode 100644 src/Provider/PdfOrientationProvider.php delete mode 100644 src/Provider/PdfOrientationProviderInterface.php delete mode 100644 src/Provider/PdfPageSizeProvider.php delete mode 100644 src/Provider/PdfPageSizeProviderInterface.php create mode 100644 src/Validator/Constraints/Pdf/ValidOrientation.php create mode 100644 src/Validator/Constraints/Pdf/ValidOrientationValidator.php create mode 100644 src/Validator/Constraints/Pdf/ValidPageSize.php create mode 100644 src/Validator/Constraints/Pdf/ValidPageSizeValidator.php delete mode 100644 tests/Application/config/packages/setono_sylius_gift_card.yaml create mode 100644 tests/Application/config/packages/test/setono_sylius_gift_card.yaml delete mode 100644 tests/Unit/Provider/PdfOrientationProviderTest.php delete mode 100644 tests/Unit/Provider/PdfPageSizeProviderTest.php create mode 100644 tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php create mode 100644 tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php diff --git a/composer.json b/composer.json index b70e4501..477b3772 100644 --- a/composer.json +++ b/composer.json @@ -71,6 +71,7 @@ }, "config": { "allow-plugins": { + "composer/package-versions-deprecated": true, "dealerdirect/phpcodesniffer-composer-installer": false, "ergebnis/composer-normalize": true, "symfony/thanks": true diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 6c76412a..096af698 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -21,130 +21,6 @@ $giftCard->getOrder() - - - addDefaultsIfNotSet - addDefaultsIfNotSet - addDefaultsIfNotSet - addDefaultsIfNotSet - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - cannotBeEmpty - children - children - children - children - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - defaultValue - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - end - example - info - max - min - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - scalarNode - - - arrayNode - arrayNode - arrayNode - arrayNode - end - integerNode - - - arrayNode - arrayNode - arrayNode - arrayNode - end - integerNode - - - end - - array diff --git a/psalm.xml b/psalm.xml index 2fd56eac..aa550cb0 100644 --- a/psalm.xml +++ b/psalm.xml @@ -13,6 +13,7 @@ + diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 235023b6..5113f322 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -37,6 +37,55 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->addDefaultsIfNotSet() ->children() + ->arrayNode('pdf_rendering') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('default_orientation')->defaultValue('Portrait')->end() + ->arrayNode('available_orientations') + ->scalarPrototype()->end() + ->defaultValue([ + 'Portrait', + 'Landscape', + ]) + ->end() + ->scalarNode('default_page_size')->defaultValue('A4')->end() + ->arrayNode('available_page_sizes') + ->scalarPrototype()->end() + ->defaultValue([ + 'A0', + 'A1', + 'A2', + 'A3', + 'A4', + 'A5', + 'A6', + 'A7', + 'A8', + 'A9', + 'B0', + 'B1', + 'B2', + 'B3', + 'B4', + 'B5', + 'B6', + 'B7', + 'B8', + 'B9', + 'B10', + 'C5E', + 'Comm10E', + 'DLE', + 'Executive', + 'Folio', + 'Ledger', + 'Legal', + 'Letter', + 'Tabloid', + ]) + ->end() + ->end() + ->end() ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() ->integerNode('code_length') ->defaultValue(20) diff --git a/src/DependencyInjection/SetonoSyliusGiftCardExtension.php b/src/DependencyInjection/SetonoSyliusGiftCardExtension.php index 00452b31..6cf5bce2 100644 --- a/src/DependencyInjection/SetonoSyliusGiftCardExtension.php +++ b/src/DependencyInjection/SetonoSyliusGiftCardExtension.php @@ -15,13 +15,39 @@ final class SetonoSyliusGiftCardExtension extends AbstractResourceExtension public function load(array $configs, ContainerBuilder $container): void { /** - * @var array{code_length: int, driver: string, resources: array} $config + * @var array{ + * pdf_rendering: array{ + * default_orientation: string, + * available_orientations: array, + * default_page_size: string, + * available_page_sizes: array, + * }, + * code_length: int, + * driver: string, + * resources: array + * } $config * @psalm-suppress PossiblyNullArgument */ $config = $this->processConfiguration($this->getConfiguration([], $container), $configs); $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $container->setParameter('setono_sylius_gift_card.code_length', $config['code_length']); + $container->setParameter( + 'setono_sylius_gift_card.pdf_rendering.default_orientation', + $config['pdf_rendering']['default_orientation'] + ); + $container->setParameter( + 'setono_sylius_gift_card.pdf_rendering.available_orientations', + $config['pdf_rendering']['available_orientations'] + ); + $container->setParameter( + 'setono_sylius_gift_card.pdf_rendering.default_page_size', + $config['pdf_rendering']['default_page_size'] + ); + $container->setParameter( + 'setono_sylius_gift_card.pdf_rendering.available_page_sizes', + $config['pdf_rendering']['available_page_sizes'] + ); $this->registerResources('setono_sylius_gift_card', $config['driver'], $config['resources'], $container); diff --git a/src/Factory/GiftCardConfigurationFactory.php b/src/Factory/GiftCardConfigurationFactory.php index c13e616d..d8f89360 100644 --- a/src/Factory/GiftCardConfigurationFactory.php +++ b/src/Factory/GiftCardConfigurationFactory.php @@ -14,10 +14,20 @@ final class GiftCardConfigurationFactory implements GiftCardConfigurationFactory private FactoryInterface $imageFactory; - public function __construct(FactoryInterface $decoratedFactory, FactoryInterface $imageFactory) - { + private string $defaultOrientation; + + private string $defaultPageSize; + + public function __construct( + FactoryInterface $decoratedFactory, + FactoryInterface $imageFactory, + string $defaultOrientation, + string $defaultPageSize + ) { $this->decoratedFactory = $decoratedFactory; $this->imageFactory = $imageFactory; + $this->defaultOrientation = $defaultOrientation; + $this->defaultPageSize = $defaultPageSize; } public function createNew(): GiftCardConfigurationInterface @@ -29,6 +39,9 @@ public function createNew(): GiftCardConfigurationInterface $backgroundImage = $this->imageFactory->createNew(); $configuration->setBackgroundImage($backgroundImage); + $configuration->setOrientation($this->defaultOrientation); + $configuration->setPageSize($this->defaultPageSize); + return $configuration; } } diff --git a/src/Form/Type/GiftCardConfigurationType.php b/src/Form/Type/GiftCardConfigurationType.php index acedd0b4..adc2da9b 100644 --- a/src/Form/Type/GiftCardConfigurationType.php +++ b/src/Form/Type/GiftCardConfigurationType.php @@ -4,8 +4,6 @@ namespace Setono\SyliusGiftCardPlugin\Form\Type; -use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface; -use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface; use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -16,23 +14,23 @@ final class GiftCardConfigurationType extends AbstractResourceType { - private PdfPageSizeProviderInterface $pdfFormatProvider; + private array $availableOrientations; - private PdfOrientationProviderInterface $pdfOrientationProvider; + private array $availablePageSizes; /** * @psalm-param array $validationGroups */ public function __construct( - PdfPageSizeProviderInterface $pdfFormatProvider, - PdfOrientationProviderInterface $pdfOrientationProvider, + array $availableOrientations, + array $availablePageSizes, string $dataClass, - array $validationGroups + array $validationGroups = [] ) { - parent::__construct($dataClass, $validationGroups); + $this->availableOrientations = $availableOrientations; + $this->availablePageSizes = $availablePageSizes; - $this->pdfFormatProvider = $pdfFormatProvider; - $this->pdfOrientationProvider = $pdfOrientationProvider; + parent::__construct($dataClass, $validationGroups); } public function buildForm(FormBuilderInterface $builder, array $options): void @@ -65,17 +63,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.default_validity_period', ]); $builder->add('pageSize', ChoiceType::class, [ - 'choices' => $this->pdfFormatProvider->getAvailablePageSizes(), + 'choices' => $this->availablePageSizes, 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.page_size', 'choice_translation_domain' => false, 'choice_label' => function (string $value) { return $value; }, - 'placeholder' => PdfPageSizeProviderInterface::FORMAT_A5, - 'empty_data' => PdfPageSizeProviderInterface::FORMAT_A5, ]); $builder->add('orientation', ChoiceType::class, [ - 'choices' => $this->pdfOrientationProvider->getAvailableOrientations(), + 'choices' => $this->availableOrientations, 'label' => 'setono_sylius_gift_card.form.gift_card_configuration.orientation', 'choice_translation_domain' => false, 'choice_label' => function (string $value) { diff --git a/src/Model/GiftCardConfiguration.php b/src/Model/GiftCardConfiguration.php index d516946b..0f949f84 100644 --- a/src/Model/GiftCardConfiguration.php +++ b/src/Model/GiftCardConfiguration.php @@ -6,8 +6,6 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; -use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface; -use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface; use Sylius\Component\Core\Model\ImageInterface; use Sylius\Component\Resource\Model\TimestampableTrait; use Sylius\Component\Resource\Model\ToggleableTrait; @@ -40,9 +38,9 @@ class GiftCardConfiguration implements GiftCardConfigurationInterface protected ?string $defaultValidityPeriod = null; - protected ?string $pageSize = PdfPageSizeProviderInterface::FORMAT_A5; + protected ?string $pageSize = null; - protected ?string $orientation = PdfOrientationProviderInterface::ORIENTATION_PORTRAIT; + protected ?string $orientation = null; public function __construct() { diff --git a/src/Provider/PdfOrientationProvider.php b/src/Provider/PdfOrientationProvider.php deleted file mode 100644 index caa5761c..00000000 --- a/src/Provider/PdfOrientationProvider.php +++ /dev/null @@ -1,16 +0,0 @@ - + %setono_sylius_gift_card.pdf_rendering.default_orientation% + %setono_sylius_gift_card.pdf_rendering.default_page_size% diff --git a/src/Resources/config/services/form.xml b/src/Resources/config/services/form.xml index c2e6b6fb..d20d7bf1 100644 --- a/src/Resources/config/services/form.xml +++ b/src/Resources/config/services/form.xml @@ -79,8 +79,9 @@ - - + + %setono_sylius_gift_card.pdf_rendering.available_orientations% + %setono_sylius_gift_card.pdf_rendering.available_page_sizes% %setono_sylius_gift_card.model.gift_card_configuration.class% %setono_sylius_gift_card.form.type.gift_card_configuration.validation_groups% diff --git a/src/Resources/config/services/provider.xml b/src/Resources/config/services/provider.xml index 3c6b945a..3996d07e 100644 --- a/src/Resources/config/services/provider.xml +++ b/src/Resources/config/services/provider.xml @@ -24,16 +24,6 @@ - - - - - - + + + %setono_sylius_gift_card.pdf_rendering.available_orientations% + + + + + + %setono_sylius_gift_card.pdf_rendering.available_page_sizes% + + + diff --git a/src/Resources/config/validation/GiftCardConfiguration.xml b/src/Resources/config/validation/GiftCardConfiguration.xml index c34a0f6a..7d830fc8 100644 --- a/src/Resources/config/validation/GiftCardConfiguration.xml +++ b/src/Resources/config/validation/GiftCardConfiguration.xml @@ -39,5 +39,15 @@ + + + + + + + + + + diff --git a/src/Validator/Constraints/Pdf/ValidOrientation.php b/src/Validator/Constraints/Pdf/ValidOrientation.php new file mode 100644 index 00000000..eef64ec2 --- /dev/null +++ b/src/Validator/Constraints/Pdf/ValidOrientation.php @@ -0,0 +1,12 @@ +availableOrientations = $availableOrientations; + } + + public function validate($value, Constraint $constraint): void + { + if (!$constraint instanceof ValidOrientation) { + throw new UnexpectedTypeException($constraint, ValidOrientation::class); + } + + if (null === $value) { + return; + } + + if (!\in_array($value, $this->availableOrientations, true)) { + $this->context->buildViolation($constraint->message) + ->addViolation(); + } + } +} diff --git a/src/Validator/Constraints/Pdf/ValidPageSize.php b/src/Validator/Constraints/Pdf/ValidPageSize.php new file mode 100644 index 00000000..1a086d59 --- /dev/null +++ b/src/Validator/Constraints/Pdf/ValidPageSize.php @@ -0,0 +1,12 @@ +availablePageSizes = $availableOrientations; + } + + public function validate($value, Constraint $constraint): void + { + if (!$constraint instanceof ValidPageSize) { + throw new UnexpectedTypeException($constraint, ValidPageSize::class); + } + + if (null === $value) { + return; + } + + if (!\in_array($value, $this->availablePageSizes, true)) { + $this->context->buildViolation($constraint->message) + ->addViolation(); + } + } +} diff --git a/tests/Application/config/packages/setono_sylius_gift_card.yaml b/tests/Application/config/packages/setono_sylius_gift_card.yaml deleted file mode 100644 index ccb4949d..00000000 --- a/tests/Application/config/packages/setono_sylius_gift_card.yaml +++ /dev/null @@ -1,3 +0,0 @@ -imports: - - { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/config.yaml" } - - { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/fixtures.yaml" } diff --git a/tests/Application/config/packages/test/setono_sylius_gift_card.yaml b/tests/Application/config/packages/test/setono_sylius_gift_card.yaml new file mode 100644 index 00000000..9a091d6c --- /dev/null +++ b/tests/Application/config/packages/test/setono_sylius_gift_card.yaml @@ -0,0 +1,9 @@ +setono_sylius_gift_card: + pdf_rendering: + default_orientation: 'Landscape' + available_orientations: + - Landscape + default_page_size: 'B0' + available_page_sizes: + - A5 + - B0 diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index d3aa3981..40b20d79 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -5,7 +5,6 @@ namespace Tests\Setono\SyliusGiftCardPlugin\Unit\DependencyInjection; use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; -use PHPUnit\Framework\TestCase; use Setono\SyliusGiftCardPlugin\DependencyInjection\Configuration; use Setono\SyliusGiftCardPlugin\Doctrine\ORM\GiftCardRepository; use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardChannelConfigurationType; @@ -24,8 +23,9 @@ use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; use Sylius\Component\Resource\Factory\Factory; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -final class ConfigurationTest extends TestCase +final class ConfigurationTest extends KernelTestCase { use ConfigurationTestCaseTrait; @@ -43,6 +43,46 @@ public function processed_configuration_for_array_node_1(): void 'setono_sylius_gift_card' => [], ], [ 'code_length' => 20, + 'pdf_rendering' => [ + 'default_orientation' => 'Portrait', + 'available_orientations' => [ + 'Portrait', + 'Landscape', + ], + 'default_page_size' => 'A4', + 'available_page_sizes' => [ + 'A0', + 'A1', + 'A2', + 'A3', + 'A4', + 'A5', + 'A6', + 'A7', + 'A8', + 'A9', + 'B0', + 'B1', + 'B2', + 'B3', + 'B4', + 'B5', + 'B6', + 'B7', + 'B8', + 'B9', + 'B10', + 'C5E', + 'Comm10E', + 'DLE', + 'Executive', + 'Folio', + 'Ledger', + 'Legal', + 'Letter', + 'Tabloid', + ] + ], 'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM, 'resources' => [ 'gift_card' => [ @@ -88,4 +128,23 @@ public function processed_configuration_for_array_node_1(): void ], ]); } + + /** + * @test + */ + public function it_overrides_default_configuration(): void + { + self::bootKernel(); + $container = self::$container; + + self::assertEquals( + $container->getParameter('setono_sylius_gift_card.pdf_rendering.available_page_sizes'), + ['A5', 'B0'] + ); + + self::assertEquals( + $container->getParameter('setono_sylius_gift_card.pdf_rendering.available_orientations'), + ['Landscape'] + ); + } } diff --git a/tests/Unit/Provider/PdfOrientationProviderTest.php b/tests/Unit/Provider/PdfOrientationProviderTest.php deleted file mode 100644 index 144fcdb7..00000000 --- a/tests/Unit/Provider/PdfOrientationProviderTest.php +++ /dev/null @@ -1,21 +0,0 @@ -assertIsArray($provider->getAvailableOrientations()); - } -} diff --git a/tests/Unit/Provider/PdfPageSizeProviderTest.php b/tests/Unit/Provider/PdfPageSizeProviderTest.php deleted file mode 100644 index af05e798..00000000 --- a/tests/Unit/Provider/PdfPageSizeProviderTest.php +++ /dev/null @@ -1,21 +0,0 @@ -assertIsArray($provider->getAvailablePageSizes()); - } -} diff --git a/tests/Unit/Provider/PdfRenderingOptionProviderTest.php b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php index 02a79287..c6148e6c 100644 --- a/tests/Unit/Provider/PdfRenderingOptionProviderTest.php +++ b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php @@ -6,8 +6,6 @@ use PHPUnit\Framework\TestCase; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration; -use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface; -use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface; use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProvider; final class PdfRenderingOptionProviderTest extends TestCase @@ -31,11 +29,11 @@ public function it_provides_page_size_if_not_null(): void { $provider = new PdfRenderingOptionProvider(); $giftCardConfiguration = new GiftCardConfiguration(); - $giftCardConfiguration->setPageSize(PdfPageSizeProviderInterface::FORMAT_A8); + $giftCardConfiguration->setPageSize('A8'); $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); $this->assertArrayHasKey('page-size', $renderingOptions); - $this->assertEquals(PdfPageSizeProviderInterface::FORMAT_A8, $renderingOptions['page-size']); + $this->assertEquals('A8', $renderingOptions['page-size']); } /** @@ -45,10 +43,10 @@ public function it_provides_orientation_if_not_null(): void { $provider = new PdfRenderingOptionProvider(); $giftCardConfiguration = new GiftCardConfiguration(); - $giftCardConfiguration->setOrientation(PdfOrientationProviderInterface::ORIENTATION_LANDSCAPE); + $giftCardConfiguration->setOrientation('Landscape'); $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); $this->assertArrayHasKey('orientation', $renderingOptions); - $this->assertEquals(PdfOrientationProviderInterface::ORIENTATION_LANDSCAPE, $renderingOptions['orientation']); + $this->assertEquals('Landscape', $renderingOptions['orientation']); } } diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php new file mode 100644 index 00000000..b046707a --- /dev/null +++ b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php @@ -0,0 +1,47 @@ +expectExceptionObject(new UnexpectedTypeException($constraint, ValidOrientation::class)); + $validator->validate('super', $constraint); + } + + /** + * @test + */ + public function it_adds_violation_if_orientation_is_invalid(): void + { + $validator = new ValidOrientationValidator(['Portrait']); + $constraint = new ValidOrientation(); + + $executionContext = $this->prophesize(ExecutionContextInterface::class); + $constraintViolationBuilder = $this->prophesize(ConstraintViolationBuilderInterface::class); + + $executionContext->buildViolation($constraint->message)->willReturn($constraintViolationBuilder); + $constraintViolationBuilder->addViolation()->shouldBeCalled(); + + $validator->initialize($executionContext->reveal()); + $validator->validate('Invalid orientation', $constraint); + } +} diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php new file mode 100644 index 00000000..408f9b40 --- /dev/null +++ b/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php @@ -0,0 +1,47 @@ +expectExceptionObject(new UnexpectedTypeException($constraint, ValidPageSize::class)); + $validator->validate('super', $constraint); + } + + /** + * @test + */ + public function it_adds_violation_if_orientation_is_invalid(): void + { + $validator = new ValidPageSizeValidator(['A1']); + $constraint = new ValidPageSize(); + + $executionContext = $this->prophesize(ExecutionContextInterface::class); + $constraintViolationBuilder = $this->prophesize(ConstraintViolationBuilderInterface::class); + + $executionContext->buildViolation($constraint->message)->willReturn($constraintViolationBuilder); + $constraintViolationBuilder->addViolation()->shouldBeCalled(); + + $validator->initialize($executionContext->reveal()); + $validator->validate('A0', $constraint); + } +} From 0982447b4d3e33b4511535466b940e2f321a3112 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Wed, 26 Jan 2022 13:28:05 +0100 Subject: [PATCH 4/9] Fix ECS and add back the missing configuration file --- .../Application/config/packages/setono_sylius_gift_card.yaml | 3 +++ tests/Unit/DependencyInjection/ConfigurationTest.php | 2 +- .../Constraints/Pdf/ValidOrientationValidatorTest.php | 4 +++- .../Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/Application/config/packages/setono_sylius_gift_card.yaml diff --git a/tests/Application/config/packages/setono_sylius_gift_card.yaml b/tests/Application/config/packages/setono_sylius_gift_card.yaml new file mode 100644 index 00000000..ccb4949d --- /dev/null +++ b/tests/Application/config/packages/setono_sylius_gift_card.yaml @@ -0,0 +1,3 @@ +imports: + - { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/config.yaml" } + - { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/fixtures.yaml" } diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 40b20d79..3a3a43d1 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -81,7 +81,7 @@ public function processed_configuration_for_array_node_1(): void 'Legal', 'Letter', 'Tabloid', - ] + ], ], 'driver' => SyliusResourceBundle::DRIVER_DOCTRINE_ORM, 'resources' => [ diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php index b046707a..b4a6b6e7 100644 --- a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php +++ b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php @@ -1,11 +1,13 @@ Date: Wed, 26 Jan 2022 13:37:13 +0100 Subject: [PATCH 5/9] Add test on configuration factory --- .../GiftCardConfigurationFactoryTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/Unit/Factory/GiftCardConfigurationFactoryTest.php diff --git a/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php b/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php new file mode 100644 index 00000000..09fdef69 --- /dev/null +++ b/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php @@ -0,0 +1,45 @@ +prophesize(FactoryInterface::class); + $imageFactory = $this->prophesize(FactoryInterface::class); + $defaultOrientation = 'Portrait'; + $defaultPageSize = 'A8'; + + $decoratedFactory->createNew()->willReturn($giftCardConfiguration); + $imageFactory->createNew()->willReturn(new GiftCardConfigurationImage()); + + $factory = new GiftCardConfigurationFactory( + $decoratedFactory->reveal(), + $imageFactory->reveal(), + $defaultOrientation, + $defaultPageSize + ); + $createdGiftCardConfiguration = $factory->createNew(); + + $this->assertSame($giftCardConfiguration, $createdGiftCardConfiguration); + $this->assertSame($defaultOrientation, $createdGiftCardConfiguration->getOrientation()); + $this->assertSame($defaultPageSize, $createdGiftCardConfiguration->getPageSize()); + } +} From ad972009e6d065138dc784af8b862a275c7d6776 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Thu, 10 Mar 2022 08:12:06 +0100 Subject: [PATCH 6/9] Use constants instead of hardcoded values --- src/DependencyInjection/Configuration.php | 47 ++------ src/Generator/GiftCardPdfGenerator.php | 10 +- .../PdfRenderingOptionProviderInterface.php | 12 -- ...er.php => PdfRenderingOptionsProvider.php} | 2 +- .../PdfRenderingOptionsProviderInterface.php | 114 ++++++++++++++++++ src/Resources/config/services/provider.xml | 4 +- .../GiftCardConfigurationFactoryTest.php | 5 +- .../Generator/GiftCardPdfGeneratorTest.php | 8 +- .../PdfRenderingOptionProviderTest.php | 13 +- .../Pdf/ValidOrientationValidatorTest.php | 7 +- 10 files changed, 149 insertions(+), 73 deletions(-) delete mode 100644 src/Provider/PdfRenderingOptionProviderInterface.php rename src/Provider/{PdfRenderingOptionProvider.php => PdfRenderingOptionsProvider.php} (87%) create mode 100644 src/Provider/PdfRenderingOptionsProviderInterface.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5113f322..ced374a5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -17,6 +17,7 @@ use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Sylius\Bundle\ResourceBundle\Controller\ResourceController; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; @@ -40,49 +41,19 @@ public function getConfigTreeBuilder(): TreeBuilder ->arrayNode('pdf_rendering') ->addDefaultsIfNotSet() ->children() - ->scalarNode('default_orientation')->defaultValue('Portrait')->end() + ->scalarNode('default_orientation') + ->defaultValue(PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT) + ->end() ->arrayNode('available_orientations') ->scalarPrototype()->end() - ->defaultValue([ - 'Portrait', - 'Landscape', - ]) + ->defaultValue(PdfRenderingOptionsProviderInterface::AVAILABLE_ORIENTATIONS) + ->end() + ->scalarNode('default_page_size') + ->defaultValue(PdfRenderingOptionsProviderInterface::PAGE_SIZE_A4) ->end() - ->scalarNode('default_page_size')->defaultValue('A4')->end() ->arrayNode('available_page_sizes') ->scalarPrototype()->end() - ->defaultValue([ - 'A0', - 'A1', - 'A2', - 'A3', - 'A4', - 'A5', - 'A6', - 'A7', - 'A8', - 'A9', - 'B0', - 'B1', - 'B2', - 'B3', - 'B4', - 'B5', - 'B6', - 'B7', - 'B8', - 'B9', - 'B10', - 'C5E', - 'Comm10E', - 'DLE', - 'Executive', - 'Folio', - 'Ledger', - 'Legal', - 'Letter', - 'Tabloid', - ]) + ->defaultValue(PdfRenderingOptionsProviderInterface::AVAILABLE_PAGE_SIZES) ->end() ->end() ->end() diff --git a/src/Generator/GiftCardPdfGenerator.php b/src/Generator/GiftCardPdfGenerator.php index 4bf23e5f..ccde4cb9 100644 --- a/src/Generator/GiftCardPdfGenerator.php +++ b/src/Generator/GiftCardPdfGenerator.php @@ -8,7 +8,7 @@ use Knp\Snappy\GeneratorInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface; -use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProviderInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Twig\Environment; class GiftCardPdfGenerator implements GiftCardPdfGeneratorInterface @@ -17,16 +17,16 @@ class GiftCardPdfGenerator implements GiftCardPdfGeneratorInterface private GeneratorInterface $snappy; - private PdfRenderingOptionProviderInterface $renderingOptionProvider; + private PdfRenderingOptionsProviderInterface $renderingOptionsProvider; public function __construct( Environment $twig, GeneratorInterface $snappy, - PdfRenderingOptionProviderInterface $renderingOptionProvider + PdfRenderingOptionsProviderInterface $renderingOptionsProvider ) { $this->twig = $twig; $this->snappy = $snappy; - $this->renderingOptionProvider = $renderingOptionProvider; + $this->renderingOptionsProvider = $renderingOptionsProvider; } public function generatePdfResponse( @@ -38,7 +38,7 @@ public function generatePdfResponse( 'configuration' => $giftCardChannelConfiguration, ]); - $renderingOptions = $this->renderingOptionProvider->getRenderingOptions($giftCardChannelConfiguration); + $renderingOptions = $this->renderingOptionsProvider->getRenderingOptions($giftCardChannelConfiguration); return new PdfResponse($this->snappy->getOutputFromHtml($html, $renderingOptions), 'gift_card.pdf'); } diff --git a/src/Provider/PdfRenderingOptionProviderInterface.php b/src/Provider/PdfRenderingOptionProviderInterface.php deleted file mode 100644 index 8d3b45a2..00000000 --- a/src/Provider/PdfRenderingOptionProviderInterface.php +++ /dev/null @@ -1,12 +0,0 @@ - - + diff --git a/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php b/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php index 09fdef69..fe6bcd68 100644 --- a/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php +++ b/tests/Unit/Factory/GiftCardConfigurationFactoryTest.php @@ -9,6 +9,7 @@ use Setono\SyliusGiftCardPlugin\Factory\GiftCardConfigurationFactory; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImage; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Sylius\Component\Resource\Factory\FactoryInterface; final class GiftCardConfigurationFactoryTest extends TestCase @@ -24,8 +25,8 @@ public function it_creates_a_new_gift_card_configuration(): void $decoratedFactory = $this->prophesize(FactoryInterface::class); $imageFactory = $this->prophesize(FactoryInterface::class); - $defaultOrientation = 'Portrait'; - $defaultPageSize = 'A8'; + $defaultOrientation = PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT; + $defaultPageSize = PdfRenderingOptionsProviderInterface::PAGE_SIZE_A8; $decoratedFactory->createNew()->willReturn($giftCardConfiguration); $imageFactory->createNew()->willReturn(new GiftCardConfigurationImage()); diff --git a/tests/Unit/Generator/GiftCardPdfGeneratorTest.php b/tests/Unit/Generator/GiftCardPdfGeneratorTest.php index 6f14283a..150d3863 100644 --- a/tests/Unit/Generator/GiftCardPdfGeneratorTest.php +++ b/tests/Unit/Generator/GiftCardPdfGeneratorTest.php @@ -10,7 +10,7 @@ use Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGenerator; use Setono\SyliusGiftCardPlugin\Model\GiftCard; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration; -use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProviderInterface; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Twig\Environment; final class GiftCardPdfGeneratorTest extends TestCase @@ -27,9 +27,9 @@ public function it_generates_pdf_response_for_gift_card(): void $twig = $this->prophesize(Environment::class); $snappy = $this->prophesize(Pdf::class); - $renderingOptionProvider = $this->prophesize(PdfRenderingOptionProviderInterface::class); + $renderingOptionsProvider = $this->prophesize(PdfRenderingOptionsProviderInterface::class); - $renderingOptionProvider->getRenderingOptions($giftCardChannelConfiguration)->willReturn([]); + $renderingOptionsProvider->getRenderingOptions($giftCardChannelConfiguration)->willReturn([]); $twig->render('@SetonoSyliusGiftCardPlugin/Shop/GiftCard/pdf.html.twig', [ 'giftCard' => $giftCard, 'configuration' => $giftCardChannelConfiguration, @@ -39,7 +39,7 @@ public function it_generates_pdf_response_for_gift_card(): void $giftCardPdfGenerator = new GiftCardPdfGenerator( $twig->reveal(), $snappy->reveal(), - $renderingOptionProvider->reveal() + $renderingOptionsProvider->reveal() ); $response = $giftCardPdfGenerator->generatePdfResponse($giftCard, $giftCardChannelConfiguration); diff --git a/tests/Unit/Provider/PdfRenderingOptionProviderTest.php b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php index c6148e6c..a4a1804c 100644 --- a/tests/Unit/Provider/PdfRenderingOptionProviderTest.php +++ b/tests/Unit/Provider/PdfRenderingOptionProviderTest.php @@ -6,7 +6,8 @@ use PHPUnit\Framework\TestCase; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration; -use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionProvider; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProvider; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; final class PdfRenderingOptionProviderTest extends TestCase { @@ -15,7 +16,7 @@ final class PdfRenderingOptionProviderTest extends TestCase */ public function it_provides_rendering_options(): void { - $provider = new PdfRenderingOptionProvider(); + $provider = new PdfRenderingOptionsProvider(); $giftCardConfiguration = new GiftCardConfiguration(); $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); @@ -27,13 +28,13 @@ public function it_provides_rendering_options(): void */ public function it_provides_page_size_if_not_null(): void { - $provider = new PdfRenderingOptionProvider(); + $provider = new PdfRenderingOptionsProvider(); $giftCardConfiguration = new GiftCardConfiguration(); - $giftCardConfiguration->setPageSize('A8'); + $giftCardConfiguration->setPageSize(PdfRenderingOptionsProviderInterface::PAGE_SIZE_A8); $renderingOptions = $provider->getRenderingOptions($giftCardConfiguration); $this->assertArrayHasKey('page-size', $renderingOptions); - $this->assertEquals('A8', $renderingOptions['page-size']); + $this->assertEquals(PdfRenderingOptionsProviderInterface::PAGE_SIZE_A8, $renderingOptions['page-size']); } /** @@ -41,7 +42,7 @@ public function it_provides_page_size_if_not_null(): void */ public function it_provides_orientation_if_not_null(): void { - $provider = new PdfRenderingOptionProvider(); + $provider = new PdfRenderingOptionsProvider(); $giftCardConfiguration = new GiftCardConfiguration(); $giftCardConfiguration->setOrientation('Landscape'); diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php index b4a6b6e7..40214fac 100644 --- a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php +++ b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Setono\SyliusGiftCardPlugin\Validator\Constraints\Pdf\ValidOrientation; use Setono\SyliusGiftCardPlugin\Validator\Constraints\Pdf\ValidOrientationValidator; use Symfony\Component\Validator\Constraints\NotBlank; @@ -22,11 +23,11 @@ class ValidOrientationValidatorTest extends TestCase */ public function it_throws_exception_if_constraint_has_wrong_type() { - $validator = new ValidOrientationValidator(['Portrait']); + $validator = new ValidOrientationValidator([PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT]); $constraint = new NotBlank(); $this->expectExceptionObject(new UnexpectedTypeException($constraint, ValidOrientation::class)); - $validator->validate('super', $constraint); + $validator->validate('Any orientation', $constraint); } /** @@ -34,7 +35,7 @@ public function it_throws_exception_if_constraint_has_wrong_type() */ public function it_adds_violation_if_orientation_is_invalid(): void { - $validator = new ValidOrientationValidator(['Portrait']); + $validator = new ValidOrientationValidator([PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT]); $constraint = new ValidOrientation(); $executionContext = $this->prophesize(ExecutionContextInterface::class); From ac8712f3f0e459b84bd694ee05d756557bdae38d Mon Sep 17 00:00:00 2001 From: Roshyo Date: Thu, 10 Mar 2022 08:15:40 +0100 Subject: [PATCH 7/9] Add more Unit tests --- .../Pdf/ValidOrientationValidatorTest.php | 17 ++++++++++++- .../Pdf/ValidPageSizeValidatorTest.php | 25 +++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php index 40214fac..624f26bd 100644 --- a/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php +++ b/tests/Unit/Validator/Constraints/Pdf/ValidOrientationValidatorTest.php @@ -21,7 +21,7 @@ class ValidOrientationValidatorTest extends TestCase /** * @test */ - public function it_throws_exception_if_constraint_has_wrong_type() + public function it_throws_exception_if_constraint_has_wrong_type(): void { $validator = new ValidOrientationValidator([PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT]); $constraint = new NotBlank(); @@ -30,6 +30,21 @@ public function it_throws_exception_if_constraint_has_wrong_type() $validator->validate('Any orientation', $constraint); } + /** + * @test + */ + public function it_does_nothing_if_value_is_null(): void + { + $validator = new ValidOrientationValidator([PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT]); + $constraint = new ValidOrientation(); + + $constraintViolationBuilder = $this->prophesize(ConstraintViolationBuilderInterface::class); + + $constraintViolationBuilder->addViolation()->shouldNotBeCalled(); + + $validator->validate(null, $constraint); + } + /** * @test */ diff --git a/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php b/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php index cf02ec79..b2bd67e6 100644 --- a/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php +++ b/tests/Unit/Validator/Constraints/Pdf/ValidPageSizeValidatorTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Setono\SyliusGiftCardPlugin\Validator\Constraints\Pdf\ValidPageSize; use Setono\SyliusGiftCardPlugin\Validator\Constraints\Pdf\ValidPageSizeValidator; use Symfony\Component\Validator\Constraints\NotBlank; @@ -20,13 +21,27 @@ class ValidPageSizeValidatorTest extends TestCase /** * @test */ - public function it_throws_exception_if_constraint_has_wrong_type() + public function it_throws_exception_if_constraint_has_wrong_type(): void { - $validator = new ValidPageSizeValidator(['A0']); + $validator = new ValidPageSizeValidator([PdfRenderingOptionsProviderInterface::PAGE_SIZE_A0]); $constraint = new NotBlank(); $this->expectExceptionObject(new UnexpectedTypeException($constraint, ValidPageSize::class)); - $validator->validate('super', $constraint); + $validator->validate('Any value', $constraint); + } + + /** + * @test + */ + public function it_does_nothing_if_value_is_null(): void + { + $validator = new ValidPageSizeValidator([PdfRenderingOptionsProviderInterface::PAGE_SIZE_A0]); + $constraint = new ValidPageSize(); + + $constraintViolationBuilder = $this->prophesize(ConstraintViolationBuilderInterface::class); + + $constraintViolationBuilder->addViolation()->shouldNotBeCalled(); + $validator->validate(null, $constraint); } /** @@ -34,7 +49,7 @@ public function it_throws_exception_if_constraint_has_wrong_type() */ public function it_adds_violation_if_orientation_is_invalid(): void { - $validator = new ValidPageSizeValidator(['A1']); + $validator = new ValidPageSizeValidator([PdfRenderingOptionsProviderInterface::PAGE_SIZE_A1]); $constraint = new ValidPageSize(); $executionContext = $this->prophesize(ExecutionContextInterface::class); @@ -44,6 +59,6 @@ public function it_adds_violation_if_orientation_is_invalid(): void $constraintViolationBuilder->addViolation()->shouldBeCalled(); $validator->initialize($executionContext->reveal()); - $validator->validate('A0', $constraint); + $validator->validate(PdfRenderingOptionsProviderInterface::PAGE_SIZE_A0, $constraint); } } From ee84d168f6cf83258de8a8f8ae07094f9e8a66ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Fri, 18 Mar 2022 11:11:12 +0100 Subject: [PATCH 8/9] Fixing review comments --- psalm.xml | 2 - src/DependencyInjection/Configuration.php | 13 ++++--- .../SetonoSyliusGiftCardExtension.php | 10 +++-- src/Form/Type/GiftCardConfigurationType.php | 6 ++- src/Generator/GiftCardCodeGenerator.php | 7 ++++ .../DependencyInjection/ConfigurationTest.php | 23 +---------- .../SetonoSyliusGiftCardExtensionTest.php | 38 ++++++++++++++++++- 7 files changed, 65 insertions(+), 34 deletions(-) diff --git a/psalm.xml b/psalm.xml index aa550cb0..e136d8f8 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,7 +1,6 @@ - diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ced374a5..d6ffb1b5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -35,6 +35,9 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode = $treeBuilder->getRootNode(); + /** + * @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod + */ $rootNode ->addDefaultsIfNotSet() ->children() @@ -84,16 +87,11 @@ private function addResourcesSection(ArrayNodeDefinition $node): void $this->addGiftCardConfigurationSection($resourcesNode); $this->addGiftCardConfigurationImageSection($resourcesNode); $this->addChannelConfigurationSection($resourcesNode); - - $resourcesNode - ->end() - ->end() - ->end() - ; } private function addGiftCardSection(NodeBuilder $nodeBuilder): void { + /** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */ $nodeBuilder ->arrayNode('gift_card') ->addDefaultsIfNotSet() @@ -113,6 +111,7 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void { + /** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */ $nodeBuilder ->arrayNode('gift_card_configuration') ->addDefaultsIfNotSet() @@ -132,6 +131,7 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder): void { + /** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */ $nodeBuilder ->arrayNode('gift_card_configuration_image') ->addDefaultsIfNotSet() @@ -151,6 +151,7 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder): private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void { + /** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */ $nodeBuilder ->arrayNode('gift_card_channel_configuration') ->addDefaultsIfNotSet() diff --git a/src/DependencyInjection/SetonoSyliusGiftCardExtension.php b/src/DependencyInjection/SetonoSyliusGiftCardExtension.php index 6cf5bce2..5be8a8df 100644 --- a/src/DependencyInjection/SetonoSyliusGiftCardExtension.php +++ b/src/DependencyInjection/SetonoSyliusGiftCardExtension.php @@ -18,9 +18,9 @@ public function load(array $configs, ContainerBuilder $container): void * @var array{ * pdf_rendering: array{ * default_orientation: string, - * available_orientations: array, + * available_orientations: list, * default_page_size: string, - * available_page_sizes: array, + * available_page_sizes: list, * }, * code_length: int, * driver: string, @@ -54,7 +54,11 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services.xml'); if ($container->hasParameter('kernel.bundles')) { - /** @var array $bundles */ + /** + * @var array $bundles + * + * @psalm-suppress UndefinedDocblockClass + */ $bundles = $container->getParameter('kernel.bundles'); if (array_key_exists('SetonoSyliusCatalogPromotionPlugin', $bundles)) { $loader->load('services/conditional/catalog_promotion_rule.xml'); diff --git a/src/Form/Type/GiftCardConfigurationType.php b/src/Form/Type/GiftCardConfigurationType.php index adc2da9b..89ee978b 100644 --- a/src/Form/Type/GiftCardConfigurationType.php +++ b/src/Form/Type/GiftCardConfigurationType.php @@ -14,12 +14,16 @@ final class GiftCardConfigurationType extends AbstractResourceType { + /** @var list */ private array $availableOrientations; + /** @var list */ private array $availablePageSizes; /** - * @psalm-param array $validationGroups + * @param list $availableOrientations + * @param list $availablePageSizes + * @param list $validationGroups */ public function __construct( array $availableOrientations, diff --git a/src/Generator/GiftCardCodeGenerator.php b/src/Generator/GiftCardCodeGenerator.php index e973eb03..eb9de287 100644 --- a/src/Generator/GiftCardCodeGenerator.php +++ b/src/Generator/GiftCardCodeGenerator.php @@ -6,15 +6,22 @@ use function preg_replace; use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface; +use Webmozart\Assert\Assert; final class GiftCardCodeGenerator implements GiftCardCodeGeneratorInterface { private GiftCardRepositoryInterface $giftCardRepository; + /** @var positive-int */ private int $codeLength; + /** + * @param positive-int $codeLength + */ public function __construct(GiftCardRepositoryInterface $giftCardRepository, int $codeLength) { + Assert::greaterThan($codeLength, 0); + $this->giftCardRepository = $giftCardRepository; $this->codeLength = $codeLength; } diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 3a3a43d1..df740200 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -5,6 +5,7 @@ namespace Tests\Setono\SyliusGiftCardPlugin\Unit\DependencyInjection; use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; +use PHPUnit\Framework\TestCase; use Setono\SyliusGiftCardPlugin\DependencyInjection\Configuration; use Setono\SyliusGiftCardPlugin\Doctrine\ORM\GiftCardRepository; use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardChannelConfigurationType; @@ -23,9 +24,8 @@ use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; use Sylius\Component\Resource\Factory\Factory; -use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; -final class ConfigurationTest extends KernelTestCase +final class ConfigurationTest extends TestCase { use ConfigurationTestCaseTrait; @@ -128,23 +128,4 @@ public function processed_configuration_for_array_node_1(): void ], ]); } - - /** - * @test - */ - public function it_overrides_default_configuration(): void - { - self::bootKernel(); - $container = self::$container; - - self::assertEquals( - $container->getParameter('setono_sylius_gift_card.pdf_rendering.available_page_sizes'), - ['A5', 'B0'] - ); - - self::assertEquals( - $container->getParameter('setono_sylius_gift_card.pdf_rendering.available_orientations'), - ['Landscape'] - ); - } } diff --git a/tests/Unit/DependencyInjection/SetonoSyliusGiftCardExtensionTest.php b/tests/Unit/DependencyInjection/SetonoSyliusGiftCardExtensionTest.php index ff79a75e..daa29eeb 100644 --- a/tests/Unit/DependencyInjection/SetonoSyliusGiftCardExtensionTest.php +++ b/tests/Unit/DependencyInjection/SetonoSyliusGiftCardExtensionTest.php @@ -6,6 +6,7 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Setono\SyliusGiftCardPlugin\DependencyInjection\SetonoSyliusGiftCardExtension; +use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; final class SetonoSyliusGiftCardExtensionTest extends AbstractExtensionTestCase @@ -22,7 +23,42 @@ public function after_loading_the_correct_parameter_has_been_set(): void { $this->load(); - $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.code_length', 20); $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.driver', SyliusResourceBundle::DRIVER_DOCTRINE_ORM); + $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.code_length', 20); + $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.pdf_rendering.default_orientation', 'Portrait'); + $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.pdf_rendering.available_orientations', ['Portrait', 'Landscape']); + $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.pdf_rendering.default_page_size', 'A4'); + $this->assertContainerBuilderHasParameter('setono_sylius_gift_card.pdf_rendering.available_page_sizes', [ + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A0, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A1, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A2, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A3, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A4, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A5, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A6, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A7, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A8, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_A9, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B0, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B1, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B2, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B3, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B4, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B5, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B6, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B7, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B8, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B9, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_B10, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_C5E, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_COMM_10_E, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_DLE, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_EXECUTIVE, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_FOLIO, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_LEDGER, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_LEGAL, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_LETTER, + PdfRenderingOptionsProviderInterface::PAGE_SIZE_TABLOID, + ]); } } From 8776ccb7ebb2b2d7a719877d7d0dfe3547ccf036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Fri, 18 Mar 2022 11:21:16 +0100 Subject: [PATCH 9/9] Fix validator review comments --- src/Validator/Constraints/Pdf/ValidOrientationValidator.php | 2 +- src/Validator/Constraints/Pdf/ValidPageSizeValidator.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/Constraints/Pdf/ValidOrientationValidator.php b/src/Validator/Constraints/Pdf/ValidOrientationValidator.php index 9a21edbd..2f6d3f91 100644 --- a/src/Validator/Constraints/Pdf/ValidOrientationValidator.php +++ b/src/Validator/Constraints/Pdf/ValidOrientationValidator.php @@ -23,7 +23,7 @@ public function validate($value, Constraint $constraint): void throw new UnexpectedTypeException($constraint, ValidOrientation::class); } - if (null === $value) { + if (null === $value || '' === $value) { return; } diff --git a/src/Validator/Constraints/Pdf/ValidPageSizeValidator.php b/src/Validator/Constraints/Pdf/ValidPageSizeValidator.php index 57a09bdb..9b70886d 100644 --- a/src/Validator/Constraints/Pdf/ValidPageSizeValidator.php +++ b/src/Validator/Constraints/Pdf/ValidPageSizeValidator.php @@ -23,7 +23,7 @@ public function validate($value, Constraint $constraint): void throw new UnexpectedTypeException($constraint, ValidPageSize::class); } - if (null === $value) { + if (null === $value || '' === $value) { return; }