Skip to content

Commit

Permalink
Merge pull request #133 from webgriffe/merge-1-15-in-master
Browse files Browse the repository at this point in the history
Merge #132 (v1.15.0) in master
  • Loading branch information
lruozzi9 authored Jun 6, 2022
2 parents 354d8fd + cd7e79b commit 142fc07
Show file tree
Hide file tree
Showing 3 changed files with 344 additions and 33 deletions.
149 changes: 143 additions & 6 deletions spec/ValueHandler/ProductOptionValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\Webgriffe\SyliusAkeneoPlugin\ValueHandler;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Api\AttributeApiInterface;
use Akeneo\Pim\ApiClient\Api\AttributeOptionApiInterface;
use Akeneo\Pim\ApiClient\Exception\HttpException;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -21,6 +22,7 @@
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ValueHandler\ProductOptionValueHandler;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;
Expand All @@ -43,6 +45,7 @@ public function let(
ProductVariantInterface $productVariant,
ProductInterface $product,
ProductOptionInterface $productOption,
AttributeApiInterface $attributeApi,
AkeneoPimClientInterface $apiClient,
AttributeOptionApiInterface $attributeOptionApi,
ProductOptionRepositoryInterface $productOptionRepository,
Expand All @@ -52,14 +55,17 @@ public function let(
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
TranslationLocaleProviderInterface $translationLocaleProvider
TranslationLocaleProviderInterface $translationLocaleProvider,
TranslatorInterface $translator,
ProductOptionValueInterface $existentProductOptionValue
): void {
$productVariant->getCode()->willReturn(self::VARIANT_CODE);
$productVariant->getProduct()->willReturn($product);
$product->getCode()->willReturn(self::PRODUCT_CODE);
$product->getOptions()->willReturn(new ArrayCollection([$productOption->getWrappedObject()]));
$productOption->getCode()->willReturn(self::OPTION_CODE);
$apiClient->getAttributeOptionApi()->willReturn($attributeOptionApi);
$apiClient->getAttributeApi()->willReturn($attributeApi);
$attributeOptionApi
->get(self::OPTION_CODE, self::VALUE_CODE)
->willReturn(
Expand All @@ -69,7 +75,14 @@ public function let(
'sort_order' => 4,
'labels' => ['en_US' => self::EN_LABEL, 'it_IT' => self::IT_LABEL],
]
);
)
;
$attributeApi->get(self::OPTION_CODE)->willReturn(
[
'code' => self::OPTION_CODE,
'type' => 'pim_catalog_simpleselect'
]
);
$productOptionRepository->findOneBy(['code' => self::OPTION_CODE])->willReturn($productOption);
$productOptionValueFactory->createNew()->willReturn($productOptionValue);
$productOptionValue->getTranslation('en_US')->willReturn($englishProductOptionValueTranslation);
Expand All @@ -80,13 +93,16 @@ public function let(
$englishProductOptionValueTranslation->getLocale()->willReturn('en_US');
$translationLocaleProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'it_IT']);

$productOptionValueRepository->findOneBy(['code' => self::OPTION_CODE . '_' . self::VALUE_CODE])->willReturn($existentProductOptionValue);

$this->beConstructedWith(
$apiClient,
$productOptionRepository,
$productOptionValueFactory,
$productOptionValueTranslationFactory,
$productOptionValueRepository,
$translationLocaleProvider
$translationLocaleProvider,
$translator
);
}

Expand Down Expand Up @@ -166,6 +182,35 @@ public function it_throws_exception_during_handle_when_value_has_an_invalid_numb
)->during('handle', [$productVariant, self::OPTION_CODE, $value]);
}

public function it_throws_an_exception_during_handle_if_attribute_does_not_exists_on_akeneo(
ProductVariantInterface $productVariant,
AttributeApiInterface $attributeApi
): void {
$value = [
[
'scope' => null,
'locale' => null,
'data' => self::VALUE_CODE,
],
];
$attributeApi->get(self::OPTION_CODE)->willThrow(
new HttpException('Not found', new Request('GET', '/'), new Response(404))
);

$this->shouldThrow(
new RuntimeException(
sprintf(
'Cannot handle option value on Akeneo product "%s", the option of the parent product "%s" is ' .
'"%s". The attribute "%s" does not exists.',
self::VARIANT_CODE,
self::PRODUCT_CODE,
self::OPTION_CODE,
self::OPTION_CODE
)
)
)->during('handle', [$productVariant, self::OPTION_CODE, $value]);
}

public function it_throws_an_exception_during_handle_if_attribute_option_does_not_exists_on_akeneo(
ProductVariantInterface $productVariant,
AttributeOptionApiInterface $attributeOptionApi
Expand Down Expand Up @@ -225,7 +270,8 @@ public function it_creates_product_option_value_from_factory_with_all_translatio
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository
): void {
$value = [
[
Expand All @@ -234,6 +280,7 @@ public function it_creates_product_option_value_from_factory_with_all_translatio
'data' => self::VALUE_CODE,
],
];
$productOptionValueRepository->findOneBy(['code' => self::OPTION_CODE . '_' . self::VALUE_CODE])->willReturn(null);
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);

