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 addFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $categories = [];
     foreach ($data as $categoryCode) {
         $category = $this->categoryRepository->findOneByIdentifier($categoryCode);
         if (null !== $category) {
             $categories[] = $category;
         } else {
             throw InvalidArgumentException::expected($field, 'existing category code', 'adder', 'category', $categoryCode);
         }
     }
     foreach ($categories as $category) {
         $product->addCategory($category);
     }
 }
 /**
  * {@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) : [];
     foreach ($categoryCodes as $categoryCode) {
         $product->addCategory($this->serializer->denormalize($categoryCode, $this->categoryClass, $format, $context));
     }
 }