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->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); } }
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' => []]); }
/** * @param ProductInterface $product * * @return array */ protected function buildCategories(ProductInterface $product) { $result = []; foreach ($product->getCategories() as $category) { $result[] = ['id' => $category->getId(), 'code' => $category->getCode(), 'rootId' => $category->getRoot()]; } return $result; }
/** * 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)); } }