Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function addAttributeFilter(AttributeInterface $attribute, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     try {
         $options = $this->resolver->resolve($options);
     } catch (\Exception $e) {
         throw InvalidArgumentException::expectedFromPreviousException($e, $attribute->getCode(), 'filter', 'string');
     }
     $this->checkLocaleAndScope($attribute, $locale, $scope, 'string');
     if (Operators::IS_EMPTY !== $operator && Operators::IS_NOT_EMPTY !== $operator) {
         $this->checkValue($options['field'], $value);
     }
     $joinAlias = $this->getUniqueAlias('filter' . $attribute->getCode());
     $backendField = sprintf('%s.%s', $joinAlias, $attribute->getBackendType());
     if (Operators::IS_EMPTY === $operator) {
         $this->qb->leftJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $this->prepareAttributeJoinCondition($attribute, $joinAlias, $locale, $scope));
         $this->qb->andWhere($this->prepareCriteriaCondition($backendField, $operator, $value));
     } else {
         $condition = $this->prepareAttributeJoinCondition($attribute, $joinAlias, $locale, $scope);
         if (Operators::IS_NOT_EMPTY === $operator) {
             $condition .= sprintf('AND (%s AND %s)', $this->qb->expr()->isNotNull($backendField), $this->qb->expr()->neq($backendField, $this->qb->expr()->literal('')));
             $this->qb->innerJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $condition);
         } elseif (Operators::DOES_NOT_CONTAIN === $operator) {
             $whereCondition = $this->prepareCondition($backendField, $operator, $value) . ' OR ' . $this->prepareCondition($backendField, Operators::IS_NULL, null);
             $this->qb->leftJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $condition);
             $this->qb->andWhere($whereCondition);
         } else {
             $condition .= ' AND ' . $this->prepareCondition($backendField, $operator, $value);
             $this->qb->innerJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $condition);
         }
     }
     return $this;
 }
 function it_normalize_completnesses_and_index_them($normalizer, AttributeInterface $name, AttributeInterface $description)
 {
     $normalizer->normalize('completeness', 'internal_api', ['a_context_key' => 'context_value'])->willReturn('normalized_completeness');
     $name->getCode()->willReturn('name');
     $description->getCode()->willReturn('description');
     $this->normalize(['en_US' => ['channels' => ['mobile' => ['missing' => [$name, $description], 'completeness' => 'completeness'], 'print' => ['missing' => [$name, $description], 'completeness' => 'completeness'], 'tablet' => ['missing' => [$name, $description], 'completeness' => 'completeness']]], 'fr_FR' => ['channels' => ['mobile' => ['missing' => [$name, $description], 'completeness' => 'completeness'], 'print' => ['missing' => [$name, $description], 'completeness' => 'completeness'], 'tablet' => ['missing' => [$name, $description], 'completeness' => 'completeness']]]], 'internal_api', ['a_context_key' => 'context_value'])->shouldReturn(['en_US' => ['channels' => ['mobile' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness'], 'print' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness'], 'tablet' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness']]], 'fr_FR' => ['channels' => ['mobile' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness'], 'print' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness'], 'tablet' => ['missing' => ['name', 'description'], 'completeness' => 'normalized_completeness']]]]);
 }
Пример #3
0
 function it_does_not_support_other_attributes($identifier, $textarea, AttributeInterface $image)
 {
     $this->supportAttribute($identifier)->shouldReturn(false);
     $this->supportAttribute($textarea)->shouldReturn(false);
     $image->getAttributeType()->willReturn('pim_catalog_image');
     $this->supportAttribute($image)->shouldReturn(false);
 }
 function it_copies_simple_select_value_to_a_product_value($builder, $attrValidatorHelper, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product1, ProductInterface $product2, ProductInterface $product3, ProductInterface $product4, ProductValueInterface $fromProductValue, ProductValueInterface $toProductValue, AttributeOptionInterface $attributeOption)
 {
     $fromLocale = 'fr_FR';
     $toLocale = 'fr_FR';
     $toScope = 'mobile';
     $fromScope = 'mobile';
     $fromAttribute->getCode()->willReturn('fromAttributeCode');
     $toAttribute->getCode()->willReturn('toAttributeCode');
     $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled();
     $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled();
     $fromProductValue->getData()->willReturn($attributeOption);
     $toProductValue->setOption($attributeOption)->shouldBeCalledTimes(3);
     $product1->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product1->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue);
     $product2->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn(null);
     $product2->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue);
     $product3->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product3->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null);
     $product4->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product4->getValue('toAttributeCode', $toLocale, $toScope)->willReturn($toProductValue);
     $builder->addProductValue($product3, $toAttribute, $toLocale, $toScope)->shouldBeCalledTimes(1)->willReturn($toProductValue);
     $products = [$product1, $product2, $product3, $product4];
     foreach ($products as $product) {
         $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]);
     }
 }
 function it_allows_setting_attribute_data_option_to_null(ProductInterface $product, AttributeInterface $attribute, ProductValueInterface $value)
 {
     $attribute->getCode()->willReturn('choice');
     $product->getValue('choice', 'fr_FR', 'mobile')->shouldBeCalled()->willReturn($value);
     $value->setOption(null)->shouldBeCalled();
     $this->setAttributeData($product, $attribute, null, ['locale' => 'fr_FR', 'scope' => 'mobile']);
 }
 /**
  * Returns available information for the attribute and filters which supports it
  *
  * @param AttributeInterface $attribute
  * @param array              $attributeFilters
  *
  * @return array
  */
 protected function getFilterInformationForAttribute(AttributeInterface $attribute, array $attributeFilters)
 {
     $field = $attribute->getCode();
     $attributeType = $attribute->getAttributeType();
     $isLocalizable = $attribute->isLocalizable() ? 'yes' : 'no';
     $isScopable = $attribute->isScopable() ? 'yes' : 'no';
     $newEntries = [];
     if (array_key_exists($attributeType, $attributeFilters)) {
         foreach ($attributeFilters[$attributeType] as $filter) {
             $class = get_class($filter);
             $operators = implode(', ', $filter->getOperators());
             $newEntries[] = [$field, $isLocalizable, $isScopable, $attributeType, $operators, $class];
         }
         return $newEntries;
     }
     if ($attribute->isBackendTypeReferenceData()) {
         foreach ($this->registry->getAttributeFilters() as $filter) {
             if ($filter->supportsAttribute($attribute)) {
                 $class = get_class($filter);
                 $operators = implode(', ', $filter->getOperators());
                 $newEntries[] = [$field, $isLocalizable, $isScopable, $attributeType, $operators, $class];
             }
         }
         return $newEntries;
     }
     return [[$field, $isLocalizable, $isScopable, $attributeType, '', 'Not supported']];
 }
 function it_throws_an_exception_when_the_locale_is_not_provided($qb, AttributeInterface $attribute)
 {
     $attribute->getCode()->willReturn('my_code');
     $attribute->getBackendType()->willReturn('options');
     $attribute->getAttributeType()->willReturn('pim_catalog_simpleselect');
     $this->shouldThrow('\\InvalidArgumentException')->duringAddAttributeSorter($attribute, 'desc', null, 'ecommerce');
 }
