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_provide_product_completeness_if_family_is_not_defined(ProductInterface $product, ChannelInterface $mobile, LocaleInterface $en, FamilyInterface $shirt)
 {
     $product->getFamily()->willReturn(null);
     $en->getCode()->willReturn('en_US');
     $mobile->getCode()->willReturn('mobile');
     $this->getProductCompleteness($product, [$mobile], [$en], 'en_US')->shouldReturn(['en_US' => ['channels' => ['mobile' => ['completeness' => null, 'missing' => []]], 'stats' => ['total' => 0, 'complete' => 0]]]);
 }
 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' => []]);
 }
 /**
  * Returns the required attribute codes for a product
  *
  * @param ProductInterface $product
  *
  * @return array
  */
 public function getRequiredAttributeCodes(ProductInterface $product)
 {
     $codes = array();
     if ($product->getFamily()) {
         $codes = $this->getFamilyAttributeCodes($product->getFamily());
     }
     foreach ($product->getGroups() as $group) {
         $codes = array_merge($codes, $this->getGroupAttributeCodes($group));
     }
     if ($product->getId()) {
         foreach ($product->getValues() as $value) {
             $codes[] = $value->getAttribute()->getCode();
         }
     }
     return array_unique($codes);
 }
 /**
  * @param  $product
  * @return array
  */
 public function getDefaultDrupalProduct(ProductInterface $product)
 {
     $labels = [];
     $attributeAsLabel = $product->getFamily()->getAttributeAsLabel();
     $availableLocales = $attributeAsLabel->getAvailableLocales();
     if (!is_null($availableLocales)) {
         foreach ($availableLocales as $availableLocale) {
             $labels[$availableLocale->getCode()] = $product->getLabel($availableLocale->getCode());
         }
     }
     // TODO: fix availableLocales doesn t must be NULL
     foreach ($attributeAsLabel->getTranslations() as $translation) {
         $labels[$translation->getLocale()] = $product->getLabel($translation->getLocale());
     }
     $defaultDrupalProduct = ['sku' => $product->getReference(), 'family' => $product->getFamily()->getCode(), 'created' => $product->getCreated()->format('c'), 'updated' => $product->getUpdated()->format('c'), 'status' => $product->isEnabled(), 'labels' => $labels, 'categories' => [], 'groups' => [], 'associations' => [], 'values' => []];
     return $defaultDrupalProduct;
 }
 function it_normalizes_product(SerializerInterface $serializer, ProductInterface $product, FamilyInterface $family, Completeness $completeness)
 {
     $serializer->implement('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
     $this->setSerializer($serializer);
     $product->getFamily()->willReturn($family);
     $product->getGroups()->willReturn([]);
     $product->getValues()->willReturn([]);
     $product->getCompletenesses()->willReturn([$completeness]);
     $product->getCreated()->willReturn(null);
     $product->getUpdated()->willReturn(null);
     $product->isEnabled()->willReturn(true);
     $serializer->normalize($family, 'mongodb_json', [])->willReturn('family normalization');
     $serializer->normalize($completeness, 'mongodb_json', [])->willReturn(['completenessCode' => 'completeness normalization']);
     $this->normalize($product, 'mongodb_json', [])->shouldReturn([ProductNormalizer::FAMILY_FIELD => 'family normalization', ProductNormalizer::COMPLETENESSES_FIELD => array('completenessCode' => 'completeness normalization'), ProductNormalizer::ENABLED_FIELD => true]);
 }
 function it_adds_missing_product_values_from_family_on_new_product(FamilyInterface $family, ProductInterface $product, AttributeInterface $sku, AttributeInterface $name, AttributeInterface $desc, $localeRepository, LocaleInterface $fr, LocaleInterface $en, $channelRepository, ChannelInterface $ecom, ChannelInterface $print, ProductValueInterface $skuValue)
 {
     // get expected attributes
     $product->getAttributes()->willReturn([$sku]);
     $product->getFamily()->willReturn($family);
     $family->getAttributes()->willReturn([$sku, $name, $desc]);
     // get expected values
     $sku->getCode()->willReturn('sku');
     $sku->getAttributeType()->willReturn('pim_catalog_identifier');
     $sku->isLocalizable()->willReturn(false);
     $sku->isScopable()->willReturn(false);
     $sku->isLocaleSpecific()->willReturn(false);
     $name->getCode()->willReturn('name');
     $name->getAttributeType()->willReturn('pim_catalog_text');
     $name->isLocalizable()->willReturn(true);
     $name->isScopable()->willReturn(false);
     $name->isLocaleSpecific()->willReturn(false);
     $desc->getCode()->willReturn('desc');
     $desc->getAttributeType()->willReturn('pim_catalog_text');
     $desc->isLocalizable()->willReturn(true);
     $desc->isScopable()->willReturn(true);
     $desc->isLocaleSpecific()->willReturn(false);
     $fr->getCode()->willReturn('fr_FR');
     $en->getCode()->willReturn('fr_FR');
     $localeRepository->getActivatedLocales()->willReturn([$fr, $en]);
     $ecom->getCode()->willReturn('ecom');
     $ecom->getLocales()->willReturn([$en, $fr]);
     $print->getCode()->willReturn('print');
     $print->getLocales()->willReturn([$en, $fr]);
     $channelRepository->findAll()->willReturn([$ecom, $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_normalizes_the_values_of_product(ProductInterface $product, AttributeInterface $attribute, ProductValueInterface $value, ArrayCollection $values, \ArrayIterator $iterator, $filter, $serializer)
 {
     $values->getIterator()->willReturn($iterator);
     $product->getAssociations()->willReturn([]);
     $product->getFamily()->willReturn(null);
     $product->getGroupCodes()->willReturn([]);
     $product->getCategoryCodes()->willReturn([]);
     $product->isEnabled()->willReturn(true);
     $value->getAttribute()->willReturn($attribute);
     $attribute->getCode()->willReturn('name');
     $product->getValues()->willReturn($values);
     $filter->filter($values, Argument::any())->shouldBeCalled()->willReturn($values);
     $iterator->rewind()->willReturn(null);
     $valueCount = 1;
     $iterator->valid()->will(function () use(&$valueCount) {
         return $valueCount-- > 0;
     });
     $iterator->current()->willReturn($value);
     $iterator->next()->willReturn(null);
     $serializer->normalize($value, 'json', Argument::any())->willReturn(['locale' => null, 'scope' => null, 'value' => 'foo']);
     $this->normalize($product, 'json')->shouldReturn(['family' => null, 'groups' => [], 'categories' => [], 'enabled' => true, 'associations' => [], 'values' => ['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']]]]);
 }
 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]);
 }
 /**
  * Returns an array containing all completeness info and missing attributes for a product
  *
  * @param ProductInterface                                    $product
  * @param \Pim\Bundle\CatalogBundle\Entity\ChannelInterface[] $channels
  * @param \Pim\Bundle\CatalogBundle\Entity\LocaleInterface[]  $locales
  * @param string                                              $localeCode
  *
  * @return array
  */
 public function getProductCompleteness(ProductInterface $product, array $channels, array $locales, $localeCode)
 {
     $family = $product->getFamily();
     $getCodes = function ($entities) {
         return array_map(function ($entity) {
             return $entity->getCode();
         }, $entities);
     };
     $channelCodes = $getCodes($channels);
     $localeCodes = $getCodes($locales);
     $channelTemplate = ['channels' => array_fill_keys($channelCodes, ['completeness' => null, 'missing' => []]), 'stats' => ['total' => 0, 'complete' => 0]];
     $completenesses = array_fill_keys($localeCodes, $channelTemplate);
     if (!$family) {
         return $completenesses;
     }
     $allCompletenesses = $product->getCompletenesses();
     foreach ($allCompletenesses as $completeness) {
         $locale = $completeness->getLocale();
         $channel = $completeness->getChannel();
         $compLocaleCode = $locale->getCode();
         if (isset($completenesses[$compLocaleCode])) {
             $completenesses[$compLocaleCode]['channels'][$channel->getCode()]['completeness'] = $completeness;
             $completenesses[$compLocaleCode]['stats']['total']++;
             if (0 === $completeness->getMissingCount()) {
                 $completenesses[$compLocaleCode]['stats']['complete']++;
             }
         }
     }
     $requirements = $this->familyRepository->getFullRequirementsQB($family, $localeCode)->getQuery()->getResult();
     $productValues = $product->getValues();
     foreach ($requirements as $requirement) {
         if ($requirement->isRequired()) {
             $this->addRequirementToCompleteness($completenesses, $requirement, $productValues, $locales);
         }
     }
     return $completenesses;
 }
 function it_normalizes_product_with_a_multiselect_value($filter, $serializer, ProductInterface $product, AbstractAttribute $skuAttribute, AbstractAttribute $colorsAttribute, AbstractProductValue $sku, AbstractProductValue $colors, AttributeOption $red, AttributeOption $blue, Collection $values, Family $family)
 {
     $family->getCode()->willReturn('shoes');
     $skuAttribute->getCode()->willReturn('sku');
     $skuAttribute->getAttributeType()->willReturn('pim_catalog_identifier');
     $skuAttribute->isLocalizable()->willReturn(false);
     $skuAttribute->isScopable()->willReturn(false);
     $sku->getAttribute()->willReturn($skuAttribute);
     $sku->getData()->willReturn('sku-001');
     $colorsAttribute->getCode()->willReturn('colors');
     $colorsAttribute->isLocalizable()->willReturn(false);
     $colorsAttribute->isScopable()->willReturn(false);
     $colors->getAttribute()->willReturn($colorsAttribute);
     $colors->getData()->willReturn([$red, $blue]);
     $product->getIdentifier()->willReturn($sku);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroupCodes()->willReturn('');
     $product->getCategoryCodes()->willReturn('');
     $product->getAssociations()->willReturn([]);
     $product->getValues()->willReturn($values);
     $filter->filter($values, ['identifier' => $sku, 'scopeCode' => null, 'localeCodes' => []])->willReturn([$sku, $colors]);
     $serializer->normalize($sku, 'flat', Argument::any())->willReturn(['sku' => 'sku-001']);
     $serializer->normalize($colors, 'flat', Argument::any())->willReturn(['colors' => 'red, blue']);
     $this->normalize($product, 'flat', [])->shouldReturn(['sku' => 'sku-001', 'family' => 'shoes', 'groups' => '', 'categories' => '', 'colors' => 'red, blue', 'enabled' => 1]);
 }
 /**
  * Generate family requirements information to be used to
  * calculate completenesses.
  *
  * @param ProductInterface $product
  * @param ChannelInterface $channel
  *
  * @return array
  */
 protected function getFamilyRequirements(ProductInterface $product = null, ChannelInterface $channel = null)
 {
     $selectFamily = null;
     if (null !== $product) {
         $selectFamily = $product->getFamily();
     }
     $families = $this->familyRepository->getFullFamilies($selectFamily, $channel);
     $familyRequirements = [];
     foreach ($families as $family) {
         $reqsByChannels = [];
         $channels = [];
         foreach ($family->getAttributeRequirements() as $attributeReq) {
             $channel = $attributeReq->getChannel();
             $channels[$channel->getCode()] = $channel;
             if (!isset($reqsByChannels[$channel->getCode()])) {
                 $reqsByChannels[$channel->getCode()] = [];
             }
             $reqsByChannels[$channel->getCode()][] = $attributeReq;
         }
         $familyRequirements[$family->getId()] = $this->getFieldsNames($channels, $reqsByChannels);
     }
     return $familyRequirements;
 }
 /**
  * Sets the product values,
  *  - always set values related to family's attributes
  *  - sets optional values (not related to family's attributes) when a data is provided
  *  - sets optional values (not related to family's attributes) with empty data if value already exists
  *
  * @param ProductInterface $product
  * @param string           $attributeCode
  * @param array            $values
  */
 protected function updateProductValues(ProductInterface $product, $attributeCode, array $values)
 {
     $family = $product->getFamily();
     $authorizedCodes = null !== $family ? $family->getAttributeCodes() : [];
     $isFamilyAttribute = in_array($attributeCode, $authorizedCodes);
     foreach ($values as $value) {
         $hasValue = $product->getValue($attributeCode, $value['locale'], $value['scope']);
         $providedData = '' === $value['data'] || [] === $value['data'] || null === $value['data'] ? false : true;
         if ($isFamilyAttribute || $providedData || $hasValue) {
             $options = ['locale' => $value['locale'], 'scope' => $value['scope']];
             $this->propertySetter->setData($product, $attributeCode, $value['data'], $options);
         }
     }
 }
 /**
  * Set data from $actions to the given $product
  *
  * Actions should looks like that
  *
  * $actions =
  * [
  *     [
  *          'field'   => 'group',
  *          'value'   => 'summer_clothes',
  *          'options' => null
  *      ],
  *      [
  *          'field'   => 'category',
  *          'value'   => 'catalog_2013,catalog_2014',
  *          'options' => null
  *      ],
  * ]
  *
  * @param ProductInterface $product
  * @param array            $actions
  *
  * @throws \LogicException
  *
  * @return ProductInterface $product
  */
 protected function updateProduct(ProductInterface $product, array $actions)
 {
     $modifiedAttributesNb = 0;
     foreach ($actions as $action) {
         $attribute = $this->attributeRepository->findOneBy(['code' => $action['field']]);
         if (null === $attribute) {
             throw new \LogicException(sprintf('Attribute with code %s does not exist'), $action['field']);
         }
         $family = $product->getFamily();
         if (null !== $family && $family->hasAttribute($attribute)) {
             $this->productUpdater->setData($product, $action['field'], $action['value'], $action['options']);
             $modifiedAttributesNb++;
         }
     }
     if (0 === $modifiedAttributesNb) {
         $this->stepExecution->incrementSummaryInfo('skipped_products');
         $this->stepExecution->addWarning($this->getName(), 'pim_enrich.mass_edit_action.edit-common-attributes.message.no_valid_attribute', [], $product);
         return null;
     }
     return $product;
 }
 /**
  * Get expected attributes for the product
  *
  * @param ProductInterface $product
  *
  * @return AttributeInterface[]
  */
 protected function getExpectedAttributes(ProductInterface $product)
 {
     $attributes = [];
     $productAttributes = $product->getAttributes();
     foreach ($productAttributes as $attribute) {
         $attributes[$attribute->getCode()] = $attribute;
     }
     if ($family = $product->getFamily()) {
         foreach ($family->getAttributes() as $attribute) {
             $attributes[$attribute->getCode()] = $attribute;
         }
     }
     return $attributes;
 }
 function it_processes_a_product_to_create_a_configurable_product_in_magento($clientParametersRegistry, $normalizerGuesser, $groupManager, $channelManager, $productFilter, Group $group, Channel $channel, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, MagentoSoapClientParameters $clientParameters, WebserviceGuesser $webserviceGuesser, Webservice $webservice, ProductNormalizer $productNormalizer, ConfigurableNormalizer $configurableNormalizer, GroupRepository $groupRepository, Family $family)
 {
     $clientParametersRegistry->getInstance(Argument::cetera())->willReturn($clientParameters);
     $webserviceGuesser->getWebservice($clientParameters)->willReturn($webservice);
     $normalizerGuesser->getProductNormalizer(Argument::cetera())->willReturn($productNormalizer);
     $normalizerGuesser->getConfigurableNormalizer(Argument::cetera())->willReturn($configurableNormalizer);
     $webservice->getStoreViewsList()->willReturn([['store_id' => '1', 'code' => 'default', 'website_id' => '1', 'group_id' => '1', 'name' => 'Default Store View', 'sort_order' => '0', 'is_active' => '1']]);
     $webservice->getAllAttributes()->willReturn(['name' => ['attribute_id' => '71', 'code' => 'name', 'type' => 'text', 'required' => '1', 'scope' => 'store']]);
     $webservice->getAllAttributesOptions()->willReturn([]);
     $webservice->getConfigurablesStatus([1 => ['group' => $group, 'products' => [$product1, $product2, $product3]]])->willReturn([]);
     $webservice->getAttributeSetId('familyCode')->willReturn(5);
     $groupManager->getRepository()->willReturn($groupRepository);
     $groupRepository->getVariantGroupIds()->willReturn([1]);
     $channelManager->getChannelByCode('magento')->willReturn($channel);
     $channel->getId()->willReturn(3);
     $group->getId()->willReturn(1);
     $group->getProducts()->willReturn([$product1, $product2, $product3]);
     $group->getCode()->willReturn('groupCode');
     $product1->getGroups()->willReturn([$group]);
     $product1->getId()->willReturn(10);
     $product1->getFamily()->willReturn($family);
     $product2->getId()->willReturn(11);
     $product2->getFamily()->willReturn($family);
     $product3->getId()->willReturn(12);
     $product3->getFamily()->willReturn($family);
     $family->getCode()->willReturn('familyCode');
     $productFilter->apply($channel, [$product1, $product2, $product3])->willReturn([$product1, $product2, $product3]);
     $configurableNormalizer->normalize(Argument::cetera())->willReturn(['bar']);
     $this->setChannel('magento');
     $this->process([$product1])->shouldReturn([['bar']]);
 }