$this->handle($productVariant, self::OPTION_CODE, $value);
Expand Down Expand Up @@ -263,7 +310,6 @@ public function it_updates_existing_product_option_value_and_all_translations(
'data' => self::VALUE_CODE,
],
];
$productOptionValueRepository->findOneBy(['code' => self::OPTION_CODE . '_' . self::VALUE_CODE])->willReturn($existentProductOptionValue);
$englishProductOptionValue->getLocale()->willReturn('en_US');
$italianProductOptionValue->getLocale()->willReturn('it_IT');
$existentProductOptionValue->getTranslation('en_US')->willReturn($englishProductOptionValue);
Expand All @@ -285,7 +331,8 @@ public function it_skips_locale_not_defined_on_sylius(
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
AttributeOptionApiInterface $attributeOptionApi,
FactoryInterface $productOptionValueTranslationFactory
FactoryInterface $productOptionValueTranslationFactory,
RepositoryInterface $productOptionValueRepository
): void {
$value = [
[
Expand All @@ -294,6 +341,7 @@ public function it_skips_locale_not_defined_on_sylius(
'data' => self::VALUE_CODE,
],
];
$productOptionValueRepository->findOneBy(['code' => self::OPTION_CODE . '_' . self::VALUE_CODE])->willReturn(null);
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$attributeOptionApi
->get(self::OPTION_CODE, self::VALUE_CODE)
Expand Down Expand Up @@ -321,4 +369,93 @@ public function it_skips_locale_not_defined_on_sylius(
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}

public function it_supports_product_option_metrical_value(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository,
AttributeApiInterface $attributeApi,
FactoryInterface $productOptionValueTranslationFactory,
TranslatorInterface $translator
): void {
$attributeApi->get(self::OPTION_CODE)->willReturn(
[
'code' => self::OPTION_CODE,
'type' => 'pim_catalog_metric'
]
);
$value = [
[
'scope' => null,
'locale' => null,
'data' => [
'amount' => '250.0000',
'unit' => 'CUBIC_CENTIMETER',
],
],
];
$translator->trans('webgriffe_sylius_akeneo.ui.metric_amount_unit', ['unit' => 'CUBIC_CENTIMETER', 'amount' => 250.0000], null, 'en_US')->shouldBeCalledOnce()->willReturn('250 cm3');
$translator->trans('webgriffe_sylius_akeneo.ui.metric_amount_unit', ['unit' => 'CUBIC_CENTIMETER', 'amount' => 250.0000], null, 'it_IT')->shouldBeCalledOnce()->willReturn('250 cm3');
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$productOptionValueRepository->findOneBy(['code' => 'option-code_2500000_CUBIC_CENTIMETER'])->willReturn(null);

$this->handle($productVariant, self::OPTION_CODE, $value);

$productOptionValue->setCode('option-code_2500000_CUBIC_CENTIMETER')->shouldHaveBeenCalled();
$productOptionValue->setOption($productOption)->shouldHaveBeenCalled();
$productOption->addValue($productOptionValue)->shouldHaveBeenCalled();
$englishProductOptionValueTranslation->setValue('250 cm3')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setLocale('it_IT')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setValue('250 cm3')->shouldHaveBeenCalled();
$productOptionValue->addTranslation($englishProductOptionValueTranslation)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($italianProductOptionValueTranslation)->shouldHaveBeenCalled();
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}

public function it_supports_product_option_boolean_value(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository,
AttributeApiInterface $attributeApi,
FactoryInterface $productOptionValueTranslationFactory,
TranslatorInterface $translator
): void {
$attributeApi->get(self::OPTION_CODE)->willReturn(
[
'code' => self::OPTION_CODE,
'type' => 'pim_catalog_boolean'
]
);
$value = [
[
'scope' => null,
'locale' => null,
'data' => true,
],
];
$translator->trans('sylius.ui.yes_label', [], null, 'en_US')->shouldBeCalledOnce()->willReturn('Yes');
$translator->trans('sylius.ui.yes_label', [], null, 'it_IT')->shouldBeCalledOnce()->willReturn('Si');
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$productOptionValueRepository->findOneBy(['code' => 'option-code_1'])->willReturn(null);

$this->handle($productVariant, self::OPTION_CODE, $value);

$productOptionValue->setCode('option-code_1')->shouldHaveBeenCalled();
$productOptionValue->setOption($productOption)->shouldHaveBeenCalled();
$productOption->addValue($productOptionValue)->shouldHaveBeenCalled();
$englishProductOptionValueTranslation->setValue('Yes')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setLocale('it_IT')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setValue('Si')->shouldHaveBeenCalled();
$productOptionValue->addTranslation($englishProductOptionValueTranslation)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($italianProductOptionValueTranslation)->shouldHaveBeenCalled();
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/WebgriffeSyliusAkeneoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ final class WebgriffeSyliusAkeneoExtension extends AbstractResourceExtension imp
'$productOptionValueTranslationFactory' => 'sylius.factory.product_option_value_translation',
'$productOptionValueRepository' => 'sylius.repository.product_option_value',
'$translationLocaleProvider' => 'sylius.translation_locale_provider.admin',
'$translator' => 'translator',
],
],
'translatable_property' => [
Expand Down
Loading

0 comments on commit 142fc07

Please sign in to comment.