function it_fails_if_one_of_the_associated_group_does_not_exist($productBuilder, $groupRepository, ProductInterface $product, AssociationInterface $xsellAssociation)
 {
     $product->getAssociations()->willReturn([$xsellAssociation]);
     $productBuilder->addMissingAssociations($product)->shouldBeCalled();
     $product->getAssociationForTypeCode('xsell')->willReturn($xsellAssociation);
     $groupRepository->findOneByIdentifier('not existing group')->willReturn(null);
     $this->shouldThrow(InvalidArgumentException::expected('associations', 'existing group code', 'adder', 'association', 'not existing group'))->during('addFieldData', [$product, 'associations', ['xsell' => ['groups' => ['not existing group'], 'products' => []]]]);
 }
 function it_hydrates_a_result_record(Builder $builder, Query $query, ProductInterface $product, Association $association, AssociationType $associationType, ProductInterface $associatedProduct1, ProductInterface $associatedProduct2, ArrayCollection $productsCollection, ArrayCollection $productIdsCollection, ArrayIterator $productsIterator, ArrayCollection $associationsCollection, ArrayIterator $associationsIterator, ArrayIterator $arrayIterator)
 {
     $product->getId()->willReturn('110ae6b98ead0ee8778b46bb');
     $options = ['locale_code' => 'en_US', 'scope_code' => 'print', 'current_group_id' => null, 'attributes_configuration' => [], 'association_type_id' => 1, 'current_product' => $product];
     $builder->find()->willReturn($builder);
     $builder->count()->willReturn($builder);
     $builder->getQuery()->willReturn($query);
     $builder->hydrate(false)->willReturn($builder);
     $builder->setQueryArray(Argument::any())->willReturn($builder);
     $builder->limit(Argument::any())->willReturn($builder);
     $builder->skip(Argument::any())->willReturn($builder);
     $product->getAssociations()->willReturn($associationsCollection);
     $associationsCollection->getIterator()->willReturn($associationsIterator);
     $associationsIterator->rewind()->shouldBeCalled();
     $associationsCount = 1;
     $associationsIterator->valid()->will(function () use(&$associationsCount) {
         return $associationsCount-- > 0;
     });
     $associationsIterator->next()->shouldBeCalled();
     $associationsIterator->current()->will(new ReturnPromise([$association]));
     $associationsCollection->filter(Argument::any())->willReturn($associationsIterator);
     $associationsIterator->first()->willReturn($association);
     $association->getAssociationType()->willReturn($associationType);
     $associationType->getId()->willReturn(1);
     $associatedProduct1->getId()->willReturn('220ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('330ae6b98abd0ec8778b46bb');
     $association->getProducts()->willReturn($productsCollection);
     $productsCollection->getIterator()->willReturn($productsIterator);
     $productsIterator->rewind()->shouldBeCalled();
     $productsCount = 2;
     $productsIterator->valid()->will(function () use(&$productsCount) {
         return $productsCount-- > 0;
     });
     $associatedProduct1->getId()->willReturn('220ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('330ae6b98abd0ec8778b46bb');
     $association->getProducts()->willReturn($productsCollection);
     $productsCollection->getIterator()->willReturn($productsIterator);
     $productsIterator->rewind()->shouldBeCalled();
     $productsCount = 2;
     $productsIterator->valid()->will(function () use(&$productsCount) {
         return $productsCount-- > 0;
     });
     $productsIterator->next()->shouldBeCalled();
     $productsIterator->current()->will(new ReturnPromise([$associatedProduct1, $associatedProduct2]));
     $productsCollection->map(Argument::any())->willReturn($productIdsCollection);
     $productIdsCollection->toArray()->willReturn(['220ae6b98ead0ed7778b46bb', '330ae6b98abd0ec8778b46bb']);
     $queryDefinition = ['type' => 1, 'sort' => ['normalizedData.is_associated' => -1, '_id' => 1], 'limit' => 10, 'skip' => 0, 'query' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '110ae6b98ead0ee8778b46bb'])]], 'newObj' => []];
     $query->getQuery()->willReturn($queryDefinition);
     $fixture = ['_id' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb']), 'normalizedData' => [], 'sku' => ['attribute' => ['code' => 'sku', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => null, 'scope' => null, 'value' => 'mysku'], 'name' => ['attribute' => ['code' => 'name', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => null], 'desc' => ['attribute' => ['code' => 'desc', 'attributeType' => 'text', 'backendType' => 'text'], 'locale' => 'fr_FR', 'scope' => 'print'], 'is_associated' => 1];
     $query->execute()->willReturn($arrayIterator);
     $arrayIterator->toArray()->willReturn([$fixture]);
     $rows = $this->hydrate($builder, $options);
     $rows->shouldHaveCount(1);
     $firstResult = $rows[0];
     $firstResult->shouldBeAnInstanceOf('\\Oro\\Bundle\\DataGridBundle\\Datasource\\ResultRecord');
 }
 /**
  * Clear associations (remove groups and products from existing associations)
  *
  * @param ProductInterface $product
  */
 protected function clearAssociations(ProductInterface $product)
 {
     foreach ($product->getAssociations() as $association) {
         foreach ($association->getGroups() as $group) {
             $association->removeGroup($group);
         }
         foreach ($association->getProducts() as $prod) {
             $association->removeProduct($prod);
         }
     }
 }
 /**
  * {@inheritdoc}
  *
  * @param ProductInterface $object
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $context = $this->resolveContext($context);
     $results = $this->serializer->normalize($object->getIdentifier(), $format, $context);
     $results[self::FIELD_FAMILY] = $this->normalizeFamily($object->getFamily());
     $results[self::FIELD_GROUPS] = $this->normalizeGroups($object->getGroupCodes());
     $results[self::FIELD_CATEGORY] = $this->normalizeCategories($object->getCategoryCodes());
     $results = array_merge($results, $this->normalizeAssociations($object->getAssociations()));
     $results = array_replace($results, $this->normalizeValues($object, $format, $context));
     $results[self::FIELD_ENABLED] = (int) $object->isEnabled();
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function findMissingAssociationTypes(ProductInterface $product)
 {
     $qb = $this->createQueryBuilder('a');
     if ($associations = $product->getAssociations()) {
         $associationTypeIds = $associations->map(function ($association) {
             return $association->getAssociationType()->getId();
         });
         if (!$associationTypeIds->isEmpty()) {
             $qb->andWhere($qb->expr()->notIn('a.id', $associationTypeIds->toArray()));
         }
     }
     return $qb->getQuery()->getResult();
 }
 function it_massively_insert_and_update_objects($bulkSaver, $bulkDetacher, $stepExecution, ProductInterface $product1, ProductInterface $product2, AssociationInterface $association2)
 {
     $bulkSaver->saveAll([$product1, $product2]);
     $bulkDetacher->detachAll([$product1, $product2]);
     $product1->getId()->willReturn(null);
     $association1 = new Association();
     $product1->getAssociations()->willReturn(new ArrayCollection([$association1]));
     $stepExecution->incrementSummaryInfo('process')->shouldBeCalled();
     $product2->getId()->willReturn(42);
     $association2->getId()->willReturn(1);
     $product2->getAssociations()->willReturn(new ArrayCollection([$association2]));
     $stepExecution->incrementSummaryInfo('process')->shouldBeCalled();
     $this->write([$product1, $product2]);
 }
 /**
  * {@inheritdoc}
  *
  * @param ProductInterface $product
  */
 public function normalize($product, $format = null, array $context = [])
 {
     $data = [];
     foreach ($product->getAssociations() as $association) {
         $code = $association->getAssociationType()->getCode();
         $data[$code] = ['groups' => [], 'products' => []];
         foreach ($association->getGroups() as $group) {
             $data[$code]['groups'][] = $group->getCode();
         }
         foreach ($association->getProducts() as $product) {
             $data[$code]['products'][] = $product->getReference();
         }
     }
     ksort($data);
     return $data;
 }
 function it_normalizes_the_associations_of_the_product(ProductInterface $product, AssociationInterface $association1, AssociationInterface $association2, AssociationTypeInterface $type1, AssociationTypeInterface $type2, ProductInterface $productAssociated1, ProductInterface $productAssociated2, ProductInterface $productAssociated3, GroupInterface $groupAssociated1, GroupInterface $groupAssociated2)
 {
     $type1->getCode()->willReturn('wahou the type');
     $type2->getCode()->willReturn('such a type');
     $groupAssociated1->getCode()->willReturn('group 1');
     $groupAssociated2->getCode()->willReturn('group 2');
     $productAssociated1->getReference()->willReturn('product 1');
     $productAssociated2->getReference()->willReturn('product 2');
     $productAssociated3->getReference()->willReturn('product 3');
     $association1->getAssociationType()->willReturn($type1);
     $association2->getAssociationType()->willReturn($type2);
     $association1->getGroups()->willReturn([$groupAssociated1]);
     $association2->getGroups()->willReturn([$groupAssociated1, $groupAssociated2]);
     $association1->getProducts()->willReturn([]);
     $association2->getProducts()->willReturn([$productAssociated1, $productAssociated2, $productAssociated3]);
     $product->getAssociations()->willReturn([$association1, $association2]);
     $this->normalize($product, 'json')->shouldReturn(['such a type' => ['groups' => ['group 1', 'group 2'], 'products' => ['product 1', 'product 2', 'product 3']], 'wahou the type' => ['groups' => ['group 1'], 'products' => []]]);
 }
 /**
  * @param array                 $result
  * @param string                $associationTypeId
  * @param ProductInterface|null $product
  *
  * @return array
  */
 public function transform(array $result, $associationTypeId, ProductInterface $product = null)
 {
     if ($product) {
         $associationTypeId = (int) $associationTypeId;
         $result['is_associated'] = false;
         $currentAssociation = $product->getAssociations()->filter(function ($association) use($associationTypeId) {
             return $association->getAssociationType()->getId() === $associationTypeId;
         })->first();
         if ($currentAssociation) {
             $associatedIds = $currentAssociation->getProducts()->map(function ($product) {
                 return $product->getId();
             })->toArray();
             if (in_array($result['id'], $associatedIds)) {
                 $result['is_associated'] = true;
             }
         }
         $result['is_checked'] = $result['is_associated'];
     }
     return $result;
 }
 function it_normalizes_the_values_of_product(ProductInterface $product, AttributeInterface $attribute, ProductValueInterface $value, ArrayCollection $values, \ArrayIterator $iterator, $filter, $serializer)
 {
     $values->getIterator()->willReturn($iterator);
     $product->getAssociations()->willReturn([]);
     $product->getFamily()->willReturn(null);
     $product->getGroupCodes()->willReturn([]);
     $product->getCategoryCodes()->willReturn([]);
     $product->isEnabled()->willReturn(true);
     $value->getAttribute()->willReturn($attribute);
     $attribute->getCode()->willReturn('name');
     $product->getValues()->willReturn($values);
     $filter->filter($values, Argument::any())->shouldBeCalled()->willReturn($values);
     $iterator->rewind()->willReturn(null);
     $valueCount = 1;
     $iterator->valid()->will(function () use(&$valueCount) {
         return $valueCount-- > 0;
     });
     $iterator->current()->willReturn($value);
     $iterator->next()->willReturn(null);
     $serializer->normalize($value, 'json', Argument::any())->willReturn(['locale' => null, 'scope' => null, 'value' => 'foo']);
     $this->normalize($product, 'json')->shouldReturn(['family' => null, 'groups' => [], 'categories' => [], 'enabled' => true, 'associations' => [], 'values' => ['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']]]]);
 }
 function it_normalizes_an_existing_product_without_family_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)
 {
     $mongoFactory->createMongoId('product1')->willReturn($mongoId);
     $mongoFactory->createMongoDate()->willReturn($mongoDate);
     $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(null);
     $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, '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' => []]);
 }
 function it_normalizes_a_product_with_no_associations(ProductInterface $product)
 {
     $product->getAssociations()->willReturn([]);
     $this->normalize($product, 'standard')->shouldReturn([]);
 }
 function it_normalizes_product_with_price($filter, ProductInterface $product, AttributeInterface $priceAttribute, ProductValueInterface $price, Collection $prices, Collection $values, ProductPriceInterface $productPrice, FamilyInterface $family, SerializerInterface $serializer)
 {
     $family->getCode()->willReturn('shoes');
     $priceAttribute->getCode()->willReturn('price');
     $priceAttribute->getAttributeType()->willReturn('pim_catalog_price_collection');
     $priceAttribute->isLocalizable()->willReturn(false);
     $priceAttribute->isScopable()->willReturn(false);
     $price->getAttribute()->willReturn($priceAttribute);
     $price->getData()->willReturn(null);
     $productPrice->getData()->willReturn("356.00");
     $productPrice->getCurrency()->willReturn("EUR");
     $prices->add($productPrice);
     $price->getPrices()->willReturn($prices);
     $product->getIdentifier()->willReturn($price);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroupCodes()->willReturn(['group1', 'group2', 'variant_group_1']);
     $product->getCategoryCodes()->willReturn(['nice shoes', 'converse']);
     $product->getAssociations()->willReturn([]);
     $values->add($price);
     $product->getValues()->willReturn($values);
     $filter->filterCollection($values, 'pim.transform.product_value.flat', Argument::cetera())->willReturn([$price]);
     $serializer->normalize($price, 'flat', Argument::any())->willReturn(['price-EUR' => '356.00']);
     $this->normalize($product, 'flat', ['price-EUR' => ''])->shouldReturn(['price-EUR' => '356.00', 'family' => 'shoes', 'groups' => 'group1,group2,variant_group_1', 'categories' => 'nice shoes,converse', 'enabled' => 1]);
 }
