From 6a29efa1f0c9de01e5fe006b1635b5e2bd4bb566 Mon Sep 17 00:00:00 2001 From: Claudio Zizza Date: Wed, 3 Jul 2019 14:44:40 +0200 Subject: [PATCH] Fix normalization to save select typed attribute options Because select typed attributes had an validation error on save, the custom AttributeOption object had to introduce a check in the TableAttribute normalizer. --- CHANGELOG-3.0.md | 8 +++++ .../Entity/AttributeOptionSpec.php | 29 +++++++++++++++++++ .../AttributeOptionNormalizerSpec.php | 28 +++++++++++++++++- src/Entity/AttributeOption.php | 6 ++++ src/Normalizer/AttributeOptionNormalizer.php | 10 +++++-- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 spec/Flagbit/Bundle/TableAttributeBundle/Entity/AttributeOptionSpec.php diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index 888fa94..cf1aa06 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -1,3 +1,9 @@ +# 3.0.1 + +## Bug fixes + +- Fixes issue saving Akeneo AttributeOptions. [#29][pr29] + # 3.0.0 - Add support for Akeneo 3.0.0. @@ -5,3 +11,5 @@ ## BC breaks - `Flagbit\Bundle\TableAttributeBundle\Normalizer\StructuredAttributeOptionNormalizer` was deleted. `Flagbit\Bundle\TableAttributeBundle\Normalizer\AttributeOptionNormalizer` is now used for its service. + +[pr29]: https://github.com/flagbit/akeneo-table-attribute-bundle/pull/29 diff --git a/spec/Flagbit/Bundle/TableAttributeBundle/Entity/AttributeOptionSpec.php b/spec/Flagbit/Bundle/TableAttributeBundle/Entity/AttributeOptionSpec.php new file mode 100644 index 0000000..76cb526 --- /dev/null +++ b/spec/Flagbit/Bundle/TableAttributeBundle/Entity/AttributeOptionSpec.php @@ -0,0 +1,29 @@ +setAttribute($attribute); + $attribute->getType()->willReturn('foo'); + $this->isTableAttribute()->shouldReturn(false); + } + + function it_is_an_attribute_option_of_a_table_attribute(AttributeInterface $attribute) + { + $this->setAttribute($attribute); + $attribute->getType()->willReturn(TableType::FLAGBIT_CATALOG_TABLE); + $this->isTableAttribute()->shouldReturn(true); + } + + function it_is_an_attribute_option_without_an_attribute() + { + $this->isTableAttribute()->shouldReturn(false); + } +} diff --git a/spec/Flagbit/Bundle/TableAttributeBundle/Normalizer/AttributeOptionNormalizerSpec.php b/spec/Flagbit/Bundle/TableAttributeBundle/Normalizer/AttributeOptionNormalizerSpec.php index 90f448f..95cde0f 100644 --- a/spec/Flagbit/Bundle/TableAttributeBundle/Normalizer/AttributeOptionNormalizerSpec.php +++ b/spec/Flagbit/Bundle/TableAttributeBundle/Normalizer/AttributeOptionNormalizerSpec.php @@ -34,10 +34,13 @@ public function it_checks_return_type_array_and_right_values 'onlyActivatedLocales' => true, ]; - $baseNormalizer->normalize($attributeOption, 'array', $activatedLocales)->shouldBeCalled(); + $baseNormalizer->normalize($attributeOption, 'array', $activatedLocales) + ->willReturn([]) + ->shouldBeCalled(); $attributeOption->getType()->willReturn('text'); $attributeOption->getTypeConfig()->willReturn([]); $attributeOption->getConstraints()->willReturn($constraints); + $attributeOption->isTableAttribute()->willReturn(true); $normalizedValues = $this->normalize($attributeOption, 'array', $activatedLocales); @@ -51,4 +54,27 @@ public function it_checks_return_type_array_and_right_values $normalizedValues->shouldHaveKeyWithValue('type_config', []); $normalizedValues->shouldHaveKeyWithValue('constraints', $constraints); } + + public function it_checks_return_type_for_default_akeneo_attribute_options + ( + AttributeOption $attributeOption, + $baseNormalizer + ) + { + $baseNormalizer->normalize($attributeOption, 'array', []) + ->willReturn([]) + ->shouldBeCalled(); + $attributeOption->getType()->willReturn(null); + $attributeOption->getTypeConfig()->willReturn([]); + $attributeOption->getConstraints()->willReturn([]); + $attributeOption->isTableAttribute()->willReturn(false); + + $normalizedValues = $this->normalize($attributeOption, 'array'); + + $normalizedValues->shouldBeArray(); + + $normalizedValues->shouldNotHaveKey('type'); + $normalizedValues->shouldNotHaveKey('type_config'); + $normalizedValues->shouldNotHaveKey('constraints'); + } } diff --git a/src/Entity/AttributeOption.php b/src/Entity/AttributeOption.php index b08916f..e0dd738 100644 --- a/src/Entity/AttributeOption.php +++ b/src/Entity/AttributeOption.php @@ -3,6 +3,7 @@ namespace Flagbit\Bundle\TableAttributeBundle\Entity; use Akeneo\Pim\Structure\Component\Model\AttributeOption as BaseAttributeOption; +use Flagbit\Bundle\TableAttributeBundle\AttributeType\TableType; /** * @todo move Table specific columns into an own entity @@ -71,4 +72,9 @@ public function setTypeConfig($typeConfig) { $this->typeConfig = $typeConfig; } + + public function isTableAttribute() : bool + { + return null !== $this->attribute && TableType::FLAGBIT_CATALOG_TABLE === $this->attribute->getType(); + } } diff --git a/src/Normalizer/AttributeOptionNormalizer.php b/src/Normalizer/AttributeOptionNormalizer.php index 241133c..75950b5 100644 --- a/src/Normalizer/AttributeOptionNormalizer.php +++ b/src/Normalizer/AttributeOptionNormalizer.php @@ -2,6 +2,7 @@ namespace Flagbit\Bundle\TableAttributeBundle\Normalizer; +use Flagbit\Bundle\TableAttributeBundle\Entity\AttributeOption; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class AttributeOptionNormalizer implements NormalizerInterface @@ -21,9 +22,12 @@ public function normalize($object, $format = null, array $context = []) { $normalizedValues = $this->baseNormalizer->normalize($object, $format, $context); - $normalizedValues['type'] = $object->getType(); - $normalizedValues['type_config'] = $object->getTypeConfig(); - $normalizedValues['constraints'] = $object->getConstraints(); + /** @var AttributeOption $object */ + if ($object->isTableAttribute()) { + $normalizedValues['type'] = $object->getType(); + $normalizedValues['type_config'] = $object->getTypeConfig(); + $normalizedValues['constraints'] = $object->getConstraints(); + } return $normalizedValues; }