Пример #8
0
 function it_checks_if_attribute_is_supported(AttributeInterface $goodAttribute, AttributeInterface $badAttribute)
 {
     $goodAttribute->getAttributeType()->willReturn('acme_attribute_type');
     $badAttribute->getAttributeType()->willReturn('acme_other_attribute_type');
     $this->supportsAttribute($goodAttribute)->shouldReturn(true);
     $this->supportsAttribute($badAttribute)->shouldReturn(false);
 }
Пример #9
0
 /**
  * Sets the attribute
  *
  * @param AttributeInterface $attribute
  *
  * @throws ColumnLabelException
  */
 public function setAttribute(AttributeInterface $attribute = null)
 {
     $this->attribute = $attribute;
     if (null === $attribute) {
         $this->locale = null;
         $this->scope = null;
         $this->suffixes = $this->rawSuffixes;
         $this->propertyPath = lcfirst(Inflector::classify($this->name));
     } else {
         if (!in_array($attribute->getBackendType(), [AbstractAttributeType::BACKEND_TYPE_REF_DATA_OPTION, AbstractAttributeType::BACKEND_TYPE_REF_DATA_OPTIONS])) {
             $this->propertyPath = $attribute->getBackendType();
         } else {
             $this->propertyPath = $attribute->getReferenceDataName();
         }
         $suffixes = $this->rawSuffixes;
         if ($attribute->isLocalizable()) {
             if (count($suffixes)) {
                 $this->locale = array_shift($suffixes);
             } else {
                 throw new ColumnLabelException('The column "%column%" must contain a locale code', ['%column%' => $this->label]);
             }
         }
         if ($attribute->isScopable()) {
             if (count($suffixes)) {
                 $this->scope = array_shift($suffixes);
             } else {
                 throw new ColumnLabelException('The column "%column%" must contain a scope code', ['%column%' => $this->label]);
             }
         }
         $this->suffixes = $suffixes;
     }
 }
 function it_denormalizes_data_into_reference_data($resolver, AttributeInterface $attribute, ReferenceDataInterface $battlecruiser, ReferenceDataRepository $referenceDataRepo)
 {
     $attribute->getReferenceDataName()->willReturn('starship');
     $resolver->resolve('starship')->willReturn($referenceDataRepo);
     $referenceDataRepo->findOneBy(['code' => 'battlecruiser'])->willReturn($battlecruiser);
     $this->denormalize('battlecruiser', 'pim_reference_data_simpleselect', 'json', ['attribute' => $attribute])->shouldReturn($battlecruiser);
 }
 function it_removes_attributes_from_a_product_template(ProductTemplateInterface $template, AttributeInterface $name)
 {
     $name->getCode()->willReturn('name');
     $template->getValuesData()->willReturn(['name' => 'foo', 'color' => 'bar']);
     $template->setValuesData(['color' => 'bar'])->shouldBeCalled();
     $this->removeAttribute($template, $name);
 }
 function it_returns_well_formatted_actions_for_batch_job(AttributeInterface $attrColor, AttributeInterface $attrSize, ChannelInterface $channelMobile, ChannelInterface $channelEcommerce, AttributeRequirementInterface $colorMobileRequirement, AttributeRequirementInterface $colorEcommerceRequirement, AttributeRequirementInterface $sizeEcommerceRequirement)
 {
     $attrColor->getCode()->willReturn('color');
     $attrSize->getCode()->willReturn('size');
     $channelMobile->getCode()->willReturn('mobile');
     $channelEcommerce->getCode()->willReturn('ecommerce');
     $colorMobileRequirement->getAttribute()->willReturn($attrColor);
     $colorEcommerceRequirement->getAttribute()->willReturn($attrColor);
     $sizeEcommerceRequirement->getAttribute()->willReturn($attrSize);
     $colorMobileRequirement->getChannel()->willReturn($channelMobile);
     $colorEcommerceRequirement->getChannel()->willReturn($channelEcommerce);
     $sizeEcommerceRequirement->getChannel()->willReturn($channelEcommerce);
     $colorMobileRequirement->isRequired()->willReturn(false);
     $colorEcommerceRequirement->isRequired()->willReturn(true);
     $sizeEcommerceRequirement->isRequired()->willReturn(true);
     $colorMobileRequirement->getAttributeCode()->willReturn('color');
     $colorEcommerceRequirement->getAttributeCode()->willReturn('color');
     $sizeEcommerceRequirement->getAttributeCode()->willReturn('size');
     $colorMobileRequirement->getChannelCode()->willReturn('mobile');
     $colorEcommerceRequirement->getChannelCode()->willReturn('ecommerce');
     $sizeEcommerceRequirement->getChannelCode()->willReturn('ecommerce');
     $this->addAttributeRequirement($colorMobileRequirement);
     $this->addAttributeRequirement($colorEcommerceRequirement);
     $this->addAttributeRequirement($sizeEcommerceRequirement);
     $this->getActions()->shouldReturn([['attribute_code' => 'color', 'channel_code' => 'mobile', 'is_required' => false], ['attribute_code' => 'color', 'channel_code' => 'ecommerce', 'is_required' => true], ['attribute_code' => 'size', 'channel_code' => 'ecommerce', 'is_required' => true]]);
 }
