function it_hydrates_a_result_record(Builder $builder, Query $query, ProductInterface $product, Association $association, AssociationType $associationType, ProductInterface $associatedProduct1, ProductInterface $associatedProduct2, DocumentManager $documentManager, \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $metadata, Collection $collection, ArrayIterator $arrayIterator)
 {
     $options = ['locale_code' => 'en_US', 'scope_code' => 'print', 'current_group_id' => null, 'attributes_configuration' => [], 'association_type_id' => 1, 'current_product' => $product];
     $builder->getQuery()->willReturn($query);
     $builder->hydrate(false)->willReturn($builder);
     $associatedProduct1->getId()->willReturn('550ae6b98ead0ed7778b46bb');
     $associatedProduct2->getId()->willReturn('550ae6b98abd0ec8778b46bb');
     $product->getAssociations()->willReturn([$association]);
     $association->getAssociationType()->willReturn($associationType);
     $associationType->getId()->willReturn(1);
     $association->getProducts()->willReturn([$associatedProduct1, $associatedProduct2]);
     $queryDefinition = ['type' => 1, 'sort' => ['normalizedData.is_associated' => -1, '_id' => 1], 'limit' => 10, 'skip' => 0, 'query' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb'])]], 'newObj' => []];
     $query->getQuery()->willReturn($queryDefinition);
     $query->getDocumentManager()->willReturn($documentManager);
     $documentManager->getDocumentCollection(Argument::any())->willReturn($collection);
     $documentManager->getClassMetadata(Argument::any())->willReturn($metadata);
     $metadata->getFieldNames()->willReturn(['id', 'created', 'updated', 'locale', 'scope', 'values', 'indexedValues', 'indexedValuesOutdated', 'family', 'familyId', 'categories', 'categoryIds', 'enabled', 'groups', 'groupIds', 'associations', 'completenesses', 'normalizedData']);
     $pipeline = [['$match' => ['_id' => ['$ne' => \MongoId::__set_state(['$id' => '550ae6b98ead0ee8778b46bb'])]]], ['$project' => ['id' => 1, 'created' => 1, 'updated' => 1, 'locale' => 1, 'scope' => 1, 'values' => 1, 'indexedValues' => 1, 'indexedValuesOutdated' => 1, 'family' => 1, 'familyId' => 1, 'categories' => 1, 'categoryIds' => 1, 'enabled' => 1, 'groups' => 1, 'groupIds' => 1, 'associations' => 1, 'completenesses' => 1, 'normalizedData' => 1, 'is_associated' => ['$cond' => [['$or' => [['$eq' => ['$_id', \MongoId::__set_state(['$id' => '550ae6b98ead0ed7778b46bb'])]], ['$eq' => ['$_id', \MongoId::__set_state(['$id' => '550ae6b98abd0ec8778b46bb'])]]]], 1, 0]]]], ['$sort' => ['is_associated' => -1]], ['$skip' => 0], ['$limit' => 10]];
     $collection->aggregate($pipeline)->willReturn($arrayIterator);
     $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];
     $arrayIterator->toArray()->willReturn([$fixture]);
     $rows = $this->hydrate($builder, $options);
     $rows->shouldHaveCount(1);
     $firstResult = $rows[0];
     $firstResult->shouldBeAnInstanceOf('\\Oro\\Bundle\\DataGridBundle\\Datasource\\ResultRecord');
 }
 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' => []]);
 }
 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' => []]]]);
 }
 /**
  * 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);
         }
     }
 }
 /**
  * @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];
 }
 /**
  * {@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]);
 }
 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;
     });
     $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');
 }
 /**
  * @param ProductInterface $currentProduct
  * @param array            $queryDefinition
  * @param string []        $productFields
  * @param int              $associationTypeId
  *
  * @return array
  */
 protected function pipelineFromQuery(ProductInterface $currentProduct, array $queryDefinition, array $productFields, $associationTypeId)
 {
     $or = [];
     foreach ($currentProduct->getAssociations() as $association) {
         if ($associationTypeId != $association->getAssociationType()->getId()) {
             continue;
         }
         foreach ($association->getProducts() as $myProduct) {
             $or[] = ['$eq' => ['$_id', new \MongoId($myProduct->getId())]];
         }
     }
     $match = $queryDefinition['query'];
     $direction = $queryDefinition['sort']['normalizedData.is_associated'];
     $limit = $queryDefinition['limit'];
     $skip = $queryDefinition['skip'];
     $productFields = array_fill_keys($productFields, 1);
     $productFields['is_associated'] = ['$cond' => [['$or' => $or], 1, 0]];
     $pipeline = [['$match' => $match], ['$project' => $productFields], ['$sort' => ['is_associated' => $direction]], ['$skip' => $skip], ['$limit' => $limit]];
     return $pipeline;
 }
 /**
  * @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']]]]);
 }
 /**
  * @param ProductInterface $product
  * @param array            $drupalProduct
  */
 protected function computeProductAssociation(ProductInterface $product, array &$drupalProduct)
 {
     $identifierAttributeCode = $this->productManager->getIdentifierAttribute()->getCode();
     /** @var Group $group */
     foreach ($product->getAssociations() as $association) {
         $associationCode = $association->getAssociationType()->getCode();
         if ($association->getGroups()->count() > 0 || $association->getProducts()->count() > 0) {
             /**@var Product $product * */
             $drupalProduct['associations'][$associationCode] = ['type' => null, 'groups' => [], 'products' => []];
             $drupalProduct['associations'][$associationCode]['type'] = $associationCode;
             foreach ($association->getGroups() as $group) {
                 $drupalProduct['associations'][$associationCode]['groups'][] = $group->getCode();
             }
             foreach ($association->getProducts() as $product) {
                 $drupalProduct['associations'][$associationCode]['products'][] = $product->getValue($identifierAttributeCode)->getData();
             }
         }
     }
 }
 /**
  * Get create calls for a given product.
  *
  * @param ProductInterface $product
  *
  * @return array
  */
 protected function getCreateCallsForProduct(ProductInterface $product)
 {
     $createAssociationCalls = [];
     foreach ($product->getAssociations() as $productAssociation) {
         $createAssociationCalls = array_merge($createAssociationCalls, $this->getCreateCallsForAssociation($product, $productAssociation));
     }
     return $createAssociationCalls;
 }
 function it_normalizes_product_with_a_multiselect_value($filter, $serializer, ProductInterface $product, AbstractAttribute $skuAttribute, AbstractAttribute $colorsAttribute, AbstractProductValue $sku, AbstractProductValue $colors, AttributeOption $red, AttributeOption $blue, Collection $values, Family $family)
 {
     $family->getCode()->willReturn('shoes');
     $skuAttribute->getCode()->willReturn('sku');
     $skuAttribute->getAttributeType()->willReturn('pim_catalog_identifier');
     $skuAttribute->isLocalizable()->willReturn(false);
     $skuAttribute->isScopable()->willReturn(false);
     $sku->getAttribute()->willReturn($skuAttribute);
     $sku->getData()->willReturn('sku-001');
     $colorsAttribute->getCode()->willReturn('colors');
     $colorsAttribute->isLocalizable()->willReturn(false);
     $colorsAttribute->isScopable()->willReturn(false);
     $colors->getAttribute()->willReturn($colorsAttribute);
     $colors->getData()->willReturn([$red, $blue]);
     $product->getIdentifier()->willReturn($sku);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroupCodes()->willReturn('');
     $product->getCategoryCodes()->willReturn('');
     $product->getAssociations()->willReturn([]);
     $product->getValues()->willReturn($values);
     $filter->filter($values, ['identifier' => $sku, 'scopeCode' => null, 'localeCodes' => []])->willReturn([$sku, $colors]);
     $serializer->normalize($sku, 'flat', Argument::any())->willReturn(['sku' => 'sku-001']);
     $serializer->normalize($colors, 'flat', Argument::any())->willReturn(['colors' => 'red, blue']);
     $this->normalize($product, 'flat', [])->shouldReturn(['sku' => 'sku-001', 'family' => 'shoes', 'groups' => '', 'categories' => '', 'colors' => 'red, blue', 'enabled' => 1]);
 }
 /**
  * @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;
 }
 /**
  * Denormalize product associations
  *
  * @param string           &$data
  * @param string           $format
  * @param array            $context
  * @param ProductInterface $product
  *
  * @throws RevertException
  */
 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->fieldNameBuilder->getAssociationFieldNames();
     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->fieldNameBuilder->extractAssociationFieldNameInfos($fieldName))) {
             throw new RevertException(sprintf('Association type "%s" does not exist anymore', $matches['assoc_type_code']));
         }
     }
 }
 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]);
 }
 /**
  * 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;
 }