/** * @param ProductVariantInterface $variant * @param ProductVariantInterface $existingVariant * * @return bool */ private function matchOptions(ProductVariantInterface $variant, ProductVariantInterface $existingVariant) { foreach ($variant->getOptionValues() as $option) { if (!$existingVariant->hasOptionValue($option)) { return false; } } return true; }
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); }
/** * @param ProductVariantInterface $variant * @param array $optionMap * @param mixed $permutation */ private function addOptionValue(ProductVariantInterface $variant, array $optionMap, $permutation) { if (!is_array($permutation)) { $variant->addOptionValue($optionMap[$permutation]); return; } foreach ($permutation as $id) { $variant->addOptionValue($optionMap[$id]); } }
function it_adds_violation_if_variant_with_given_same_options_already_exists(ExecutionContextInterface $context, ProductInterface $variable, ProductOptionValueInterface $optionValue, ProductVariantInterface $existingVariant, ProductVariantInterface $variant) { $constraint = new ProductVariantCombination(['message' => 'Variant with given options already exists']); $variant->getProduct()->willReturn($variable); $variant->getOptionValues()->willReturn(new ArrayCollection([$optionValue->getWrappedObject()])); $existingVariant->hasOptionValue($optionValue)->willReturn(true); $variable->hasVariants()->willReturn(true); $variable->hasOptions()->willReturn(true); $variable->getVariants()->willReturn([$existingVariant]); $context->addViolation('Variant with given options already exists', Argument::any())->shouldBeCalled(); $this->validate($variant, $constraint); }
/** * @param ProductVariantInterface $variant * @param ProductInterface $variable * * @return bool */ private function matches(ProductVariantInterface $variant, ProductInterface $variable) { foreach ($variable->getVariants() as $existingVariant) { if ($variant === $existingVariant || !$variant->getOptionValues()->count()) { continue; } $matches = true; foreach ($variant->getOptionValues() as $optionValue) { if (!$existingVariant->hasOptionValue($optionValue)) { $matches = false; } } if ($matches) { return true; } } return false; }
function it_does_not_reverse_transform_variable_with_variants_if_options_not_match(ProductInterface $variable, ProductVariantInterface $variant, ProductOptionValueInterface $optionValue) { $variable->getVariants()->willReturn([$variant]); $variant->hasOptionValue($optionValue)->willReturn(false); $this->reverseTransform([$optionValue])->shouldReturn(null); }
function it_is_configurable_if_it_has_one_variant_and_at_least_one_option(ProductOptionInterface $option, ProductVariantInterface $variant) { $variant->setProduct($this)->shouldBeCalled(); $this->addVariant($variant); $this->addOption($option); $this->isConfigurable()->shouldReturn(true); $this->isSimple()->shouldReturn(false); }
function it_returns_available_variants(ProductVariantInterface $unavailableVariant, ProductVariantInterface $variant) { $unavailableVariant->isAvailable()->willReturn(false); $variant->isAvailable()->willReturn(true); $unavailableVariant->setProduct($this)->shouldBeCalled(); $variant->setProduct($this)->shouldBeCalled(); $this->addVariant($unavailableVariant); $this->addVariant($variant); $this->getAvailableVariants()->shouldHaveCount(1); $this->getAvailableVariants()->first()->shouldReturn($variant); }