From deeab7dd82b93a8b85953de843d1e590d021d0ac Mon Sep 17 00:00:00 2001 From: Fabiana Romagnoli Date: Mon, 4 Jan 2021 14:00:41 +0100 Subject: [PATCH] AttributeValueHandler also handle integer type --- .../AttributeValueHandlerSpec.php | 44 ++++++++++++++++++- src/ValueHandler/AttributeValueHandler.php | 8 ++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/spec/ValueHandler/AttributeValueHandlerSpec.php b/spec/ValueHandler/AttributeValueHandlerSpec.php index 84b4c6c4..dafe1100 100644 --- a/spec/ValueHandler/AttributeValueHandlerSpec.php +++ b/spec/ValueHandler/AttributeValueHandlerSpec.php @@ -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'; @@ -39,6 +41,7 @@ function let( ProductAttributeInterface $textareaProductAttribute, ProductAttributeInterface $integerProductAttribute, ProductAttributeInterface $selectProductAttribute, + ProductAttributeInterface $datetimeProductAttribute, ProductAttributeValueInterface $attributeValue, RepositoryInterface $attributeRepository, FactoryInterface $factory, @@ -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); @@ -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']); @@ -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) @@ -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 diff --git a/src/ValueHandler/AttributeValueHandler.php b/src/ValueHandler/AttributeValueHandler.php index 43ad08c7..2378690d 100644 --- a/src/ValueHandler/AttributeValueHandler.php +++ b/src/ValueHandler/AttributeValueHandler.php @@ -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; @@ -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 @@ -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) {