/**
  * {@inheritdoc}
  */
 public function getVariant(ProductInterface $subject)
 {
     if ($subject->getVariants()->isEmpty()) {
         return null;
     }
     return $subject->getVariants()->first();
 }
 function it_does_not_add_violation_if_conflictual_product_and_validated_one_are_the_same($productRepository, ProductInterface $product, $context)
 {
     $constraint = new ProductUnique(array('property' => 'name', 'message' => 'Product with given name already exists'));
     $product->getName()->willReturn('iPhone');
     $productRepository->findOneBy(array('name' => 'iPhone'))->shouldBeCalled()->willReturn($product);
     $context->addViolationAt(Argument::any())->shouldNotBeCalled();
     $this->validate($product, $constraint);
 }
 function it_transforms_product_associations_to_array(ProductAssociationInterface $productAssociation, ProductAssociationTypeInterface $productAssociationType, ProductInterface $firstAssociatedProduct, ProductInterface $secondAssociatedProduct)
 {
     $productAssociation->getType()->willReturn($productAssociationType);
     $productAssociation->getAssociatedProducts()->willReturn(new ArrayCollection([$firstAssociatedProduct->getWrappedObject(), $secondAssociatedProduct->getWrappedObject()]));
     $firstAssociatedProduct->getId()->willReturn(7);
     $secondAssociatedProduct->getId()->willReturn(21);
     $productAssociationType->getCode()->willReturn('accessories');
     $this->transform([$productAssociation])->shouldReturn(['accessories' => '7,21']);
 }
示例#4
0
 function it_assigns_prototype_attributes_and_options_to_product($attributeValueRepository, PrototypeInterface $prototype, ProductInterface $product, AttributeInterface $attribute, AttributeValueInterface $attributeValue, OptionInterface $option)
 {
     $prototype->getAttributes()->willReturn(array($attribute))->shouldBeCalled();
     $prototype->getOptions()->willReturn(array($option))->shouldBeCalled();
     $attributeValueRepository->createNew()->shouldBeCalled()->willReturn($attributeValue);
     $attributeValue->setAttribute($attribute)->shouldBeCalled();
     $product->addAttribute($attributeValue)->shouldBeCalled();
     $product->addOption($option)->shouldBeCalled();
     $this->build($prototype, $product);
 }
 function it_add_violation_if_product_is_simple_and_code_has_been_used_in_other_product_variant(ExecutionContextInterface $context, ProductInterface $product, ProductVariantInterface $existingProductVariant, ProductVariantRepositoryInterface $productVariantRepository, ConstraintViolationBuilderInterface $constraintViolationBuilder)
 {
     $constraint = new UniqueSimpleProductCode(['message' => 'Simple product code has to be unique']);
     $product->isSimple()->willReturn(true);
     $product->getCode()->willReturn('AWESOME_PRODUCT');
     $context->buildViolation('Simple product code has to be unique', Argument::cetera())->willReturn($constraintViolationBuilder);
     $constraintViolationBuilder->atPath('code')->shouldBeCalled()->willReturn($constraintViolationBuilder);
     $constraintViolationBuilder->addViolation()->shouldBeCalled()->willReturn($constraintViolationBuilder);
     $productVariantRepository->findOneBy(['code' => 'AWESOME_PRODUCT'])->willReturn($existingProductVariant);
     $this->validate($product, $constraint);
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function build(PrototypeInterface $prototype, ProductInterface $product)
 {
     foreach ($prototype->getAttributes() as $attribute) {
         $attributeValue = $this->attributeValueRepository->createNew();
         $attributeValue->setAttribute($attribute);
         $product->addAttribute($attributeValue);
     }
     foreach ($prototype->getOptions() as $option) {
         $product->addOption($option);
     }
 }
示例#7
0
 function it_creates_new_product_from_archetype(FactoryInterface $factory, ProductInterface $product, RepositoryInterface $archetypeRepository, ArchetypeBuilderInterface $archetypeBuilder, ArchetypeInterface $archetype, VariantInterface $variant, FactoryInterface $variantFactory)
 {
     $variantFactory->createNew()->willReturn($variant);
     $variant->setMaster(true)->shouldBeCalled();
     $product->setMasterVariant($variant)->shouldBeCalled();
     $factory->createNew()->willReturn($product);
     $archetypeRepository->findOneBy(array('code' => 'book'))->willReturn($archetype);
     $product->setArchetype($archetype)->shouldBeCalled();
     $archetypeBuilder->build($product)->shouldBeCalled();
     $this->createFromArchetype('book')->shouldReturn($product);
 }
 /**
  * @param ProductOptionValueInterface[] $optionValues
  *
  * @return ProductVariantInterface|null
  */
 private function matches(array $optionValues)
 {
     foreach ($this->product->getVariants() as $variant) {
         foreach ($optionValues as $optionValue) {
             if (null === $optionValue || !$variant->hasOptionValue($optionValue)) {
                 continue 2;
             }
         }
         return $variant;
     }
     return null;
 }
 /**
  * {@inheritdoc}
  */
 public function checkParity(ProductVariantInterface $variant, ProductInterface $product)
 {
     foreach ($product->getVariants() as $existingVariant) {
         // This check is require, because this function has to look for any other different variant with same option values set
         if ($variant === $existingVariant || count($variant->getOptionValues()) !== count($product->getOptions())) {
             continue;
         }
         if ($this->matchOptions($variant, $existingVariant)) {
             return true;
         }
     }
     return false;
 }
示例#10
0
 /**
  * {@inheritdoc}
  */
 public function addAttribute($name, $value, $presentation = null)
 {
     $attribute = $this->attributeRepository->findOneBy(array('name' => $name));
     if (null === $attribute) {
         $attribute = $this->attributeRepository->createNew();
         $attribute->setName($name);
         $attribute->setPresentation($presentation ?: $name);
         $this->productManager->persist($attribute);
         $this->productManager->flush($attribute);
     }
     $attributeValue = $this->attributeValueRepository->createNew();
     $attributeValue->setAttribute($attribute);
     $attributeValue->setValue($value);
     $this->product->addAttribute($attributeValue);
     return $this;
 }
 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)
 {
     $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');
     $productVariantFactory->createForProduct($productVariable)->willReturn($permutationVariant);
     $permutationVariant->addOptionValue(Argument::type(ProductOptionValueInterface::class))->shouldBeCalled();
     $productVariable->addVariant($permutationVariant)->shouldBeCalled();
     $this->generate($productVariable);
 }
