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' => []]);
 }
 /**
  * {@inheritdoc}
  */
 public function getProductCountByTree(ProductInterface $product)
 {
     $categories = $product->getCategories();
     $categoryIds = array();
     foreach ($categories as $category) {
         $categoryIds[] = $category->getId();
     }
     $categoryRepository = $this->entityManager->getRepository($this->categoryClass);
     $categoryTable = $this->entityManager->getClassMetadata($this->categoryClass)->getTableName();
     $categoryIds = implode(',', $categoryIds);
     if (!empty($categoryIds)) {
         $sql = "SELECT" . "    tree.id AS tree_id," . "    COUNT(category.id) AS product_count" . "  FROM {$categoryTable} tree" . "  LEFT JOIN {$categoryTable} category" . "    ON category.root = tree.id" . " AND category.id IN ({$categoryIds})" . " WHERE tree.parent_id IS NULL" . " GROUP BY tree.id";
     } else {
         $sql = "SELECT" . "    tree.id AS tree_id," . "    '0' AS product_count" . "  FROM {$categoryTable} tree" . "  LEFT JOIN {$categoryTable} category" . "    ON category.root = tree.id" . " WHERE tree.parent_id IS NULL" . " GROUP BY tree.id";
     }
     $stmt = $this->entityManager->getConnection()->prepare($sql);
     $stmt->execute();
     $productCounts = $stmt->fetchAll();
     $trees = array();
     foreach ($productCounts as $productCount) {
         $tree = array();
         $tree['productCount'] = $productCount['product_count'];
         $tree['tree'] = $categoryRepository->find($productCount['tree_id']);
         $trees[] = $tree;
     }
     return $trees;
 }
 function it_adds_category_field($categoryRepository, ProductInterface $product, CategoryInterface $mug, CategoryInterface $shirt, CategoryInterface $men)
 {
     $categoryRepository->findOneByIdentifier('mug')->willReturn($mug);
     $categoryRepository->findOneByIdentifier('shirt')->willReturn($shirt);
     $product->getCategories()->willReturn([$men]);
     $product->addCategory($mug)->shouldBeCalled();
     $product->addCategory($shirt)->shouldBeCalled();
     $this->addFieldData($product, 'categories', ['mug', 'shirt']);
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["category_code"]
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $categories = [];
     foreach ($data as $categoryCode) {
         $category = $this->categoryRepository->findOneByIdentifier($categoryCode);
         if (null === $category) {
             throw InvalidArgumentException::expected($field, 'existing category code', 'setter', 'category', $categoryCode);
         } else {
             $categories[] = $category;
         }
     }
     $oldCategories = $product->getCategories();
     foreach ($oldCategories as $category) {
         $product->removeCategory($category);
     }
     foreach ($categories as $category) {
         $product->addCategory($category);
     }
 }
 function it_returns_only_products_in_the_given_channel(ProductInterface $product1, ProductInterface $product2, Channel $channel, ArrayCollection $productCategories1, ArrayCollection $productCategories2, ArrayCollection $completenesses, Category $rootCategory, Category $category1, Category $category2, AbstractCompleteness $completeness1, AbstractCompleteness $completeness2)
 {
     $channel->getCategory()->willReturn($rootCategory);
     $channel->getId()->willReturn(2);
     $rootCategory->getId()->willReturn(1);
     $product1->getCategories()->willReturn($productCategories1);
     $product2->getCategories()->willReturn($productCategories2);
     $product1->getCompletenesses()->willReturn($completenesses);
     $product2->getCompletenesses()->willReturn($completenesses);
     $completenesses->toArray()->willReturn([$completeness1, $completeness2]);
     $completeness1->getChannel()->willReturn($channel);
     $completeness2->getChannel()->willReturn($channel);
     $completeness1->getRatio()->willReturn(100);
     $completeness2->getRatio()->willReturn(100);
     $productCategories1->toArray()->willReturn([$category1]);
     $productCategories2->toArray()->willReturn([$category2]);
     $category1->getRoot()->willReturn(1);
     $category2->getRoot()->willReturn(5);
     $this->apply($channel, [$product1, $product2])->shouldReturn([$product1]);
 }
 /**
  * @param ProductInterface $product
  * @param array            $drupalProduct
  */
 protected function computeProductCategory(ProductInterface $product, array &$drupalProduct)
 {
     /** @var Category $category */
     foreach ($product->getCategories() as $category) {
         if ($category->getLevel() > 0) {
             $drupalProduct['categories'][$this->formatedRootCategories[$category->getRoot()]][] = $category->getCode();
         }
     }
 }
 /**
  * Get categories for the given product.
  *
  * @param ProductInterface  $product
  * @param MappingCollection $categoryMapping
  * @param string            $scopeCode
  *
  * @return array
  *
  * @throws CategoryNotFoundException
  */
 protected function getProductCategories(ProductInterface $product, MappingCollection $categoryMapping, $scopeCode)
 {
     $productCategories = [];
     $channelCategoryTree = $this->channelManager->getChannelByCode($scopeCode)->getCategory();
     foreach ($product->getCategories() as $category) {
         if ($category->getRoot() == $channelCategoryTree->getId()) {
             $prestashopCategoryId = $this->categoryMappingManager->getIdFromCategory($category, $this->prestashopUrl, $categoryMapping);
             if (!$prestashopCategoryId) {
                 throw new CategoryNotFoundException(sprintf('The category %s was not found. Please export categories first or add it to the root ' . 'category mapping', $category->getLabel()));
             }
             $productCategories[] = $prestashopCategoryId;
         }
     }
     return $productCategories;
 }
 /**
  * Denormalize product categories
  *
  * @param string           $data
  * @param string           $format
  * @param array            $context
  * @param ProductInterface $product
  */
 protected function denormalizeCategories($data, $format, array $context, ProductInterface $product)
 {
     foreach ($product->getCategories() as $category) {
         $product->removeCategory($category);
     }
     $categoryCodes = strlen($data) > 0 ? explode(",", $data) : array();
     foreach ($categoryCodes as $categoryCode) {
         $product->addCategory($this->serializer->denormalize($categoryCode, $this->categoryClass, $format, $context));
     }
 }