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_adds_missing_product_values_from_family_on_new_product($valuesResolver, FamilyInterface $family, ProductInterface $product, AttributeInterface $sku, AttributeInterface $name, AttributeInterface $desc, ProductValueInterface $skuValue) { $sku->getCode()->willReturn('sku'); $sku->getAttributeType()->willReturn('pim_catalog_identifier'); $sku->isLocalizable()->willReturn(false); $sku->isScopable()->willReturn(false); $name->getCode()->willReturn('name'); $name->getAttributeType()->willReturn('pim_catalog_text'); $name->isLocalizable()->willReturn(true); $name->isScopable()->willReturn(false); $desc->getCode()->willReturn('description'); $desc->getAttributeType()->willReturn('pim_catalog_text'); $desc->isLocalizable()->willReturn(true); $desc->isScopable()->willReturn(true); // get expected attributes $product->getAttributes()->willReturn([$sku]); $family->getAttributes()->willReturn([$sku, $name, $desc]); $product->getFamily()->willReturn($family); // get eligible values $valuesResolver->resolveEligibleValues(['sku' => $sku, 'name' => $name, 'description' => $desc])->willReturn([['attribute' => 'sku', 'type' => 'pim_catalog_identifier', 'locale' => null, 'scope' => null], ['attribute' => 'name', 'type' => 'pim_catalog_text', 'locale' => 'fr_FR', 'scope' => null], ['attribute' => 'name', 'type' => 'pim_catalog_text', 'locale' => 'en_US', 'scope' => null], ['attribute' => 'description', 'type' => 'pim_catalog_text', 'locale' => 'en_US', 'scope' => 'ecommerce'], ['attribute' => 'description', 'type' => 'pim_catalog_text', 'locale' => 'fr_FR', 'scope' => 'ecommerce'], ['attribute' => 'description', 'type' => 'pim_catalog_text', 'locale' => 'en_US', 'scope' => 'print'], ['attribute' => 'description', 'type' => 'pim_catalog_text', 'locale' => 'fr_FR', 'scope' => 'print']]); // get existing values $skuValue->getAttribute()->willReturn($sku); $skuValue->getLocale()->willReturn(null); $skuValue->getScope()->willReturn(null); $product->getValues()->willReturn([$skuValue]); // add 6 new values : 4 desc (locales x scopes) + 2 name (locales $product->addValue(Argument::any())->shouldBeCalledTimes(6); $this->addMissingProductValues($product); }
function it_provides_formatted_batch_config_for_the_job(FamilyInterface $mugs) { $mugs->getCode()->willReturn('amazing_mugs'); $this->setFamily($mugs); $this->setFilters([['id', 'IN', ['1003', '1002']]]); $this->getBatchConfig()->shouldReturn('{\\"filters\\":[[\\"id\\",\\"IN\\",[\\"1003\\",\\"1002\\"]]],\\"actions\\":[{\\"field\\":\\"family\\",\\"value\\":\\"amazing_mugs\\"}]}'); }
function it_has_family(FamilyInterface $family) { $family->getId()->willReturn(42); $this->setFamily($family); $this->getFamily()->shouldReturn($family); $this->getFamilyId()->shouldReturn(42); }
function it_normalizes_family($normalizer, FamilyInterface $family, AttributeInterface $sku) { $sku->getCode()->willReturn('sku'); $family->getCode()->willReturn('mongo'); $family->getAttributeAsLabel()->willReturn($sku); $normalizer->normalize($family, 'mongodb_json', [])->willReturn(['label' => 'translations']); $this->normalize($family, 'mongodb_json', [])->shouldReturn(['code' => 'mongo', 'label' => 'translations', 'attributeAsLabel' => 'sku']); }
function it_returns_filters_on_family_grid($massActionDispatcher, Request $request, FamilyInterface $family1, FamilyInterface $family2) { $request->get('gridName')->willReturn('family-grid'); $massActionDispatcher->dispatch($request)->willReturn([$family1, $family2]); $family1->getId()->willReturn(45); $family2->getId()->willReturn(70); $massActionDispatcher->getRawFilters($request)->shouldNotBeCalled(); $this->adapt($request)->shouldReturn([['field' => 'id', 'operator' => 'IN', 'value' => [45, 70]]]); }
public function let(AttributeRequirementFactory $requirementFactory, LifecycleEventArgs $eventArgs, ChannelInterface $channel, EntityManagerInterface $entityManager, EntityRepository $repository, FamilyInterface $family) { $this->beConstructedWith($requirementFactory); $eventArgs->getEntity()->willReturn($channel); $eventArgs->getEntityManager()->willReturn($entityManager); $entityManager->getRepository(Argument::exact('PimCatalogBundle:Family'))->willReturn($repository); $repository->findAll()->willReturn([$family]); $family->getAttributes()->willReturn([]); }
function it_saves_a_family_and_does_not_schedule($completenessManager, $optionsResolver, $objectManager, FamilyInterface $family) { $family->getCode()->willReturn('my_code'); $optionsResolver->resolveSaveOptions(['schedule' => false])->shouldBeCalled()->willReturn(['flush' => true, 'schedule' => false]); $objectManager->persist($family)->shouldBeCalled(); $objectManager->flush()->shouldBeCalled(); $completenessManager->scheduleForFamily($family)->shouldNotBeCalled($family); $this->save($family, ['schedule' => false]); }
function it_saves_a_family_and_does_not_schedule($completenessManager, $optionsResolver, $objectManager, $eventDispatcher, FamilyInterface $family) { $family->getCode()->willReturn('my_code'); $optionsResolver->resolveSaveOptions(['schedule' => false])->shouldBeCalled()->willReturn(['flush' => true, 'schedule' => false]); $objectManager->persist($family)->shouldBeCalled(); $objectManager->flush()->shouldBeCalled(); $completenessManager->scheduleForFamily($family)->shouldNotBeCalled($family); $eventDispatcher->dispatch(StorageEvents::PRE_SAVE, Argument::cetera())->shouldBeCalled(); $eventDispatcher->dispatch(StorageEvents::POST_SAVE, Argument::cetera())->shouldBeCalled(); $this->save($family, ['schedule' => false]); }
/** * {@inheritdoc} */ public function getFullFamilies(FamilyInterface $family = null, ChannelInterface $channel = null) { $qb = $this->createQueryBuilder('f')->select('f, c, l, r, a, cu')->join('f.requirements', 'r')->join('r.attribute', 'a')->join('r.channel', 'c')->join('c.locales', 'l')->join('c.currencies', 'cu')->where('r.required = 1'); if (null !== $channel) { $qb->andWhere('r.channel = :channel')->setParameter('channel', $channel); } if (null !== $family) { $qb->andWhere('f.id = :familyId')->setParameter('familyId', $family->getId()); } return $qb->getQuery()->getResult(); }
function it_does_not_validate_families_with_missing_identifier_requirements($attributeRepository, $channelRepository, $context, $minimumRequirements, FamilyInterface $family, AttributeRequirementInterface $requirementEcommerce, AttributeRequirementInterface $requirementMobile, ConstraintViolationBuilderInterface $violation) { $family->getAttributeRequirements()->willReturn([$requirementEcommerce, $requirementMobile]); $attributeRepository->getIdentifierCode()->willReturn('sku'); $requirementEcommerce->getAttributeCode()->willReturn('sku'); $requirementEcommerce->getChannelCode()->willReturn('ecommerce'); $requirementMobile->getAttributeCode()->willReturn('sku'); $requirementMobile->getChannelCode()->willReturn('mobile'); $channelRepository->getChannelCodes()->willReturn(['ecommerce', 'mobile', 'print']); $family->getCode()->willReturn('familyCode'); $context->buildViolation(Argument::any(), Argument::any())->willReturn($violation)->shouldBeCalled(); $this->validate($family, $minimumRequirements); }
/** * Normalize the requirements * * @param FamilyInterface $family * * @return array */ protected function normalizeRequirements(FamilyInterface $family) { $required = []; foreach ($family->getAttributeRequirements() as $requirement) { $channelCode = $requirement->getChannel()->getCode(); if (!isset($required['requirements-' . $channelCode])) { $required['requirements-' . $channelCode] = []; } if ($requirement->isRequired()) { $required['requirements-' . $channelCode][] = $requirement->getAttribute()->getCode(); } } return $required; }
/** * @param FamilyInterface $family * * @return string[] */ protected function getMissingChannelCodes(FamilyInterface $family) { $requirements = $family->getAttributeRequirements(); $identifierCode = $this->attributeRepository->getIdentifierCode(); $currentChannelCodes = []; foreach ($requirements as $requirement) { if ($requirement->getAttributeCode() === $identifierCode) { $currentChannelCodes[] = $requirement->getChannelCode(); } } $expectedChannelCodes = $this->channelRepository->getChannelCodes(); $missingChannelCodes = array_diff($expectedChannelCodes, $currentChannelCodes); return $missingChannelCodes; }
function it_sets_invalid_values_to_attributes($validator, $propertySetter, FamilyInterface $family, AttributeInterface $attribute, AttributeRepository $attributeRepository, ProductInterface $product, ConstraintViolationListInterface $violations, StepExecution $stepExecution, JobConfigurationRepositoryInterface $jobConfigurationRepo, JobExecution $jobExecution, JobConfigurationInterface $jobConfiguration) { $stepExecution->getJobExecution()->willReturn($jobExecution); $jobConfigurationRepo->findOneBy(['jobExecution' => $jobExecution])->willReturn($jobConfiguration); $jobConfiguration->getConfiguration()->willReturn(json_encode(['filters' => [], 'actions' => [['field' => 'categories', 'value' => ['office', 'bedroom'], 'options' => []]]])); $validator->validate($product)->willReturn($violations); $violation = new ConstraintViolation('error2', 'spec', [], '', '', $product); $violations = new ConstraintViolationList([$violation, $violation]); $validator->validate($product)->willReturn($violations); $attributeRepository->findOneBy(['code' => 'categories'])->willReturn($attribute); $family->hasAttribute($attribute)->willReturn(true); $product->getFamily()->willReturn($family); $propertySetter->setData($product, 'categories', ['office', 'bedroom'], [])->shouldBeCalled(); $this->setStepExecution($stepExecution); $stepExecution->addWarning(Argument::cetera())->shouldBeCalled(); $stepExecution->incrementSummaryInfo('skipped_products')->shouldBeCalled(); $this->process($product); }
/** * {@inheritdoc} */ public function isAttributeRemovable(AttributeInterface $attribute) { if ('pim_catalog_identifier' === $attribute->getAttributeType()) { return false; } if (null !== $this->family && $this->family->getAttributes()->contains($attribute)) { return false; } foreach ($this->groups as $group) { if ($group->getType()->isVariant()) { if ($group->getAttributes()->contains($attribute)) { return false; } $template = $group->getProductTemplate(); if (null !== $template && $template->hasValueForAttribute($attribute)) { return false; } } } return true; }
/** * Normalize the requirements * * @param FamilyInterface $family * * @return array */ protected function normalizeRequirements(FamilyInterface $family) { $required = []; $flat = []; foreach ($family->getAttributeRequirements() as $requirement) { $channelCode = $requirement->getChannel()->getCode(); if (!isset($required['requirements-' . $channelCode])) { $required['requirements-' . $channelCode] = []; } if ($requirement->isRequired()) { $required['requirements-' . $channelCode][] = $requirement->getAttribute()->getCode(); } } foreach ($required as $key => $attributes) { $flat[$key] = implode(',', $attributes); } return $flat; }
/** * @param FamilyInterface $family * @param string $data */ protected function setAttributeAsLabel(FamilyInterface $family, $data) { if (null !== ($attribute = $this->attributeRepository->findOneByIdentifier($data))) { $family->setAttributeAsLabel($attribute); } else { throw new \InvalidArgumentException(sprintf('Attribute with "%s" code does not exist', $data)); } }
/** * {@inheritdoc} */ public function scheduleForFamily(FamilyInterface $family) { $sql = ' DELETE c FROM pim_catalog_completeness c JOIN %product_table% p ON p.id = c.product_id WHERE p.family_id = :family_id'; $sql = $this->applyTableNames($sql); $stmt = $this->connection->prepare($sql); $stmt->bindValue('family_id', $family->getId()); $stmt->execute(); }
/** * Schedule recalculation of completenesses for all product * of a family * * @param FamilyInterface $family */ public function scheduleForFamily(FamilyInterface $family) { if ($family->getId()) { $this->generator->scheduleForFamily($family); } }
function it_updates_a_family($attrRequiFactory, $channelRepository, FamilyTranslation $translation, FamilyInterface $family, AttributeRepositoryInterface $attributeRepository, AttributeInterface $attribute1, AttributeInterface $attribute2, AttributeInterface $attribute3, AttributeInterface $attribute4, AttributeRequirementInterface $attributeRequirement1, AttributeRequirementInterface $attributeRequirement2, AttributeRequirementInterface $attributeRequirement3, AttributeRequirementInterface $attributeRequirement4, AttributeRequirementInterface $attributeRequirement5, ChannelInterface $channel1, ChannelInterface $channel2, FamilyInterface $family) { $values = ['code' => 'mycode', 'attributes' => ['sku', 'name', 'description', 'price'], 'attribute_as_label' => 'name', 'requirements' => ['mobile' => ['sku', 'name'], 'print' => ['sku', 'name', 'description']], 'labels' => ['fr_FR' => 'Moniteurs', 'en_US' => 'PC Monitors']]; $attributeRepository->findOneByIdentifier('sku')->willReturn($attribute1); $attributeRepository->findOneByIdentifier('name')->willReturn($attribute2); $attributeRepository->findOneByIdentifier('description')->willReturn($attribute3); $attributeRepository->findOneByIdentifier('price')->willReturn($attribute4); $channelRepository->findOneByIdentifier('mobile')->willReturn($channel1); $channelRepository->findOneByIdentifier('print')->willReturn($channel2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel1, true)->willReturn($attributeRequirement1); $attrRequiFactory->createAttributeRequirement($attribute2, $channel1, true)->willReturn($attributeRequirement2); $attrRequiFactory->createAttributeRequirement($attribute1, $channel2, true)->willReturn($attributeRequirement3); $attrRequiFactory->createAttributeRequirement($attribute2, $channel2, true)->willReturn($attributeRequirement4); $attrRequiFactory->createAttributeRequirement($attribute3, $channel2, true)->willReturn($attributeRequirement5); $family->setAttributeRequirements([$attributeRequirement1, $attributeRequirement2, $attributeRequirement3, $attributeRequirement4, $attributeRequirement5])->shouldBeCalled(); $family->setCode('mycode')->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute1)->shouldBeCalled(); $family->setLocale('en_US')->shouldBeCalled(); $family->setLocale('fr_FR')->shouldBeCalled(); $family->getTranslation()->willReturn($translation); $translation->setLabel('label en us'); $translation->setLabel('label fr fr'); $family->addAttribute($attribute1)->shouldBeCalled(); $family->addAttribute($attribute2)->shouldBeCalled(); $family->addAttribute($attribute3)->shouldBeCalled(); $family->addAttribute($attribute4)->shouldBeCalled(); $family->setAttributeAsLabel($attribute2)->shouldBeCalled(); $this->update($family, $values, []); }
/** * {@inheritdoc} */ protected function doPerform(FamilyInterface $family) { foreach ($this->attRequirements as $attributeRequirement) { $family->addAttribute($attributeRequirement->getAttribute()); $family->addAttributeRequirement($this->factory->createAttributeRequirement($attributeRequirement->getAttribute(), $attributeRequirement->getChannel(), $attributeRequirement->isRequired())); } }
function it_is_not_attribute_removable_with_family_containing_attribute(AttributeInterface $attribute, FamilyInterface $family, ArrayCollection $familyAttributes) { $familyAttributes->contains($attribute)->willReturn(true); $family->getId()->willReturn(42); $family->getAttributes()->willReturn($familyAttributes); $this->setFamily($family); $this->isAttributeRemovable($attribute)->shouldReturn(false); }
/** * {@inheritdoc} */ public function hasAttributeInFamily(AttributeInterface $attribute) { return null !== $this->family && $this->family->getAttributes()->contains($attribute); }
/** * Normalizes a family * * @param FamilyInterface $family */ protected function normalizeFamily(FamilyInterface $family = null) { $this->results[self::FIELD_FAMILY] = $family ? $family->getCode() : ''; }
/** * {@inheritdoc} */ public function scheduleForFamily(FamilyInterface $family) { $productQb = $this->documentManager->createQueryBuilder($this->productClass); $productQb->update()->multiple(true)->field('family')->equals($family->getId())->field('completenesses')->unsetField()->field('normalizedData.completenesses')->unsetField()->getQuery()->execute(); }
/** * {@inheritdoc} */ public function findAllIdsForFamily(FamilyInterface $family) { $qb = $this->createQueryBuilder('p')->hydrate(false)->field('family')->equals($family->getId())->select('_id'); $results = $qb->getQuery()->execute()->toArray(); return array_keys($results); }
/** * Returns the attribute codes for a family * * @param FamilyInterface $family * * @return array */ protected function getFamilyAttributeCodes(FamilyInterface $family) { $code = $family->getCode(); if (!isset($this->familyAttributeCodes[$code])) { $this->familyAttributeCodes[$code] = $this->getAttributeCodes($family); } return $this->familyAttributeCodes[$code]; }
function it_throws_an_exception_if_channel_not_found($channelRepository, $attributeRepository, AttributeInterface $attribute, FamilyInterface $family) { $data = ['code' => 'mycode', 'requirements' => ['mobile' => ['sku', 'name'], 'print' => ['sku', 'name', 'description']]]; $family->getAttributeRequirements()->willReturn([]); $family->setCode('mycode')->shouldBeCalled(); $attributeRepository->findOneByIdentifier('sku')->willReturn($attribute); $attributeRepository->findOneByIdentifier('name')->willReturn($attribute); $attributeRepository->findOneByIdentifier('description')->willReturn($attribute); $attributeRepository->findOneByIdentifier('price')->willReturn($attribute); $channelRepository->findOneByIdentifier('print')->willReturn(null); $channelRepository->findOneByIdentifier('mobile')->willReturn(null); $this->shouldThrow(new \InvalidArgumentException(sprintf('Channel with "%s" code does not exist', 'mobile')))->during('update', [$family, $data]); }
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]); }
/** * Sets the requirements for a channel * * @param string $class * @param FamilyInterface $family * @param string $channelCode * @param array $attributeCodes */ protected function setChannelRequirements($class, FamilyInterface $family, $channelCode, $attributeCodes) { foreach ($attributeCodes as $attributeCode) { $data = array('attribute' => $attributeCode, 'channel' => $channelCode, 'required' => true); $requirement = $this->transformNestedEntity($class, 'requirements', $this->requirementClass, $data); if ($requirement->getAttribute() === null) { throw new \Exception(sprintf('The attribute "%s" used as requirement in family "%s" is not known', $attributeCode, $family->getCode())); } $family->addAttributeRequirement($requirement); } }