function it_normalizes_an_existing_product_into_mongodb_document($mongoFactory, $serializer, ProductInterface $product, \MongoId $mongoId, \MongoDate $mongoDate, Association $assoc1, Association $assoc2, CategoryInterface $category1, CategoryInterface $category2, GroupInterface $group1, GroupInterface $group2, ProductValueInterface $value1, ProductValueInterface $value2, FamilyInterface $family)
 {
     $mongoFactory->createMongoId('product1')->willReturn($mongoId);
     $mongoFactory->createMongoDate()->willReturn($mongoDate);
     $family->getId()->willReturn(36);
     $category1->getId()->willReturn(12);
     $category2->getId()->willReturn(34);
     $group1->getId()->willReturn(56);
     $group2->getId()->willReturn(78);
     $product->getId()->willReturn('product1');
     $product->getCreated()->willReturn(null);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroups()->willReturn([$group1, $group2]);
     $product->getCategories()->willReturn([$category1, $category2]);
     $product->getAssociations()->willReturn([$assoc1, $assoc2]);
     $product->getValues()->willReturn([$value1, $value2]);
     $context = ['_id' => $mongoId];
     $serializer->normalize($product, 'mongodb_json')->willReturn(['data' => 'data', 'completenesses' => 'completenesses']);
     $serializer->normalize($value1, 'mongodb_document', $context)->willReturn('my_value_1');
     $serializer->normalize($value2, 'mongodb_document', $context)->willReturn('my_value_2');
     $serializer->normalize($assoc1, 'mongodb_document', $context)->willReturn('my_assoc_1');
     $serializer->normalize($assoc2, 'mongodb_document', $context)->willReturn('my_assoc_2');
     $this->normalize($product, 'mongodb_document')->shouldReturn(['_id' => $mongoId, 'created' => $mongoDate, 'updated' => $mongoDate, 'family' => 36, 'enabled' => true, 'groupIds' => [56, 78], 'categoryIds' => [12, 34], 'associations' => ['my_assoc_1', 'my_assoc_2'], 'values' => ['my_value_1', 'my_value_2'], 'normalizedData' => ['data' => 'data'], 'completenesses' => []]);
 }
 /**
  * Validate product
  *
  * @param ProductInterface $product
  * @param Constraint       $constraint
  */
 protected function validateProduct(ProductInterface $product, Constraint $constraint)
 {
     if (null === $product->getGroups()) {
         return;
     }
     foreach ($product->getGroups() as $variantGroup) {
         if ($variantGroup->getType()->isVariant()) {
             $criteria = $this->prepareQueryCriterias($variantGroup, $product);
             $matchingProducts = $this->getMatchingProducts($variantGroup, $product, $criteria);
             if (count($matchingProducts) !== 0) {
                 $values = array();
                 foreach ($criteria as $item) {
                     $values[] = sprintf('%s: %s', $item['attribute']->getCode(), (string) $item['option']);
                 }
                 $this->addViolation($constraint, $variantGroup->getLabel(), implode(', ', $values));
             }
         }
     }
 }
 function it_does_not_validate_products_with_multiple_variant_group($context, $onlyOneVariantGroup, ProductInterface $mug, CustomGroupInterface $mugVariantGroup, CustomGroupInterface $otherGroup, GroupTypeInterface $variantType)
 {
     $mug->getGroups()->willReturn([$mugVariantGroup, $otherGroup]);
     $mugVariantGroup->getType()->willReturn($variantType);
     $mugVariantGroup->__toString()->willReturn('mug');
     $otherGroup->getType()->willReturn($variantType);
     $otherGroup->__toString()->willReturn('other');
     $variantType->isVariant()->willReturn(true);
     $mug->getIdentifier()->willReturn('mug');
     $context->addViolation($onlyOneVariantGroup->message, ['%groups%' => 'mug, other', '%product%' => 'mug'])->shouldBeCalled();
     $this->validate($mug, $onlyOneVariantGroup);
 }
 function it_sets_variant_group_field($groupRepository, ProductInterface $product, GroupInterface $shirt, GroupInterface $cross, GroupInterface $up, GroupTypeInterface $variantType, GroupTypeInterface $nonVariantType)
 {
     $groupRepository->findOneByIdentifier('shirt')->willReturn($shirt);
     $shirt->getType()->willReturn($variantType);
     $variantType->isVariant()->willReturn(true);
     $product->getGroups()->willReturn([$up, $cross]);
     $up->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $cross->getType()->willReturn($variantType);
     $variantType->isVariant()->willReturn(true);
     $product->removeGroup($cross)->shouldBeCalled();
     $product->addGroup($shirt)->shouldBeCalled();
     $this->setFieldData($product, 'variant_group', 'shirt');
 }
 function it_normalizes_product(SerializerInterface $serializer, ProductInterface $product, FamilyInterface $family, Completeness $completeness)
 {
     $serializer->implement('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
     $this->setSerializer($serializer);
     $product->getFamily()->willReturn($family);
     $product->getGroups()->willReturn([]);
     $product->getValues()->willReturn([]);
     $product->getCompletenesses()->willReturn([$completeness]);
     $product->getCreated()->willReturn(null);
     $product->getUpdated()->willReturn(null);
     $product->isEnabled()->willReturn(true);
     $serializer->normalize($family, 'mongodb_json', [])->willReturn('family normalization');
     $serializer->normalize($completeness, 'mongodb_json', [])->willReturn(['completenessCode' => 'completeness normalization']);
     $this->normalize($product, 'mongodb_json', [])->shouldReturn([ProductNormalizer::FAMILY_FIELD => 'family normalization', ProductNormalizer::COMPLETENESSES_FIELD => array('completenessCode' => 'completeness normalization'), ProductNormalizer::ENABLED_FIELD => true]);
 }
 function it_sets_groups_field($groupRepository, ProductInterface $product, GroupInterface $pack, GroupInterface $cross, GroupInterface $up, GroupTypeInterface $nonVariantType)
 {
     $groupRepository->findOneByIdentifier('pack')->willReturn($pack);
     $groupRepository->findOneByIdentifier('cross')->willReturn($cross);
     $product->getGroups()->willReturn([$up]);
     $up->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $product->removeGroup($up)->shouldBeCalled();
     $pack->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $cross->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $product->addGroup($pack)->shouldBeCalled();
     $product->addGroup($cross)->shouldBeCalled();
     $this->setFieldData($product, 'groups', ['pack', 'cross']);
 }
 /**
  * Returns the required attribute codes for a product
  *
  * @param ProductInterface $product
  *
  * @return array
  */
 public function getRequiredAttributeCodes(ProductInterface $product)
 {
     $codes = array();
     if ($product->getFamily()) {
         $codes = $this->getFamilyAttributeCodes($product->getFamily());
     }
     foreach ($product->getGroups() as $group) {
         $codes = array_merge($codes, $this->getGroupAttributeCodes($group));
     }
     if ($product->getId()) {
         foreach ($product->getValues() as $value) {
             $codes[] = $value->getAttribute()->getCode();
         }
     }
     return array_unique($codes);
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["group_code"]
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $groups = [];
     foreach ($data as $groupCode) {
         $group = $this->groupRepository->findOneByIdentifier($groupCode);
         if (null === $group) {
             throw InvalidArgumentException::expected($field, 'existing group code', 'setter', 'groups', $groupCode);
         } else {
             $groups[] = $group;
         }
     }
     $oldGroups = $product->getGroups();
     foreach ($oldGroups as $group) {
         $product->removeGroup($group);
     }
     foreach ($groups as $group) {
         $product->addGroup($group);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : "variant_group_code"
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     if (null !== $data) {
         $variantGroup = $this->groupRepository->findOneByIdentifier($data);
         if (null === $variantGroup) {
             throw InvalidArgumentException::expected($field, 'existing variant group code', 'setter', 'variant_group', $data);
         }
         if (!$variantGroup->getType()->isVariant()) {
             throw InvalidArgumentException::expected($field, 'variant group code', 'setter', 'variant_group', $data);
         }
     }
     $existingGroups = $product->getGroups();
     foreach ($existingGroups as $group) {
         if ($group->getType()->isVariant()) {
             $product->removeGroup($group);
         }
     }
     if (null !== $data) {
         $product->addGroup($variantGroup);
     }
 }
 /**
  * @param ProductInterface $product
  * @param array            $drupalProduct
  */
 protected function computeProductGroup(ProductInterface $product, array &$drupalProduct)
 {
     /** @var Group $group */
     foreach ($product->getGroups() as $group) {
         $drupalProduct['groups'][$group->getType()->getCode()]['code'] = $group->getCode();
         foreach ($group->getProducts() as $productInGroup) {
             if ($product->getReference() != $productInGroup->getReference()) {
                 $drupalProduct['groups'][$group->getType()->getCode()]['products'][] = $product->getReference();
             }
         }
     }
 }
 /**
  * Check if the product belongs to a variant group.
  *
  * @param ProductInterface $product
  *
  * @return boolean
  */
 protected function belongsToVariant(ProductInterface $product)
 {
     foreach ($product->getGroups() as $group) {
         if ($group->getType()->isVariant()) {
             return true;
         }
     }
     return false;
 }
 /**
  * Denormalize product groups
  *
  * @param string           $data
  * @param string           $format
  * @param array            $context
  * @param ProductInterface $product
  */
 protected function denormalizeGroups($data, $format, array $context, ProductInterface $product)
 {
     foreach ($product->getGroups() as $group) {
         $product->removeGroup($group);
     }
     $groupCodes = strlen($data) > 0 ? explode(",", $data) : array();
     foreach ($groupCodes as $groupCode) {
         $product->addGroup($this->serializer->denormalize($groupCode, $this->groupClass, $format, $context));
     }
 }
 function it_adds_a_violation_when_validating_product_in_groups_with_non_unique_combination_of_axis_attributes($context, $productRepository, GroupInterface $tShirtVariantGroup, GroupTypeInterface $tShirtGroupType, GroupInterface $clothesVariantGroup, GroupTypeInterface $clothesGroupType, ProductInterface $redTShirtProduct, AttributeInterface $sizeAttribute, AttributeInterface $colorAttribute, ProductValueInterface $sizeProductValue, ProductValueInterface $colorProductValue, ProductInterface $redTShirtProduct2, UniqueVariantAxis $uniqueVariantAxisConstraint)
 {
     $redTShirtProduct->getGroups()->willReturn([$tShirtVariantGroup, $clothesVariantGroup]);
     $tShirtVariantGroup->getId()->willReturn(1);
     $tShirtVariantGroup->getLabel()->willReturn('TShirts');
     $tShirtVariantGroup->getType()->willReturn($tShirtGroupType);
     $tShirtGroupType->isVariant()->willReturn(true);
     $tShirtVariantGroup->getAxisAttributes()->willReturn([$sizeAttribute, $colorAttribute]);
     $clothesVariantGroup->getId()->willReturn(2);
     $clothesVariantGroup->getLabel()->willReturn('Clothes');
     $clothesVariantGroup->getType()->willReturn($clothesGroupType);
     $clothesGroupType->isVariant()->willReturn(true);
     $clothesVariantGroup->getAxisAttributes()->willReturn([$sizeAttribute, $colorAttribute]);
     $sizeAttribute->getCode()->willReturn('size');
     $colorAttribute->getCode()->willReturn('color');
     $redTShirtProduct->getValue('size')->willReturn($sizeProductValue);
     $redTShirtProduct->getValue('color')->willReturn($colorProductValue);
     $redTShirtProduct->getId()->willReturn(1);
     $sizeProductValue->getOption()->willReturn('XL');
     $colorProductValue->getOption()->willReturn('Red');
     $redTShirtProduct2->getGroups()->willReturn([$clothesVariantGroup]);
     $redTShirtProduct2->getValue('size')->willReturn($sizeProductValue);
     $redTShirtProduct2->getValue('color')->willReturn($colorProductValue);
     $redTShirtProduct2->getId()->willReturn(2);
     $criteria = [['attribute' => $sizeAttribute, 'option' => 'XL'], ['attribute' => $colorAttribute, 'option' => 'Red']];
     $productRepository->findAllForVariantGroup($tShirtVariantGroup, $criteria)->willReturn([]);
     $productRepository->findAllForVariantGroup($clothesVariantGroup, $criteria)->willReturn([$redTShirtProduct2]);
     $context->addViolation('Group "%variant group%" already contains another product with values "%values%"', ['%variant group%' => 'Clothes', '%values%' => 'size: XL, color: Red'])->shouldBeCalled();
     $this->validate($redTShirtProduct, $uniqueVariantAxisConstraint);
 }
 function it_associates_only_products_in_channel_to_configurable($clientParametersRegistry, $normalizerGuesser, $groupManager, $channelManager, $productFilter, Group $group, Channel $channel, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, MagentoSoapClientParameters $clientParameters, WebserviceGuesser $webserviceGuesser, Webservice $webservice, ProductNormalizer $productNormalizer, ConfigurableNormalizer $configurableNormalizer, GroupRepository $groupRepository, StepExecution $stepExecution, EventDispatcher $eventDispatcher)
 {
     $clientParametersRegistry->getInstance(Argument::cetera())->willReturn($clientParameters);
     $webserviceGuesser->getWebservice($clientParameters)->willReturn($webservice);
     $normalizerGuesser->getProductNormalizer(Argument::cetera())->willReturn($productNormalizer);
     $normalizerGuesser->getConfigurableNormalizer(Argument::cetera())->willReturn($configurableNormalizer);
     $webservice->getStoreViewsList()->willReturn([['store_id' => '1', 'code' => 'default', 'website_id' => '1', 'group_id' => '1', 'name' => 'Default Store View', 'sort_order' => '0', 'is_active' => '1']]);
     $webservice->getAllAttributes()->willReturn(['name' => ['attribute_id' => '71', 'code' => 'name', 'type' => 'text', 'required' => '1', 'scope' => 'store']]);
     $webservice->getAllAttributesOptions()->willReturn([]);
     $webservice->getConfigurablesStatus([1 => ['group' => $group, 'products' => [$product1, $product2]]])->willReturn([['product_id' => '6', 'sku' => 'conf-groupCode', 'name' => 'foo', 'set' => '3', 'type' => 'configurable', 'category_ids' => ['7'], 'website_ids' => ['1']]]);
     $groupManager->getRepository()->willReturn($groupRepository);
     $groupRepository->getVariantGroupIds()->willReturn([1]);
     $channelManager->getChannelByCode('magento')->willReturn($channel);
     $channel->getId()->willReturn(3);
     $group->getId()->willReturn(1);
     $group->getProducts()->willReturn([$product1, $product2, $product3]);
     $group->getCode()->willReturn('groupCode');
     $product1->getGroups()->willReturn([$group]);
     $product1->getId()->willReturn(10);
     $product2->getId()->willReturn(11);
     $product3->getId()->willReturn(12);
     $productFilter->apply($channel, [$product1, $product2, $product3])->willReturn([$product1, $product2]);
     $configurableNormalizer->normalize(['group' => $group, 'products' => [$product1, $product2]], 'MagentoArray', Argument::type('array'))->shouldBeCalled();
     $this->setStepExecution($stepExecution);
     $this->setEventDispatcher($eventDispatcher);
     $this->setChannel('magento');
     $this->process([$product1]);
 }