Skip to content

Commit

Permalink
Fix normalization to save select typed attribute options
Browse files Browse the repository at this point in the history
Because select typed attributes had an validation error on save,
the custom AttributeOption object had to introduce a check
in the TableAttribute normalizer.
  • Loading branch information
flagbird committed Jul 4, 2019
1 parent db270ff commit 6a29efa
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# 3.0.1

## Bug fixes

- Fixes issue saving Akeneo AttributeOptions. [#29][pr29]

# 3.0.0

- Add support for Akeneo 3.0.0.

## 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace spec\Flagbit\Bundle\TableAttributeBundle\Entity;

use Akeneo\Pim\Structure\Component\Model\AttributeInterface;
use Flagbit\Bundle\TableAttributeBundle\AttributeType\TableType;
use PhpSpec\ObjectBehavior;

class AttributeOptionSpec extends ObjectBehavior
{
function it_is_an_attribute_option_of_a_different_attribute(AttributeInterface $attribute)
{
$this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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');
}
}
6 changes: 6 additions & 0 deletions src/Entity/AttributeOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
10 changes: 7 additions & 3 deletions src/Normalizer/AttributeOptionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down

0 comments on commit 6a29efa

Please sign in to comment.