function it_should_add_violation_if_variant_with_given_same_options_already_exists(ProductVariantInterface $variant, ProductVariantInterface $existingVariant, ProductInterface $variable, ProductOptionValueInterface $option, $context) { $constraint = new ProductVariantCombination(['message' => 'Variant with given options already exists']); $variant->getProduct()->willReturn($variable); $variant->getOptions()->willReturn(new ArrayCollection([$option->getWrappedObject()])); $existingVariant->hasOption($option)->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) { continue; } $matches = true; if (!$variant->getOptions()->count()) { continue; } foreach ($variant->getOptions() as $option) { if (!$existingVariant->hasOption($option)) { $matches = false; } } if ($matches) { return true; } } return false; }
function it_should_transform_variant_into_variant_options(ProductVariantInterface $variant, Collection $options) { $variant->getOptions()->willReturn($options); $options->toArray()->willReturn([]); $this->transform($variant)->shouldReturn([]); }