function it_updates_a_family($attrRequiFactory, $channelRepository, FamilyTranslation $translation, FamilyInterface $family, AttributeRepositoryInterface $attributeRepository, AttributeInterface $attribute1, AttributeInterface $attribute2, AttributeInterface $attribute3, AttributeInterface $attribute4, AttributeRequirementInterface $attributeRequirement1, AttributeRequirementInterface $attributeRequirement2, AttributeRequirementInterface $attributeRequirement3, AttributeRequirementInterface $attributeRequirement4, AttributeRequirementInterface $attributeRequirement5, ChannelInterface $channel1, ChannelInterface $channel2, FamilyInterface $family) { $values = ['code' => 'mycode', 'attributes' => ['sku', 'name', 'description', 'price'], 'attribute_as_label' => 'name', 'requirements' => ['mobile' => ['sku', 'name'], 'print' => ['sku', 'name', 'description']], 'labels' => ['fr_FR' => 'Moniteurs', 'en_US' => 'PC Monitors']]; $attributeRepository->findOneByIdentifier('sku')->willReturn($attribute1); $attributeRepository->findOneByIdentifier('name')->willReturn($attribute2); $attributeRepository->findOneByIdentifier('description')->willReturn($attribute3); $attributeRepository->findOneByIdentifier('price')->willReturn($attribute4); $channelRepository->findOneByIdentifier('mobile')->willReturn($channel1); $channelRepository->findOneByIdentifier('print')->willReturn($channel2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel1, true)->willReturn($attributeRequirement1); $attrRequiFactory->createAttributeRequirement($attribute2, $channel1, true)->willReturn($attributeRequirement2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel2, true)->willReturn($attributeRequirement3); $attrRequiFactory->createAttributeRequirement($attribute2, $channel2, true)->willReturn($attributeRequirement4); $attrRequiFactory->createAttributeRequirement($attribute3, $channel2, true)->willReturn($attributeRequirement5); $family->setAttributeRequirements([$attributeRequirement1, $attributeRequirement2, $attributeRequirement3, $attributeRequirement4, $attributeRequirement5])->shouldBeCalled(); $family->setCode('mycode')->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->setLocale('en_US')->shouldBeCalled(); $family->setLocale('fr_FR')->shouldBeCalled(); $family->getTranslation()->willReturn($translation); $translation->setLabel('label en us'); $translation->setLabel('label fr fr'); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute3)->shouldBeCalled(); $family->addAttribute($attribute4)->shouldBeCalled(); $family->setAttributeAsLabel($attribute2)->shouldBeCalled(); $this->update($family, $values, []); }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $values = new ArrayCollection(); foreach ($data as $attributeCode => $valuesData) { $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); foreach ($valuesData as $valueData) { $value = $this->denormalizer->denormalize($valueData, $this->valueClass, 'json', ['attribute' => $attribute] + $context); $values->add($value); } } return $values; }
function it_denormalizes_product_values_from_json($denormalizer, $registry, AttributeRepositoryInterface $attributeRepository, ProductValueInterface $nameValue, ProductValueInterface $colorValue, AttributeInterface $name, AttributeInterface $color) { $data = ['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']], 'color' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'value' => 'red']]]; $attributeRepository->findOneByIdentifier('name')->willReturn($name); $attributeRepository->findOneByIdentifier('color')->willReturn($color); $registry->getRepository('Attribute')->willReturn($attributeRepository); $denormalizer->denormalize($data['name'][0], 'ProductValue', 'json', ['attribute' => $name])->shouldBeCalled()->willReturn($nameValue); $denormalizer->denormalize($data['color'][0], 'ProductValue', 'json', ['attribute' => $color])->shouldBeCalled()->willReturn($colorValue); $values = $this->denormalize($data, 'ProductValue[]', 'json'); $values->shouldHaveCount(2); $values[0]->shouldBe($nameValue); $values[1]->shouldBe($colorValue); }
/** * @param GroupInterface $group * @param array $data */ protected function setAxis(GroupInterface $group, $data) { if (isset($data['axis']) && !empty($data['axis'])) { $axisCodes = explode(',', $data['axis']); $attributes = []; foreach ($axisCodes as $code) { $attribute = $this->attributeRepository->findOneByIdentifier($code); if (!$attribute) { throw new \LogicException(sprintf('Attribute with identifier "%s" not found', $code)); } $attributes[] = $attribute; } $group->setAxisAttributes($attributes); } }
/** * {@inheritdoc} */ public function process($family) { $configuration = $this->getJobConfiguration(); if (!array_key_exists('actions', $configuration)) { throw new InvalidArgumentException('Missing configuration for \'actions\'.'); } $actions = $configuration['actions']; foreach ($actions as $action) { $attribute = $this->attributeRepository->findOneByIdentifier($action['attribute_code']); $channel = $this->channelRepository->findOneByIdentifier($action['channel_code']); $isRequired = $action['is_required']; $family->addAttribute($attribute); $family->addAttributeRequirement($this->factory->createAttributeRequirement($attribute, $channel, $isRequired)); } return $family; }
/** * Get non eligible attributes to a product template * * @param GroupInterface $group * * @return AttributeInterface[] */ public function getNonEligibleAttributes(GroupInterface $variantGroup) { $attributes = $variantGroup->getAxisAttributes()->toArray(); $template = $variantGroup->getProductTemplate(); if (null !== $template) { foreach (array_keys($template->getValuesData()) as $attributeCode) { $attributes[] = $this->attributeRepository->findOneByIdentifier($attributeCode); } } $uniqueAttributes = $this->attributeRepository->findBy(['unique' => true]); foreach ($uniqueAttributes as $attribute) { if (!in_array($attribute, $attributes)) { $attributes[] = $attribute; } } return $attributes; }
/** * Extract attribute field name information with attribute code, locale code, scope code * and optionally price currency * * Returned array like: * [ * "attribute" => AttributeInterface, * "locale_code" => <locale_code>|null, * "scope_code" => <scope_code>|null, * "price_currency" => <currency_code> // this key is optional * ] * * Return null if the field name does not match an attribute. * * @param string $fieldName * * @return array|null */ public function extractColumnInfo($fieldName) { if (!isset($this->fieldNameInfoCache[$fieldName]) && !in_array($fieldName, $this->excludedFieldNames)) { $explodedFieldName = explode(self::FIELD_SEPARATOR, $fieldName); $attributeCode = $explodedFieldName[0]; $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode); if (null !== $attribute) { $this->checkFieldNameTokens($attribute, $fieldName, $explodedFieldName); $attributeInfo = $this->extractAttributeInfo($attribute, $explodedFieldName); $this->checkFieldNameLocaleByChannel($attribute, $fieldName, $attributeInfo); $this->fieldNameInfoCache[$fieldName] = $attributeInfo; } else { $this->excludedFieldNames[] = $fieldName; } } return isset($this->fieldNameInfoCache[$fieldName]) ? $this->fieldNameInfoCache[$fieldName] : null; }
/** * @param FamilyInterface $family * @param string $data */ protected function setAttributeAsLabel(FamilyInterface $family, $data) { if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($data))) { $family->setAttributeAsLabel($attribute); } else { throw new \InvalidArgumentException(sprintf('Attribute with "%s" code does not exist', $data)); } }
/** * @param string $code * * @throws ObjectNotFoundException * * @return AttributeInterface */ protected function getAttribute($code) { if (!array_key_exists($code, $this->attributes)) { $attribute = $this->attributeRepository->findOneByIdentifier($code); if (!$attribute) { throw new ObjectNotFoundException(sprintf('Attribute with code "%s" was not found.', $code)); } $this->attributes[$code] = $attribute; } return $this->attributes[$code]; }
/** * @param GroupInterface $variantGroup * @param array $axes * * @throws \InvalidArgumentException */ protected function setAxes(GroupInterface $variantGroup, array $axes) { if (null !== $variantGroup->getId()) { if (array_diff($this->getOriginalAxes($variantGroup->getAxisAttributes()), array_values($axes))) { throw new \InvalidArgumentException('Attributes: This property cannot be changed.'); } } foreach ($axes as $axis) { $attribute = $this->attributeRepository->findOneByIdentifier($axis); if (null !== $attribute) { $variantGroup->addAxisAttribute($attribute); } else { throw new \InvalidArgumentException(sprintf('Attribute "%s" does not exist', $axis)); } } }
function it_throws_exception_when_the_field_name_is_not_consistent_with_the_attribute_property($managerRegistry, AttributeRepositoryInterface $repository, AttributeInterface $attribute) { // global with extra locale $attribute->getCode()->willReturn('sku'); $attribute->isLocalizable()->willReturn(false); $attribute->isScopable()->willReturn(false); $attribute->getBackendType()->willReturn('text'); $repository->findOneByIdentifier('sku')->willReturn($attribute); $managerRegistry->getRepository(self::ATTRIBUTE_CLASS)->willReturn($repository); $this->shouldThrow(new \InvalidArgumentException('The field "sku-fr_FR" is not well-formatted, attribute "sku" expects no locale, no scope, no currency'))->duringExtractAttributeFieldNameInfos('sku-fr_FR'); // localizable without any locale $attribute->getCode()->willReturn('name'); $attribute->isLocalizable()->willReturn(true); $attribute->isScopable()->willReturn(false); $attribute->getBackendType()->willReturn('text'); $repository->findOneByIdentifier('name')->willReturn($attribute); $this->shouldThrow(new \InvalidArgumentException('The field "name" is not well-formatted, attribute "name" expects a locale, no scope, no currency'))->duringExtractAttributeFieldNameInfos('name'); // localizable, scopable and price without any currency $attribute->getCode()->willReturn('cost'); $attribute->isLocalizable()->willReturn(true); $attribute->isScopable()->willReturn(true); $attribute->getBackendType()->willReturn('prices'); $repository->findOneByIdentifier('cost')->willReturn($attribute); $this->shouldThrow(new \InvalidArgumentException('The field "cost" is not well-formatted, attribute "cost" expects a locale, a scope, an optional currency'))->duringExtractAttributeFieldNameInfos('cost'); }
public function it_should_not_remove_identifier_requirements_when_other_requirements_are_provided($attrRequiFactory, $channelRepository, FamilyInterface $family, AttributeRepositoryInterface $attributeRepository, AttributeInterface $skuAttribute, AttributeInterface $nameAttribute, AttributeInterface $descAttribute, AttributeRequirementInterface $skuMobileRqrmt, AttributeRequirementInterface $skuPrintRqrmt, AttributeRequirementInterface $namePrintRqrmt, AttributeRequirementInterface $descPrintRqrmt, ChannelInterface $mobileChannel, ChannelInterface $printChannel) { $values = ['code' => 'mycode', 'requirements' => ['print' => ['name', 'description']]]; $family->getAttributeRequirements()->willReturn([$skuMobileRqrmt, $skuPrintRqrmt]); $skuMobileRqrmt->getAttribute()->willReturn($skuAttribute); $skuPrintRqrmt->getAttribute()->willReturn($skuAttribute); $skuMobileRqrmt->getChannelCode()->willReturn('mobile'); $skuPrintRqrmt->getChannelCode()->willReturn('print'); $skuAttribute->getAttributeType()->willReturn(AttributeTypes::IDENTIFIER); $channelRepository->findOneByIdentifier('print')->willReturn($printChannel); $attributeRepository->findOneByIdentifier('name')->willReturn($nameAttribute); $attributeRepository->findOneByIdentifier('description')->willReturn($descAttribute); $attrRequiFactory->createAttributeRequirement($nameAttribute, $printChannel, true)->willReturn($namePrintRqrmt); $attrRequiFactory->createAttributeRequirement($descAttribute, $printChannel, true)->willReturn($descPrintRqrmt); $namePrintRqrmt->getAttribute()->willReturn($nameAttribute); $descPrintRqrmt->getAttribute()->willReturn($descAttribute); $channelRepository->getChannelCodes()->willReturn(['mobile', 'print']); $channelRepository->findOneByIdentifier('mobile')->willReturn($mobileChannel); $channelRepository->findOneByIdentifier('print')->willReturn($printChannel); $attributeRepository->getIdentifier()->willReturn($skuAttribute); $family->setCode('mycode')->shouldBeCalled(); $family->setAttributeRequirements([$skuMobileRqrmt, $skuPrintRqrmt, $namePrintRqrmt, $descPrintRqrmt])->shouldBeCalled(); $this->update($family, $values, []); }
/** * @param string $code * * @return AttributeInterface|null */ protected function findAttribute($code) { $attribute = $this->attributeRepository->findOneByIdentifier($code); return $attribute; }