Пример #13
0
 /**
  * {@inheritdoc}
  */
 public function addAttributeFilter(AttributeInterface $attribute, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     try {
         $options = $this->resolver->resolve($options);
     } catch (\Exception $e) {
         throw InvalidArgumentException::expectedFromPreviousException($e, $attribute->getCode(), 'filter', 'option');
     }
     $this->checkLocaleAndScope($attribute, $locale, $scope, 'option');
     $field = $options['field'];
     if (Operators::IS_EMPTY !== $operator && Operators::IS_NOT_EMPTY !== $operator) {
         $this->checkValue($field, $value);
     }
     $joinAlias = $this->getUniqueAlias('filter' . $attribute->getCode());
     // prepare join value condition
     $optionAlias = $joinAlias . '.option';
     if (Operators::IS_EMPTY === $operator || Operators::IS_NOT_EMPTY === $operator) {
         $this->qb->leftJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $this->prepareAttributeJoinCondition($attribute, $joinAlias, $locale, $scope));
         $this->qb->andWhere($this->prepareCriteriaCondition($optionAlias, $operator, null));
     } else {
         // inner join to value
         $condition = $this->prepareAttributeJoinCondition($attribute, $joinAlias, $locale, $scope);
         if (FieldFilterHelper::getProperty($field) === FieldFilterHelper::CODE_PROPERTY) {
             $value = $this->objectIdResolver->getIdsFromCodes('option', $value, $attribute);
         }
         $condition .= ' AND ' . $this->prepareCriteriaCondition($optionAlias, $operator, $value);
         $this->qb->innerJoin($this->qb->getRootAlias() . '.values', $joinAlias, 'WITH', $condition);
     }
     return $this;
 }
 function it_supports_date_attributes(AttributeInterface $dateAtt, AttributeInterface $otherAtt)
 {
     $dateAtt->getAttributeType()->willReturn('pim_catalog_date');
     $this->supportsAttribute($dateAtt)->shouldReturn(true);
     $otherAtt->getAttributeType()->willReturn('pim_catalog_other');
     $this->supportsAttribute($otherAtt)->shouldReturn(false);
 }
 function it_throws_an_error_if_attribute_data_value_does_not_contain_valid_currency($currencyManager, AttributeInterface $attribute, ProductInterface $product)
 {
     $attribute->getCode()->willReturn('attributeCode');
     $currencyManager->getActiveCodes()->willReturn(['EUR', 'USD']);
     $data = [['data' => 123, 'currency' => 'invalid currency']];
     $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'currency', 'The currency does not exist', 'remover', 'prices collection', 'invalid currency'))->during('removeAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]);
 }
 /**
  * Set data into product value
  *
  * @param ProductInterface   $product
  * @param AttributeInterface $attribute
  * @param mixed              $data
  * @param string             $locale
  * @param string             $scope
  */
 protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $locale, $scope)
 {
     $value = $product->getValue($attribute->getCode(), $locale, $scope);
     if (null === $value) {
         $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope);
     }
     $value->setData($data);
 }
 function it_returns_empty_data_if_empty_value_provided(AttributeInterface $attribute)
 {
     $attribute->getCode()->willReturn('attribute_code');
     $fieldNameInfo = ['attribute' => $attribute, 'locale_code' => 'en_US', 'scope_code' => 'mobile'];
     $value = '';
     $expectedResult = ['attribute_code' => [['locale' => 'en_US', 'scope' => 'mobile', 'data' => null]]];
     $this->convert($fieldNameInfo, $value)->shouldReturn($expectedResult);
 }
