function it_copies_simple_select_value_to_a_product_value($builder, $attrValidatorHelper, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, ProductInterface $product4, ProductValueInterface $fromProductValue, ProductValueInterface $toProductValue, AttributeOptionInterface $attributeOption) { $fromLocale = 'fr_FR'; $toLocale = 'fr_FR'; $toScope = 'mobile'; $fromScope = 'mobile'; $fromAttribute->getCode()->willReturn('fromAttributeCode'); $toAttribute->getCode()->willReturn('toAttributeCode'); $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled(); $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled(); $fromProductValue->getData()->willReturn($attributeOption); $toProductValue->setOption($attributeOption)->shouldBeCalledTimes(3); $product1->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product1->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue); $product2->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn(null); $product2->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue); $product3->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product3->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null); $product4->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product4->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue); $builder->addProductValue($product3, $toAttribute, $toLocale, $toScope)->shouldBeCalledTimes(1)->willReturn($toProductValue); $products = [$product1, $product2, $product3, $product4]; foreach ($products as $product) { $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]); } }
function it_adds_a_violation_when_validates_a_product_with_missing_value_for_an_axe_of_its_variant_group($context, ProductInterface $product, GroupInterface $tShirtVariantGroup, AttributeInterface $sizeAttribute, AttributeInterface $colorAttribute, ProductValueInterface $sizeValue, ProductValueInterface $identifierValue, HasVariantAxes $constraint, ConstraintViolationBuilderInterface $violation) { $tShirtVariantGroup->getCode()->willReturn('tshirt'); $tShirtVariantGroup->getAxisAttributes()->willReturn([$sizeAttribute, $colorAttribute]); $sizeAttribute->getCode()->willReturn('size'); $colorAttribute->getCode()->willReturn('color'); $product->getIdentifier()->willReturn($identifierValue); $product->getVariantGroup()->willReturn($tShirtVariantGroup); $product->getValue('size')->willReturn($sizeValue); $product->getValue('color')->willReturn(null); $sizeValue->getData()->willReturn('XL'); $context->buildViolation('The product "%product%" is in the variant group "%variant%" but it misses the following axes: %axes%.', ['%product%' => $identifierValue, '%variant%' => 'tshirt', '%axes%' => 'color'])->shouldBeCalled()->willReturn($violation); $this->validate($product, $constraint); }
function it_allows_setting_attribute_data_option_to_null(ProductInterface $product, AttributeInterface $attribute, ProductValueInterface $value) { $attribute->getCode()->willReturn('choice'); $product->getValue('choice', 'fr_FR', 'mobile')->shouldBeCalled()->willReturn($value); $value->setOption(null)->shouldBeCalled(); $this->setAttributeData($product, $attribute, null, ['locale' => 'fr_FR', 'scope' => 'mobile']); }
/** * Set media in the product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param FileInfoInterface|null $fileInfo * @param string|null $locale * @param string|null $scope */ protected function setMedia(ProductInterface $product, AttributeInterface $attribute, FileInfoInterface $fileInfo = null, $locale = null, $scope = null) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } $value->setMedia($fileInfo); }
/** * Set option into the product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param AttributeOptionInterface|null $option * @param string|null $locale * @param string|null $scope */ protected function setOption(ProductInterface $product, AttributeInterface $attribute, AttributeOptionInterface $option = null, $locale = null, $scope = null) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } $value->setOption($option); }
/** * Set data into product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param mixed $data * @param string $locale * @param string $scope */ protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $locale, $scope) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } $value->setData($data); }
function it_sets_attribute_when_new_value_is_different_from_product_value($builder, AttributeInterface $attribute, ProductInterface $product, FamilyInterface $family, ProductValue $productValue) { $family->getAttributeCodes()->willReturn(['sku', 'is_color']); $product->getFamily()->willReturn($family); $attribute->getCode()->willReturn('is_color'); $product->getValue('is_color', null, null)->willReturn(null); $builder->addProductValue($product, $attribute, null, null)->willReturn($productValue); $this->setAttributeData($product, $attribute, true, ['locale' => null, 'scope' => null]); }
function let(ProductRepositoryInterface $productRepository, UniqueValuesSet $uniqueValuesSet, ExecutionContextInterface $context, Form $form, ProductInterface $product, ProductValueInterface $value) { $this->beConstructedWith($productRepository, $uniqueValuesSet); $product->getValue('unique_attribute')->willReturn($value); $form->getData()->willReturn($product); $context->getPropertyPath()->willReturn(self::PROPERTY_PATH); $context->getRoot()->willReturn($form); $this->initialize($context); }
/** * @param ProductInterface $product * @param AttributeInterface $attribute * @param array $attributeOptions * @param string|null $locale * @param string|null $scope */ protected function removeOptions(ProductInterface $product, AttributeInterface $attribute, $attributeOptions, $locale, $scope) { $productValue = $product->getValue($attribute->getCode(), $locale, $scope); if (null !== $productValue) { foreach ($attributeOptions as $attributeOption) { $productValue->removeOption($attributeOption); } } }
function it_should_removes_an_attribute_data_multi_select_value_to_a_product_value(AttributeInterface $attribute, ProductInterface $product, ProductValueInterface $productValue, AttributeOptionInterface $attributeOption, $attrOptionRepository) { $attribute->getCode()->willReturn('tshirt_style'); $attrOptionRepository->findOneByIdentifier('tshirt_style.vneck')->willReturn($attributeOption); $product->getValue('tshirt_style', 'fr_FR', 'mobile')->willReturn($productValue); $productValue->removeOption($attributeOption)->shouldBeCalled(); $data = ['vneck']; $this->removeAttributeData($product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']); }
/** * Returns the image path for an attribute of a product. If no image is found, returns null. * * @param ProductInterface $product * @param AttributeInterface $attribute * @param string $locale * @param string $scope * * @return string|null */ public function getImagePath(ProductInterface $product, AttributeInterface $attribute, $locale, $scope) { $productValue = $product->getValue($attribute->getCode(), $locale, $scope); $path = null; if (null !== $productValue->getMedia() && null !== $productValue->getMedia()->getKey()) { $path = sprintf('media/cache/thumbnail/%s', $productValue->getMedia()->getKey()); } return $path; }
/** * Adds options into the product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param array $attributeOptions * @param string $locale * @param string $scope */ protected function addOptions(ProductInterface $product, AttributeInterface $attribute, $attributeOptions, $locale, $scope) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } foreach ($attributeOptions as $attributeOption) { $value->addOption($attributeOption); } }
/** * Remove prices from product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param mixed $data * @param string $locale * @param string $scope */ protected function removePrices(ProductInterface $product, AttributeInterface $attribute, $data, $locale, $scope) { $productValue = $product->getValue($attribute->getCode(), $locale, $scope); if (null !== $productValue) { foreach ($data as $price) { $priceToRemove = $productValue->getPrice($price['currency']); $productValue->removePrice($priceToRemove); } } }
function it_sets_null_value_when_receiving_empty_string(AttributeInterface $attribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, $builder, ProductValue $productValue) { $locale = 'fr_FR'; $scope = 'mobile'; $data = ''; $attribute->getCode()->willReturn('attributeCode'); $productValue->setData(null)->shouldBeCalled(); $builder->addProductValue($product2, $attribute, $locale, $scope)->willReturn($productValue); $product1->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn($productValue); $this->setAttributeData($product1, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); }
/** * Copy single value * * @param ProductInterface $fromProduct * @param ProductInterface $toProduct * @param AttributeInterface $fromAttribute * @param AttributeInterface $toAttribute * @param string $fromLocale * @param string $toLocale * @param string $fromScope * @param string $toScope */ protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope) { $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope); if (null !== $fromValue) { $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope); if (null === $toValue) { $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope); } $this->copyOptions($fromValue, $toValue); } }
/** * Get missing axis codes of a product given a variant group * * @param ProductInterface $product * @param GroupInterface $variantGroup * * @return array */ protected function getMissingAxisCodes(ProductInterface $product, GroupInterface $variantGroup) { $missingAxisCodes = []; foreach ($variantGroup->getAxisAttributes() as $attribute) { $value = $product->getValue($attribute->getCode()); if (null === $value || null === $value->getData()) { $missingAxisCodes[] = $attribute->getCode(); } } return $missingAxisCodes; }
/** * Set reference data into the product value * * @param AttributeInterface $attribute * @param ProductInterface $product * @param ReferenceDataInterface|null $referenceData * @param string|null $locale * @param string|null $scope * * @throws \LogicException */ protected function setReferenceData(AttributeInterface $attribute, ProductInterface $product, $referenceData = null, $locale = null, $scope = null) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } $setMethod = MethodNameGuesser::guess('set', $attribute->getReferenceDataName(), true); if (!method_exists($value, $setMethod)) { throw new \LogicException(sprintf('ProductValue method "%s" is not implemented', $setMethod)); } $value->{$setMethod}($referenceData); }
/** * @param ProductInterface $fromProduct * @param ProductInterface $toProduct * @param AttributeInterface $fromAttribute * @param AttributeInterface $toAttribute * @param string|null $fromLocale * @param string|null $toLocale * @param string|null $fromScope * @param string|null $toScope */ protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope) { $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope); if (null !== $fromValue) { $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope); if (null === $toValue) { $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope); } $fromDataGetter = $this->getValueMethodName($fromValue, $fromAttribute, 'get'); $toDataSetter = $this->getValueMethodName($toValue, $toAttribute, 'set'); $toValue->{$toDataSetter}($fromValue->{$fromDataGetter}()); } }
/** * Set the data into the product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param mixed $data * @param string $unit * @param string $locale * @param string $scope */ protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $unit, $locale, $scope) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } if (null === ($metric = $value->getMetric())) { $metric = $this->metricFactory->createMetric($attribute->getMetricFamily()); } $value->setMetric($metric); $metric->setUnit($unit); $metric->setData($data); }
function it_copies_a_metric_value_to_a_product_value($builder, $metricFactory, $attrValidatorHelper, MetricInterface $metric, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, ProductInterface $product4, ProductValue $fromProductValue, ProductValue $toProductValue, ProductValue $toProductValue2) { $fromLocale = 'fr_FR'; $toLocale = 'fr_FR'; $toScope = 'mobile'; $fromScope = 'mobile'; $fromAttribute->getCode()->willReturn('fromAttributeCode'); $toAttribute->getCode()->willReturn('toAttributeCode'); $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled(); $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled(); $attrValidatorHelper->validateUnitFamilies(Argument::cetera())->shouldBeCalled(); $fromProductValue->getData()->willReturn($metric); $toProductValue->setMetric($metric)->shouldBeCalledTimes(2); $toProductValue->getData()->willReturn($metric); $toProductValue->getMetric()->willReturn($metric); $toProductValue2->setMetric($metric)->shouldBeCalledTimes(1); $toProductValue2->getData()->willReturn($metric); $toProductValue2->getMetric()->willReturn(null); $metric->getFamily()->shouldBeCalled()->willReturn('Weight'); $metric->getData()->shouldBeCalled()->willReturn(123); $metric->getUnit()->shouldBeCalled()->willReturn('kg'); $metric->setData(123)->shouldBeCalled(); $metric->setUnit('kg')->shouldBeCalled(); $product1->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product1->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue); $product2->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn(null); $product2->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue); $product3->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product3->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null); $product4->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue); $product4->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue2); $metricFactory->createMetric('Weight')->shouldBeCalledTimes(1)->willReturn($metric); $builder->addProductValue($product3, $toAttribute, $toLocale, $toScope)->shouldBeCalledTimes(1)->willReturn($toProductValue); $products = [$product1, $product2, $product3, $product4]; foreach ($products as $product) { $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]); } }
function it_renders_a_product_with_an_image($templating, ProductInterface $blender, AttributeGroupInterface $media, AttributeInterface $mainImage, ProductValueInterface $productValue, FileInfoInterface $fileInfo, CacheManager $cacheManager) { $blender->getAttributes()->willReturn([$mainImage]); $blender->getValue("main_image", "en_US", "ecommerce")->willReturn($productValue); $productValue->getMedia()->willReturn($fileInfo); $fileInfo->getKey()->willReturn('fookey'); $cacheManager->isStored('fookey', 'thumbnail')->willReturn(true); $mainImage->getGroup()->willReturn($media); $media->getLabel()->willReturn('Media'); $mainImage->getCode()->willReturn('main_image'); $mainImage->getAttributeType()->willReturn('pim_catalog_image'); $templating->render(self::TEMPLATE_NAME, ['product' => $blender, 'locale' => 'en_US', 'scope' => 'ecommerce', 'groupedAttributes' => ['Media' => ['main_image' => $mainImage]], 'imageAttributes' => ['main_image' => $mainImage], 'uploadDir' => '/tmp/' . DIRECTORY_SEPARATOR, 'customFont' => null])->shouldBeCalled(); $this->render($blender, 'pdf', ['locale' => 'en_US', 'scope' => 'ecommerce']); }
/** * Set the data into the product value * * @param ProductInterface $product * @param AttributeInterface $attribute * @param mixed $data * @param string $locale * @param string $scope */ protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $locale, $scope) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { if (!$this->shouldBeSetInProduct($product, $attribute, $data)) { return; } $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } if (1 === $data || '1' === $data || 0 === $data || '0' === $data) { $data = (bool) $data; } $value->setData($data); }
function it_sets_non_number_value_to_a_product_attribute_data_value(AttributeInterface $attribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, $builder, ProductValue $productValue) { $locale = 'fr_FR'; $scope = 'mobile'; $data = 'foo'; $attribute->getCode()->willReturn('attributeCode'); $productValue->setData($data)->shouldBeCalled(); $builder->addProductValue($product2, $attribute, $locale, $scope)->willReturn($productValue); $product1->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn($productValue); $product2->getValue('attributeCode', $locale, $scope)->willReturn(null); $product3->getValue('attributeCode', $locale, $scope)->willReturn($productValue); $this->setAttributeData($product1, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $this->setAttributeData($product2, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $this->setAttributeData($product3, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); }
function it_adds_attribute_data_on_multiselect_value_to_a_product_value($builder, $attrOptionRepository, AttributeInterface $attribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, ProductValueInterface $productValue, AttributeOptionInterface $attributeOption) { $locale = 'fr_FR'; $scope = 'mobile'; $attribute->getCode()->willReturn('attributeCode'); $attributeOption->getCode()->willReturn('attributeOptionCode'); $attrOptionRepository->findOneByIdentifier('attributeCode.attributeOptionCode')->shouldBeCalledTimes(3)->willReturn($attributeOption); $productValue->addOption($attributeOption)->shouldBeCalled(); $builder->addProductValue($product2, $attribute, $locale, $scope)->willReturn($productValue); $product1->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn($productValue); $product2->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn(null); $product3->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn($productValue); $this->addAttributeData($product1, $attribute, ['attributeOptionCode'], ['locale' => $locale, 'scope' => $scope]); $this->addAttributeData($product2, $attribute, ['attributeOptionCode'], ['locale' => $locale, 'scope' => $scope]); $this->addAttributeData($product3, $attribute, ['attributeOptionCode'], ['locale' => $locale, 'scope' => $scope]); }
function it_removes_an_attribute_data_price_collection_value_to_a_product_value($currencyManager, AttributeInterface $attribute, ProductInterface $penProduct, ProductInterface $bookProduct, ProductValueInterface $productValue, ProductPriceInterface $priceEUR, ProductPriceInterface $priceUSD) { $locale = 'fr_FR'; $scope = 'mobile'; $data = [['data' => 123.2, 'currency' => 'EUR'], ['data' => null, 'currency' => 'USD']]; $currencyManager->getActiveCodes()->willReturn(['EUR', 'USD']); $attribute->getCode()->willReturn('attributeCode'); $penProduct->getValue('attributeCode', $locale, $scope)->willReturn($productValue); $bookProduct->getValue('attributeCode', $locale, $scope)->willReturn(null); $productValue->getPrice('EUR')->willReturn($priceEUR); $productValue->getPrice('USD')->willReturn($priceUSD); $productValue->removePrice($priceEUR)->shouldBeCalledTimes(1); $productValue->removePrice($priceUSD)->shouldBeCalledTimes(1); $this->removeAttributeData($penProduct, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $this->removeAttributeData($bookProduct, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); }
/** * @param ProductInterface $fromProduct * @param ProductInterface $toProduct * @param AttributeInterface $fromAttribute * @param AttributeInterface $toAttribute * @param string $fromLocale * @param string $toLocale * @param string $fromScope * @param string $toScope */ protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope) { $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope); if (null !== $fromValue) { $fromData = $fromValue->getData(); $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope); if (null === $toValue) { $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope); } if (null === ($metric = $toValue->getMetric())) { $metric = $this->metricFactory->createMetric($fromData->getFamily()); } $metric->setUnit($fromData->getUnit()); $metric->setData($fromData->getData()); $toValue->setMetric($metric); } }
/** * @param ProductInterface $fromProduct * @param ProductInterface $toProduct * @param AttributeInterface $fromAttribute * @param AttributeInterface $toAttribute * @param string $fromLocale * @param string $toLocale * @param string $fromScope * @param string $toScope */ protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope) { $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope); if (null !== $fromValue) { $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope); if (null === $toValue) { $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope); } $file = null; if (null !== $fromValue->getMedia()) { $filesystem = $this->filesystemProvider->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS); $rawFile = $this->fileFetcher->fetch($filesystem, $fromValue->getMedia()->getKey()); $file = $this->fileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false); $file->setOriginalFilename($fromValue->getMedia()->getOriginalFilename()); } $toValue->setMedia($file); } }
/** * Set reference data collection into the product value * * @param AttributeInterface $attribute * @param ProductInterface $product * @param array $refDataCollection * @param string|null $locale * @param string|null $scope * * @throws \LogicException */ protected function setReferenceDataCollection(AttributeInterface $attribute, ProductInterface $product, array $refDataCollection, $locale = null, $scope = null) { $value = $product->getValue($attribute->getCode(), $locale, $scope); if (null === $value) { $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope); } $referenceDataName = $attribute->getReferenceDataName(); $addMethod = MethodNameGuesser::guess('add', $referenceDataName, true); $removeMethod = MethodNameGuesser::guess('remove', $referenceDataName, true); $getMethod = MethodNameGuesser::guess('get', $referenceDataName); if (!method_exists($value, $addMethod) || !method_exists($value, $removeMethod) || !method_exists($value, $getMethod)) { throw new \LogicException(sprintf('One of these methods is not implemented in %s: "%s", "%s", "%s"', ClassUtils::getClass($value), $addMethod, $removeMethod, $getMethod)); } $currentCollection = $value->{$getMethod}(); foreach ($currentCollection as $currentReferenceData) { $value->{$removeMethod}($currentReferenceData); } foreach ($refDataCollection as $referenceData) { $value->{$addMethod}($referenceData); } }
function it_sets_a_attribute_data_price_collection_value_to_a_product_value($builder, AttributeInterface $attribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, ProductInterface $product4, ProductValue $productValue, ProductPriceInterface $price) { $locale = 'fr_FR'; $scope = 'mobile'; $data = [['data' => 123.2, 'currency' => 'EUR']]; $attribute->getCode()->willReturn('attributeCode'); $builder->addProductValue($product2, $attribute, $locale, $scope)->willReturn($productValue); $product1->getValue('attributeCode', $locale, $scope)->shouldBeCalled()->willReturn($productValue); $product2->getValue('attributeCode', $locale, $scope)->willReturn(null); $product3->getValue('attributeCode', $locale, $scope)->willReturn($productValue); $product4->getValue('attributeCode', $locale, $scope)->willReturn($productValue); $productValue->getPrices()->willReturn([$price]); $price->setData(null)->shouldBeCalled(); $builder->addPriceForCurrencyWithData($productValue, 'EUR', 123.2)->shouldBeCalled(); $this->setattributeData($product1, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $this->setattributeData($product2, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $this->setattributeData($product3, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); $data = [['data' => 'foo', 'currency' => 'EUR']]; $builder->addPriceForCurrencyWithData($productValue, 'EUR', 'foo')->shouldBeCalled(); $this->setattributeData($product4, $attribute, $data, ['locale' => $locale, 'scope' => $scope]); }
/** * Returns a ProductValue * * @param ProductInterface $product * @param ColumnInfoInterface $columnInfo * * @return ProductValueInterface */ protected function getProductValue(ProductInterface $product, ColumnInfoInterface $columnInfo) { return $product->getValue($columnInfo->getName(), $columnInfo->getLocale(), $columnInfo->getScope()); }