From a3114a81dcf634fd51264fc1f5cd40d0043224fb Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Tue, 13 Aug 2024 11:20:55 +0200 Subject: [PATCH] [API] Add ProductOptionValueNormalizer class --- .../Resources/config/services/serializers.xml | 5 ++ .../ProductOptionValueNormalizer.php | 56 ++++++++++++ .../ProductOptionValueNormalizerSpec.php | 90 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/Sylius/Bundle/ApiBundle/Serializer/ProductOptionValueNormalizer.php create mode 100644 src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductOptionValueNormalizerSpec.php diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/serializers.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/serializers.xml index 9d96016ed8..1aaf942dee 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/serializers.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/serializers.xml @@ -64,6 +64,11 @@ + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Serializer/ProductOptionValueNormalizer.php b/src/Sylius/Bundle/ApiBundle/Serializer/ProductOptionValueNormalizer.php new file mode 100644 index 0000000000..2d455e752b --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/Serializer/ProductOptionValueNormalizer.php @@ -0,0 +1,56 @@ +translatableEntityLocaleAssigner->assignLocale($object); + + return $this->normalizer->normalize($object, $format, $context); + } + + public function supportsNormalization($data, $format = null, $context = []): bool + { + if (isset($context[self::ALREADY_CALLED])) { + return false; + } + + return $data instanceof ProductOptionValueInterface; + } +} diff --git a/src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductOptionValueNormalizerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductOptionValueNormalizerSpec.php new file mode 100644 index 0000000000..5a00492600 --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductOptionValueNormalizerSpec.php @@ -0,0 +1,90 @@ +beConstructedWith($translatableEntityLocaleAssigner); + + $this->setNormalizer($normalizer); + } + + function it_is_an_aware_normalizer(): void + { + $this->shouldImplement(NormalizerAwareInterface::class); + } + + function it_supports_only_product_option_value_interface( + ProductOptionValueInterface $productOptionValue, + OrderInterface $order, + ): void { + $this->supportsNormalization($productOptionValue)->shouldReturn(true); + $this->supportsNormalization($order)->shouldReturn(false); + } + + function it_supports_the_normalizer_has_not_called_yet(ProductOptionValueInterface $productOptionValue): void + { + $this + ->supportsNormalization($productOptionValue, null, []) + ->shouldReturn(true) + ; + + $this + ->supportsNormalization($productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true]) + ->shouldReturn(false) + ; + } + + function it_assigns_locale_to_translatable_entity( + NormalizerInterface $normalizer, + ProductOptionValueInterface $productOptionValue, + TranslatableEntityLocaleAssignerInterface $translatableEntityLocaleAssigner, + ): void { + $normalizer + ->normalize($productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true]) + ->willReturn([]) + ; + + $translatableEntityLocaleAssigner->assignLocale($productOptionValue)->shouldBeCalled(); + + $this->normalize($productOptionValue, null, [])->shouldReturn([]); + } + + function it_throws_an_exception_if_the_given_object_is_not_a_product_option_value_interface(): void + { + $this + ->shouldThrow(\InvalidArgumentException::class) + ->during('normalize', [new \stdClass()]) + ; + } + + function it_throws_an_exception_if_the_normalizer_was_already_called(ProductOptionValueInterface $productOptionValue): void + { + $this + ->shouldThrow(\InvalidArgumentException::class) + ->during('normalize', [$productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true]]) + ; + } +}