Skip to content

Commit

Permalink
Merge pull request #31 from webgriffe/attribute-value-handler-handle-…
Browse files Browse the repository at this point in the history
…integer-type

AttributeValueHandler also handle integer type
  • Loading branch information
mmenozzi authored Jan 11, 2021
2 parents 2142509 + 6fa19e0 commit f8f472e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
44 changes: 42 additions & 2 deletions spec/ValueHandler/AttributeValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class AttributeValueHandlerSpec extends ObjectBehavior

private const TEXTAREA_ATTRIBUTE_CODE = 'causale';

private const INTEGER_ATTRIBUTE_CODE = 'integer';
private const INTEGER_ATTRIBUTE_CODE = 'position';

private const SELECT_ATTRIBUTE_CODE = 'select';

private const DATETIME_ATTRIBUTE_CODE = 'created_at';

private const NOT_EXISTING_ATTRIBUTE_CODE = 'not-existing';

private const PRODUCT_OPTION_CODE = 'finitura';
Expand All @@ -39,6 +41,7 @@ function let(
ProductAttributeInterface $textareaProductAttribute,
ProductAttributeInterface $integerProductAttribute,
ProductAttributeInterface $selectProductAttribute,
ProductAttributeInterface $datetimeProductAttribute,
ProductAttributeValueInterface $attributeValue,
RepositoryInterface $attributeRepository,
FactoryInterface $factory,
Expand All @@ -57,6 +60,7 @@ function let(
$textareaProductAttribute->getType()->willReturn('textarea');
$integerProductAttribute->getType()->willReturn('integer');
$selectProductAttribute->getType()->willReturn('select');
$datetimeProductAttribute->getType()->willReturn('datetime');
$attributeRepository->findOneBy(['code' => self::CHECKBOX_ATTRIBUTE_CODE])->willReturn($checkboxProductAttribute);
$attributeRepository->findOneBy(['code' => self::SELECT_ATTRIBUTE_CODE])->willReturn($selectProductAttribute);
$attributeRepository->findOneBy(['code' => self::TEXT_ATTRIBUTE_CODE])->willReturn($textProductAttribute);
Expand All @@ -65,6 +69,7 @@ function let(
);
$attributeRepository->findOneBy(['code' => self::PRODUCT_OPTION_CODE])->willReturn(null);
$attributeRepository->findOneBy(['code' => self::INTEGER_ATTRIBUTE_CODE])->willReturn($integerProductAttribute);
$attributeRepository->findOneBy(['code' => self::DATETIME_ATTRIBUTE_CODE])->willReturn($datetimeProductAttribute);
$attributeRepository->findOneBy(['code' => self::NOT_EXISTING_ATTRIBUTE_CODE])->willReturn(null);
$factory->createNew()->willReturn($attributeValue);
$localeProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'it_IT', 'de_DE']);
Expand Down Expand Up @@ -124,9 +129,14 @@ function it_support_existing_select_attributes(ProductVariantInterface $productV
$this->supports($productVariant, self::SELECT_ATTRIBUTE_CODE, [])->shouldReturn(true);
}

function it_support_existing_integer_attributes(ProductVariantInterface $productVariant)
{
$this->supports($productVariant, self::INTEGER_ATTRIBUTE_CODE, [])->shouldReturn(true);
}

function it_does_not_support_existing_attributes_of_not_supported_type(ProductVariantInterface $productVariant)
{
$this->supports($productVariant, self::INTEGER_ATTRIBUTE_CODE, [])->shouldReturn(false);
$this->supports($productVariant, self::DATETIME_ATTRIBUTE_CODE, [])->shouldReturn(false);
}

function it_does_not_support_not_existing_attribute(ProductVariantInterface $productVariant)
Expand Down Expand Up @@ -359,6 +369,36 @@ function it_creates_select_product_attribute_value_with_all_locales_if_it_does_n
$product->addAttribute($deAttributeValue)->shouldHaveBeenCalled();
}

function it_creates_integer_product_attribute_value_with_all_locales_if_it_does_not_already_exists(
ProductVariantInterface $productVariant,
ProductInterface $product,
ProductAttributeValueInterface $enAttributeValue,
ProductAttributeValueInterface $itAttributeValue,
ProductAttributeValueInterface $deAttributeValue,
FactoryInterface $factory
) {
$factory->createNew()->willReturn($enAttributeValue, $itAttributeValue, $deAttributeValue);
$product->getAttributeByCodeAndLocale(Argument::type('string'), Argument::type('string'))->willReturn(null);
$value = [
[
'scope' => null,
'locale' => null,
'data' => 123,
],
];
$this->handle($productVariant, self::INTEGER_ATTRIBUTE_CODE, $value);

$enAttributeValue->setLocaleCode('en_US')->shouldHaveBeenCalled();
$enAttributeValue->setValue(123)->shouldHaveBeenCalled();
$itAttributeValue->setLocaleCode('it_IT')->shouldHaveBeenCalled();
$itAttributeValue->setValue(123)->shouldHaveBeenCalled();
$deAttributeValue->setLocaleCode('de_DE')->shouldHaveBeenCalled();
$deAttributeValue->setValue(123)->shouldHaveBeenCalled();
$product->addAttribute($enAttributeValue)->shouldHaveBeenCalled();
$product->addAttribute($itAttributeValue)->shouldHaveBeenCalled();
$product->addAttribute($deAttributeValue)->shouldHaveBeenCalled();
}

function it_throws_error_when_select_value_is_not_an_existing_option(
ProductVariantInterface $productVariant,
ProductInterface $product
Expand Down
8 changes: 5 additions & 3 deletions src/ValueHandler/AttributeValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Webgriffe\SyliusAkeneoPlugin\ValueHandler;

use Sylius\Component\Attribute\AttributeType\CheckboxAttributeType;
use Sylius\Component\Attribute\AttributeType\IntegerAttributeType;
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
use Sylius\Component\Attribute\AttributeType\TextareaAttributeType;
use Sylius\Component\Attribute\AttributeType\TextAttributeType;
Expand Down Expand Up @@ -128,7 +129,8 @@ private function hasSupportedType(AttributeInterface $attribute): bool
return $attribute->getType() === TextareaAttributeType::TYPE ||
$attribute->getType() === TextAttributeType::TYPE ||
$attribute->getType() === CheckboxAttributeType::TYPE ||
$attribute->getType() === SelectAttributeType::TYPE;
$attribute->getType() === SelectAttributeType::TYPE ||
$attribute->getType() === IntegerAttributeType::TYPE;
}

private function isProductOption(ProductVariantInterface $subject, string $attributeCode): bool
Expand All @@ -145,9 +147,9 @@ private function isProductOption(ProductVariantInterface $subject, string $attri
}

/**
* @param string|bool $value
* @param int|string|bool $value
*
* @return array|string|bool
* @return array|int|string|bool
*/
private function getAttributeValue(AttributeInterface $attribute, $value)
{
Expand Down

0 comments on commit f8f472e

Please sign in to comment.