Пример #18
0
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AttributeInterface $attribute)
 {
     $constraints = [];
     if ('regexp' === $attribute->getValidationRule() && ($pattern = $attribute->getValidationRegexp())) {
         $constraints[] = new Assert\Regex(['pattern' => $pattern]);
     }
     return $constraints;
 }
 function it_denormalizes_data_into_metric(AttributeInterface $attribute, $factory, MetricInterface $metric)
 {
     $attribute->getMetricFamily()->willReturn('Frequency');
     $factory->createMetric('Frequency')->shouldBeCalled()->willReturn($metric);
     $metric->setData(3)->shouldBeCalled();
     $metric->setUnit('GIGAHERTZ')->shouldBeCalled();
     $this->denormalize(['data' => 3, 'unit' => 'GIGAHERTZ'], 'pim_catalog_metric', 'json', ['attribute' => $attribute])->shouldReturn($metric);
 }
 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']);
 }
 /**
  * Validate currency of a price
  *
  * @param AttributeInterface|MetricInterface|ProductValueInterface $object
  * @param Constraint                                               $constraint
  */
 public function validate($object, Constraint $constraint)
 {
     if ($object instanceof ProductPriceInterface) {
         if (!in_array($object->getCurrency(), $this->getCurrencyCodes())) {
             $this->context->buildViolation($constraint->unitMessage)->atPath('currency')->addViolation();
         }
     }
 }
 function it_throws_an_exception_when_unit_families_are_not_the_same(AttributeInterface $description, AttributeInterface $name)
 {
     $description->getCode()->willReturn('description');
     $name->getCode()->willReturn('name');
     $description->getMetricFamily()->willReturn('Weight');
     $name->getMetricFamily()->willReturn('Distance');
     $this->shouldThrow(new \LogicException('Metric families are not the same for attributes: "description" and "name".'))->during('validateUnitFamilies', [$description, $name]);
 }
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AttributeInterface $attribute)
 {
     $constraints = [];
     if (!$attribute->isDecimalsAllowed()) {
         $constraints[] = new NotDecimal();
     }
     return $constraints;
 }
 function it_denormalizes_data_into_number_with_french_format($valuesDenormalizer, $localizer, AttributeInterface $attribute)
 {
     $attribute->getAttributeType()->willReturn('pim_catalog_number');
     $options = ['attribute' => $attribute, 'locale' => 'fr_FR'];
     $valuesDenormalizer->denormalize(3.85, 'Pim\\Component\\Catalog\\Model\\ProductValue', 'json', $options)->willReturn(3.85);
     $localizer->localize(3.85, $options)->willReturn('3,85');
     $this->denormalize(3.85, 'Pim\\Component\\Catalog\\Model\\ProductValue', 'json', $options)->shouldReturn('3,85');
 }
