function it_adds_violation_if_variant_with_given_same_options_already_exists(ExecutionContextInterface $context, ProductInterface $product, ProductVariantInterface $variant, ProductVariantsParityCheckerInterface $variantsParityChecker) { $constraint = new ProductVariantCombination(['message' => 'Variant with given options already exists']); $variant->getProduct()->willReturn($product); $product->hasVariants()->willReturn(true); $product->hasOptions()->willReturn(true); $variantsParityChecker->checkParity($variant, $product)->willReturn(true); $context->addViolation('Variant with given options already exists', Argument::any())->shouldBeCalled(); $this->validate($variant, $constraint); }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$value instanceof ProductVariantInterface) { throw new UnexpectedTypeException($value, ProductVariantInterface::class); } $product = $value->getProduct(); if (!$product->hasVariants() || !$product->hasOptions()) { return; } if ($this->variantsParityChecker->checkParity($value, $product)) { $this->context->addViolation($constraint->message); } }
function it_generates_variants_for_every_possible_permutation_of_an_objects_options_and_option_values(ProductInterface $productVariable, ProductOptionInterface $colorOption, ProductOptionInterface $sizeOption, ProductOptionValueInterface $blackColor, ProductOptionValueInterface $largeSize, ProductOptionValueInterface $mediumSize, ProductOptionValueInterface $redColor, ProductOptionValueInterface $smallSize, ProductOptionValueInterface $whiteColor, ProductVariantFactoryInterface $productVariantFactory, ProductVariantInterface $permutationVariant, ProductVariantsParityCheckerInterface $variantsParityChecker) { $productVariable->hasOptions()->willReturn(true); $productVariable->getOptions()->willReturn([$colorOption, $sizeOption]); $colorOption->getValues()->willReturn([$blackColor, $whiteColor, $redColor]); $sizeOption->getValues()->willReturn([$smallSize, $mediumSize, $largeSize]); $blackColor->getId()->willReturn('black1'); $whiteColor->getId()->willReturn('white2'); $redColor->getId()->willReturn('red3'); $smallSize->getId()->willReturn('small4'); $mediumSize->getId()->willReturn('medium5'); $largeSize->getId()->willReturn('large6'); $variantsParityChecker->checkParity($permutationVariant, $productVariable)->willReturn(false); $productVariantFactory->createForProduct($productVariable)->willReturn($permutationVariant); $permutationVariant->addOptionValue(Argument::type(ProductOptionValueInterface::class))->shouldBeCalled(); $productVariable->addVariant($permutationVariant)->shouldBeCalled(); $this->generate($productVariable); }
/** * {@inheritdoc} */ public function generate(ProductInterface $product) { Assert::true($product->hasOptions(), 'Cannot generate variants for an object without options.'); $optionSet = []; $optionMap = []; foreach ($product->getOptions() as $key => $option) { foreach ($option->getValues() as $value) { $optionSet[$key][] = $value->getId(); $optionMap[$value->getId()] = $value; } } $permutations = $this->setBuilder->build($optionSet); foreach ($permutations as $permutation) { $variant = $this->createVariant($product, $optionMap, $permutation); if (!$this->variantsParityChecker->checkParity($variant, $product)) { $product->addVariant($variant); } } }