/** * @param GroupInterface $group * * @return array */ protected function getFormOptionsForEdition(GroupInterface $group) { $axesIds = array_map(function (AttributeInterface $attribute) { return $attribute->getId(); }, $group->getAxisAttributes()->toArray()); $options = ['data' => $group->getAxisAttributes(), 'disabled' => true, 'read_only' => true, 'query_builder' => function (AttributeRepositoryInterface $repository) use($axesIds) { $qb = $repository->findAllAxesQB(); $qb->andWhere('a.id IN (:ids)'); $qb->setParameter('ids', $axesIds); return $qb; }]; return $options; }
/** * @param VariantGroupAxis $constraint * @param GroupInterface $variantGroup */ protected function validateAttributeAxis(VariantGroupAxis $constraint, GroupInterface $variantGroup) { $allowedTypes = [AttributeTypes::OPTION_SIMPLE_SELECT, AttributeTypes::REFERENCE_DATA_SIMPLE_SELECT]; foreach ($variantGroup->getAxisAttributes() as $attribute) { if (!in_array($attribute->getAttributeType(), $allowedTypes)) { $this->addInvalidAxisViolation($constraint, $variantGroup->getCode(), $attribute->getCode()); } } }
/** * Normalize the attributes * * @param GroupInterface $group * * @return array */ protected function normalizeAxisAttributes(GroupInterface $group) { $attributes = []; foreach ($group->getAxisAttributes() as $attribute) { $attributes[] = $attribute->getCode(); } sort($attributes); return $attributes; }
function it_throws_an_error_if_axis_is_updated(GroupInterface $variantGroup) { $variantGroup->setCode('mycode')->shouldBeCalled(); $variantGroup->getId()->willReturn(42); $attribute = new Attribute(); $attribute->setCode('other'); $variantGroup->getAxisAttributes()->willReturn(new ArrayCollection([$attribute])); $values = ['code' => 'mycode', 'axis' => ['main_color']]; $this->shouldThrow(new \InvalidArgumentException('Attributes: This property cannot be changed.'))->during('update', [$variantGroup, $values, []]); }
/** * 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; }
function it_returns_non_eligible_attributes($attributeRepository, GroupInterface $group, ProductTemplateInterface $template, AttributeInterface $length, AttributeInterface $name, AttributeInterface $color, AttributeInterface $identifier, Collection $collection) { $group->getProductTemplate()->willReturn($template); $group->getAxisAttributes()->willReturn($collection); $collection->toArray()->willReturn([$length]); $template->getValuesData()->willReturn(['name' => 'foo', 'color' => 'bar']); $attributeRepository->findOneByIdentifier('name')->willReturn($name); $attributeRepository->findOneByIdentifier('color')->willReturn($color); $attributeRepository->findBy(['unique' => true])->willReturn([$name, $identifier]); $attributes = [$length, $name, $color, $identifier]; $this->getNonEligibleAttributes($group)->shouldReturn($attributes); }
function it_adds_a_violation_if_axis_attributes_are_invalid($context, GroupInterface $variantGroup, GroupType $type, VariantGroupAxis $constraint, ConstraintViolationBuilderInterface $violation, AttributeInterface $invalidAxis) { $variantGroup->getId()->willReturn(12); $variantGroup->getType()->willReturn($type); $variantGroup->getCode()->willReturn('tshirt'); $type->isVariant()->willReturn(true); $variantGroup->getAxisAttributes()->willReturn([$invalidAxis]); $invalidAxis->getAttributeType()->willReturn(AttributeTypes::TEXT); $invalidAxis->getCode()->willReturn('name'); $violationData = ['%group%' => 'tshirt', '%attribute%' => 'name']; $context->buildViolation($constraint->invalidAxisMessage, $violationData)->shouldBeCalled()->willReturn($violation); $this->validate($variantGroup, $constraint); }
/** * Validate variant group product template values * * @param GroupInterface $variantGroup * @param Constraint $constraint */ protected function validateProductTemplateValues(GroupInterface $variantGroup, Constraint $constraint) { $template = $variantGroup->getProductTemplate(); $valuesData = $template->getValuesData(); $forbiddenAttrCodes = $this->attributeRepository->findUniqueAttributeCodes(); foreach ($variantGroup->getAxisAttributes() as $axisAttribute) { $forbiddenAttrCodes[] = $axisAttribute->getCode(); } $invalidAttrCodes = array_intersect($forbiddenAttrCodes, array_keys($valuesData)); if (count($invalidAttrCodes) > 0) { $this->context->buildViolation($constraint->message, ['%group%' => $variantGroup->getCode(), '%attributes%' => $this->formatValues($invalidAttrCodes)])->addViolation(); } }
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_processes_variant_group_without_product_template($objectDetacher, $normalizer, $stepExecution, $variantGroupUpdater, $mediaFetcher, AttributeInterface $color, AttributeInterface $weight, GroupInterface $variantGroup, JobParameters $jobParameters) { $stepExecution->getJobParameters()->willReturn($jobParameters); $jobParameters->has('with_media')->willReturn(true); $jobParameters->get('with_media')->willReturn(false); $variantGroup->getProductTemplate()->willReturn(null); $variantGroup->getCode()->willReturn('my_variant_group'); $color->getCode()->willReturn('color'); $weight->getCode()->willReturn('weight'); $variantGroup->getAxisAttributes()->willReturn(new ArrayCollection([$color, $weight])); $variantStandard = ['code' => 'my_variant_group', 'axis' => ['color', 'weight'], 'type' => 'variant', 'labels' => ['en_US' => 'My variant group', 'fr_FR' => 'Mon groupe de variante']]; $normalizer->normalize($variantGroup, null, ['with_variant_group_values' => true, 'identifier' => 'my_variant_group'])->willReturn($variantStandard); $variantGroupUpdater->update($variantGroup, Argument::any())->shouldNotBeCalled(); $mediaFetcher->fetchAll(Argument::any())->shouldNotBeCalled(); $this->process($variantGroup)->shouldReturn($variantStandard); $objectDetacher->detach($variantGroup)->shouldBeCalled(); }
function it_adds_a_violation_if_variant_group_template_contains_a_unique_attribute(GroupInterface $variantGroup, GroupType $type, ProductTemplateInterface $template, VariantGroupValues $constraint, $attributeRepository, $context, ConstraintViolationBuilderInterface $violation) { $variantGroup->getType()->willReturn($type); $variantGroup->getCode()->willReturn('tshirt'); $type->isVariant()->willReturn(true); $variantGroup->getProductTemplate()->willReturn($template); $variantGroup->getAxisAttributes()->willReturn([]); $attributeRepository->findUniqueAttributeCodes()->willReturn(['sku', 'barcode']); $template->getValuesData()->willReturn(['sku' => 'SKU-001']); $violationData = ['%group%' => 'tshirt', '%attributes%' => '"sku"']; $context->buildViolation($constraint->message, $violationData)->shouldBeCalled()->willReturn($violation); $this->validate($variantGroup, $constraint); $template->getValuesData()->willReturn(['sku' => 'SKU-001', 'barcode' => 01122334455]); $violationData = ['%group%' => 'tshirt', '%attributes%' => '"sku", "barcode"']; $context->buildViolation($constraint->message, $violationData)->shouldBeCalled()->willReturn($violation); $this->validate($variantGroup, $constraint); }
/** * Get non eligible attributes to a product template * * @param GroupInterface $variantGroup * * @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; }
function it_normalizes_a_variant_group_with_its_values($transNormalizer, $valuesDenormalizer, $valuesNormalizer, GroupInterface $group, GroupTypeInterface $groupType, AttributeInterface $attr, ProductTemplateInterface $productTemplate) { $groupType->getCode()->willReturn('VARIANT'); $groupType->isVariant()->willReturn(true); $group->getCode()->willReturn('laser_sabers'); $valuesData = ['name' => 'Light saber model', 'size' => '120']; $context = ['with_variant_group_values' => true]; $format = 'csv'; $productTemplate->getValuesData()->willReturn($valuesData); $valuesDenormalizer->denormalize($valuesData, Argument::any(), Argument::any())->willReturn('denormalized_values'); $valuesNormalizer->normalize('denormalized_values', Argument::any(), Argument::any())->willReturn('normalized_values'); $group->getProductTemplate()->willReturn($productTemplate); $group->getType()->willReturn($groupType); $attr->getCode()->willReturn('light_color'); $group->getAxisAttributes()->willReturn([$attr]); $transNormalizer->normalize($group, $format, $context)->willReturn([]); $this->normalize($group, $format, $context)->shouldReturn(['code' => 'laser_sabers', 'type' => 'VARIANT', 'axis' => ['light_color'], 'values' => 'normalized_values']); }
function it_normalizes_a_variant_group_with_its_values_on_versioning_context($transNormalizer, $valuesDenormalizer, GroupInterface $group, CustomSerializer $serializer, GroupTypeInterface $groupType, AttributeInterface $attr, ProductTemplateInterface $productTemplate, ProductValueInterface $productValue1) { $groupType->getCode()->willReturn('VARIANT'); $groupType->isVariant()->willReturn(true); $group->getCode()->willReturn('lego'); $valuesData = ['name' => 'Light saber model', 'size' => '120']; $context = ['versioning' => true]; $format = 'csv'; $productTemplate->getValuesData()->willReturn($valuesData); $valuesDenormalizer->denormalize($valuesData, 'ProductValue[]', 'json')->willReturn([$productValue1]); $newContext = array_merge($context, ['with_variant_group_values' => true]); $serializer->normalize($productValue1, $format, ['entity' => 'product'] + $newContext)->willReturn(['age' => '6+']); $group->getProductTemplate()->willReturn($productTemplate); $group->getType()->willReturn($groupType); $attr->getCode()->willReturn('model'); $group->getAxisAttributes()->willReturn([$attr]); $transNormalizer->normalize($group, $format, $context)->willReturn([]); $this->setSerializer($serializer); $this->normalize($group, $format, $context)->shouldReturn(['code' => 'lego', 'type' => 'VARIANT', 'axis' => 'model', 'age' => '6+']); }
function it_is_not_attribute_removable_with_group_containing_attribute(AttributeInterface $attribute, GroupInterface $group, GroupTypeInterface $groupType, ArrayCollection $groupAttributes) { $groupType->isVariant()->willReturn(true); $groupAttributes->contains($attribute)->willReturn(true); $group->getType()->willReturn($groupType); $group->getAxisAttributes()->willReturn($groupAttributes); $group->addProduct($this)->willReturn($this); $this->addGroup($group); $this->isAttributeRemovable($attribute)->shouldReturn(false); }
function it_does_not_add_violation_when_validating_product_in_groups_with_unique_combination_of_axis_attributes($context, $productRepository, GroupInterface $tShirtVariantGroup, GroupTypeInterface $tShirtGroupType, ProductInterface $redTShirtProduct, AttributeInterface $sizeAttribute, AttributeInterface $colorAttribute, ProductValueInterface $sizeProductValue, ProductValueInterface $redColorProductValue, UniqueVariantAxis $uniqueVariantAxisConstraint) { $tShirtVariantGroup->getId()->willReturn(1); $tShirtVariantGroup->getLabel()->willReturn('TShirts'); $tShirtVariantGroup->getType()->willReturn($tShirtGroupType); $tShirtGroupType->isVariant()->willReturn(true); $tShirtVariantGroup->getAxisAttributes()->willReturn([$sizeAttribute, $colorAttribute]); $sizeAttribute->getCode()->willReturn('size'); $sizeAttribute->isBackendTypeReferenceData()->willReturn(false); $colorAttribute->getCode()->willReturn('color'); $colorAttribute->isBackendTypeReferenceData()->willReturn(false); $redTShirtProduct->getVariantGroup()->willReturn($tShirtVariantGroup); $redTShirtProduct->getId()->willReturn(1); $redTShirtProduct->getValue('size')->willReturn($sizeProductValue); $redTShirtProduct->getValue('color')->willReturn($redColorProductValue); $sizeProductValue->getOption()->willReturn('XL'); $redColorProductValue->getOption()->willReturn('Red'); $criteria = [['attribute' => $sizeAttribute, 'option' => 'XL'], ['attribute' => $colorAttribute, 'option' => 'Red']]; $productRepository->findAllForVariantGroup($tShirtVariantGroup, $criteria)->willReturn([]); $context->buildViolation()->shouldNotBeCalled(Argument::cetera()); $this->validate($redTShirtProduct, $uniqueVariantAxisConstraint); }
/** * @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)); } } }
/** * @param GroupInterface $variantGroup * * @return array */ protected function getAxisAttributeCodes($variantGroup) { $axisAttributes = $variantGroup->getAxisAttributes(); $axisAttributeCodes = []; foreach ($axisAttributes as $axisAttribute) { $axisAttributeCodes[] = $axisAttribute->getCode(); } return $axisAttributeCodes; }
/** * Prepare query criteria for variant group * * @param GroupInterface $variantGroup * @param ProductInterface $product * @param Constraint $constraint * * @return array */ protected function prepareQueryCriterias(GroupInterface $variantGroup, ProductInterface $product, Constraint $constraint) { $criteria = []; foreach ($variantGroup->getAxisAttributes() as $attribute) { $value = $product->getValue($attribute->getCode()); // we don't add criteria when option is null, as this check is performed by HasVariantAxesValidator if (null === $value || null === $value->getOption() && !$attribute->isBackendTypeReferenceData()) { $this->addEmptyAxisViolation($constraint, $variantGroup->getLabel(), $product->getIdentifier()->getVarchar(), $attribute->getCode()); continue; } $current = ['attribute' => $attribute]; if (null !== $value->getOption()) { $current['option'] = $value->getOption(); } elseif ($attribute->isBackendTypeReferenceData()) { $current['referenceData'] = ['name' => $attribute->getReferenceDataName(), 'data' => $value->getData()]; } $criteria[] = $current; } return $criteria; }