Пример #25
0
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AttributeInterface $attribute)
 {
     $constraints = [];
     if ($attribute->isRequired()) {
         $constraints[] = new Assert\NotBlank();
     }
     return $constraints;
 }
 /**
  * @param AttributeInterface $attribute
  * @param array              $locales
  *
  * @return array
  */
 protected function normalizeAttributeLabels(AttributeInterface $attribute, array $locales)
 {
     $labels = [];
     foreach ($locales as $locale) {
         $labels[$locale] = $attribute->getTranslation($locale)->getLabel();
     }
     return $labels;
 }
 function let(SerializerInterface $serializer, AttributeInterface $simpleAttribute)
 {
     $serializer->implement('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
     $this->setSerializer($serializer);
     $simpleAttribute->isLocalizable()->willReturn(false);
     $simpleAttribute->isScopable()->willReturn(false);
     $simpleAttribute->getCode()->willReturn('simple');
 }
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AttributeInterface $attribute)
 {
     $constraints = [];
     if ($attribute->isUnique()) {
         $constraints[] = new UniqueValue();
     }
     return $constraints;
 }
Пример #29
0
 /**
  * {@inheritdoc}
  */
 public function guessConstraints(AttributeInterface $attribute)
 {
     $constraints = [];
     if ('url' === $attribute->getValidationRule()) {
         $constraints[] = new Assert\Url();
     }
     return $constraints;
 }
 /**
  * As boolean attribute does not support "null" value (only true/false),
  * there are some problems to make out what should really be added to the product (specially with optional attributes)
  * What we add in product:
  * | old value | new value | attribute is in family | should be added in product |
  * | null      | false     | yes                    | no                         |
  * | null      | false     | no                     | yes                        |
  *
  * This method should be removed when boolean attribute will be rework to support 3 states: true/false/null
  *
  * @deprecated will be removed in 1.7
  *
  * @param ProductInterface   $product    product to update
  * @param AttributeInterface $attribute  attribute
  * @param mixed              $data       new value
  *
  * @return bool
  */
 private function shouldBeSetInProduct(ProductInterface $product, AttributeInterface $attribute, $data)
 {
     $family = $product->getFamily();
     if (null !== $family && in_array($attribute->getCode(), $product->getFamily()->getAttributeCodes()) && false === $data) {
         return false;
     }
     return true;
 }