示例#12
0
 /**
  * {@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);
         $product->addVariant($variant);
     }
 }
 /**
  * @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;
 }
 /**
  * {@inheritdoc}
  */
 public function generate(ProductInterface $variable)
 {
     if (!$variable->hasOptions()) {
         throw new \InvalidArgumentException('Cannot generate variants for an object without options.');
     }
     $optionSet = [];
     $optionMap = [];
     foreach ($variable->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($variable, $optionMap, $permutation);
         $variable->addVariant($variant);
     }
 }
示例#15
0
 /**
  * {@inheritdoc}
  */
 public function hasProductOutOfStockValidationMessage(ProductInterface $product)
 {
     $message = sprintf('%s does not have sufficient stock.', $product->getName());
     if (!$this->hasElement('validation-errors')) {
         return false;
     }
     return $this->getElement('validation-errors')->getText() === $message;
 }
 function it_does_not_reverse_transform_variable_with_variants_if_options_are_missing(ProductInterface $variable, ProductVariantInterface $variant)
 {
     $variable->getVariants()->willReturn([$variant]);
     $this->reverseTransform([null])->shouldReturn(null);
 }
示例#17
0
 /**
  * @Given /^(this product) should have ([^"]+) "([^"]+)"$/
  */
 public function thisItemShouldHaveOptionValue(ProductInterface $product, $optionName, $optionValue)
 {
     Assert::true($this->summaryPage->hasItemWithOptionValue($product->getName(), $optionName, $optionValue), sprintf('Product in cart "%s" should have option %s with value %s, but it has not.', $product->getName(), $optionName, $optionValue));
 }
 function it_returns_null_if_first_variant_is_not_defined(Collection $variants, ProductInterface $product)
 {
     $product->getVariants()->willReturn($variants);
     $variants->isEmpty()->willReturn(true);
     $this->getVariant($product)->shouldReturn(null);
 }