function it_removes_categories_to_a_product($categoryRepository, CategoryInterface $bookCategory, CategoryInterface $penCategory, CategoryInterface $bluePenCategory, ProductInterface $bookProduct, ProductInterface $bluePenProduct)
 {
     $categoryRepository->findOneByIdentifier('book_category')->willReturn($bookCategory);
     $bookProduct->removeCategory($bookCategory)->shouldBeCalled();
     $categoryRepository->findOneByIdentifier('pen_category')->willReturn($penCategory);
     $categoryRepository->findOneByIdentifier('blue_pen_category')->willReturn($bluePenCategory);
     $bluePenProduct->removeCategory($penCategory)->shouldBeCalled();
     $bluePenProduct->removeCategory($bluePenCategory)->shouldBeCalled();
     $this->removeFieldData($bookProduct, 'categories', ['book_category']);
     $this->removeFieldData($bluePenProduct, 'categories', ['pen_category', 'blue_pen_category']);
 }
 function it_applies_on_related_products($saver, RemoveEvent $event, CategoryInterface $object, ProductInterface $product)
 {
     $saver->saveAll([$product], ['flush' => 'expected_flush_value', 'recalculate' => false, 'schedule' => false])->shouldBeCalled();
     $event->getSubject()->willReturn($object);
     $event->getArgument('flush')->willReturn('expected_flush_value');
     $object->getProducts()->willReturn([$product]);
     $product->removeCategory($object)->shouldBeCalled();
     $this->postRemove($event)->shouldReturn(null);
 }
 function it_sets_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->removeCategory($men)->shouldBeCalled();
     $product->addCategory($mug)->shouldBeCalled();
     $product->addCategory($shirt)->shouldBeCalled();
     $this->setFieldData($product, 'categories', ['mug', 'shirt']);
 }
 function it_dispatches_an_event_when_removing_a_category($eventDispatcher, $objectManager, CategoryInterface $category, ProductInterface $product1, ProductInterface $product2)
 {
     $category->isRoot()->willReturn(false);
     $category->getProducts()->willReturn([$product1, $product2]);
     $product1->removeCategory($category)->shouldBeCalled();
     $product2->removeCategory($category)->shouldBeCalled();
     $eventDispatcher->dispatch(CategoryEvents::PRE_REMOVE_CATEGORY, Argument::type('Symfony\\Component\\EventDispatcher\\GenericEvent'))->shouldBeCalled();
     $objectManager->remove($category)->shouldBeCalled();
     $this->remove($category, ['flush' => false]);
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["category_code", "another_category_code"]
  */
 public function removeFieldData(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', 'remover', 'category', $categoryCode);
         }
         $categories[] = $category;
     }
     foreach ($categories as $categoryToRemove) {
         $product->removeCategory($categoryToRemove);
     }
 }
 function it_dispatches_an_event_when_removing_a_category($eventDispatcher, $objectManager, $optionsResolver, $productSaver, CategoryInterface $category, ProductInterface $product1, ProductInterface $product2)
 {
     $optionsResolver->resolveRemoveOptions(['flush' => true])->willReturn(['flush' => true]);
     $category->getId()->willReturn(12);
     $category->isRoot()->willReturn(false);
     $category->getProducts()->willReturn([$product1, $product2]);
     $product1->removeCategory($category)->shouldBeCalled();
     $product2->removeCategory($category)->shouldBeCalled();
     $eventDispatcher->dispatch(CategoryEvents::PRE_REMOVE_CATEGORY, Argument::type('Akeneo\\Component\\StorageUtils\\Event\\RemoveEvent'))->shouldBeCalled();
     $objectManager->remove($category)->shouldBeCalled();
     $objectManager->flush()->shouldBeCalled();
     $eventDispatcher->dispatch(CategoryEvents::POST_REMOVE_CATEGORY, Argument::type('Akeneo\\Component\\StorageUtils\\Event\\RemoveEvent'))->shouldBeCalled();
     $productSaver->saveAll([$product1, $product2], ['flush' => true, 'recalculate' => false, 'schedule' => false])->shouldBeCalled();
     $this->remove($category, ['flush' => true]);
 }
 /**
  * {@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->getCategory($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);
     }
 }
 /**
  * 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));
     }
 }