Ejemplo n.º 14
0
 /**
  * @param ProductInterface $product
  *
  * @return array
  */
 protected function getAssociationMeta(ProductInterface $product)
 {
     $meta = [];
     $associations = $product->getAssociations();
     foreach ($associations as $association) {
         $associationType = $association->getAssociationType();
         $meta[$associationType->getCode()]['groupIds'] = array_map(function ($group) {
             return $group->getId();
         }, $association->getGroups()->toArray());
     }
     return ['associations' => $meta];
 }
 /**
  * Denormalize product associations
  *
  * @param string           &$data
  * @param string           $format
  * @param array            $context
  * @param ProductInterface $product
  *
  * @throws \RuntimeException
  */
 protected function denormalizeAssociations(&$data, $format, array $context, ProductInterface $product)
 {
     foreach ($product->getAssociations() as $association) {
         foreach ($association->getGroups() as $group) {
             $association->removeGroup($group);
         }
         foreach ($association->getProducts() as $prod) {
             $association->removeProduct($prod);
         }
     }
     // Get association field names and add associations
     $assocFieldNames = $this->assocFieldResolver->resolveAssociationColumns();
     foreach ($assocFieldNames as $assocFieldName) {
         if (isset($data[$assocFieldName])) {
             if (strlen($data[$assocFieldName]) > 0) {
                 list($associationTypeCode, $part) = explode('-', $assocFieldName);
                 $association = $product->getAssociationForTypeCode($associationTypeCode);
                 $association = $this->serializer->denormalize($data[$assocFieldName], $this->associationClass, $format, ['entity' => $association, 'association_type_code' => $associationTypeCode, 'part' => $part] + $context);
                 if (!$product->getAssociationForTypeCode($associationTypeCode)) {
                     $product->addAssociation($association);
                 }
             }
             unset($data[$assocFieldName]);
         }
     }
     foreach (array_keys($data) as $fieldName) {
         if (null !== ($matches = $this->extractAssociationFieldNameInfos($fieldName))) {
             throw new \RuntimeException(sprintf('Association type "%s" does not exist anymore', $matches['assoc_type_code']));
         }
     }
 }
 /**
  * @param ProductInterface $product
  *
  * @throws \InvalidArgumentException
  *
  * @return \Symfony\Component\Validator\ConstraintViolationListInterface|null
  */
 protected function validateProductAssociations(ProductInterface $product)
 {
     $associations = $product->getAssociations();
     foreach ($associations as $association) {
         $violations = $this->validator->validate($association);
         if ($violations->count() > 0) {
             return $violations;
         }
     }
     return null;
 }
 /**
  * Get Mongo Ids of the associated products of the current product
  *
  * @param ProductInterface $product
  * @param int              $associationTypeId
  *
  * @return \MongoId[]
  */
 protected function getAssociatedProductIds(ProductInterface $product, $associationTypeId)
 {
     $ids = [];
     foreach ($product->getAssociations() as $association) {
         if ($association->getAssociationType()->getId() !== $associationTypeId) {
             continue;
         }
         foreach ($association->getProducts() as $associatedProduct) {
             $ids[] = new \MongoId($associatedProduct->getId());
         }
     }
     return $ids;
 }