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); }
/** * 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; }