/** * Test related method */ public function testConstruct() { $this->assertEntity($this->attribute); $this->assertEmptyCollection($this->attribute->getOptions()); $this->assertNull($this->attribute->getAvailableLocales()); $this->assertEmptyCollection($this->attribute->getTranslations()); $this->assertFalse($this->attribute->isRequired()); $this->assertFalse($this->attribute->isUnique()); $this->assertNull($this->attribute->getDefaultValue()); $this->assertFalse($this->attribute->isLocalizable()); $this->assertFalse($this->attribute->isScopable()); $this->assertFalse($this->attribute->isUseableAsGridColumn()); $this->assertFalse($this->attribute->isUseableAsGridFilter()); $this->assertNull($this->attribute->isDecimalsAllowed()); $this->assertNull($this->attribute->isNegativeAllowed()); }
function let(SerializerInterface $serializer, AbstractAttribute $simpleAttribute) { $serializer->implement('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface'); $this->setSerializer($serializer); $simpleAttribute->isLocalizable()->willReturn(false); $simpleAttribute->isScopable()->willReturn(false); $simpleAttribute->getCode()->willReturn('simple'); }
function it_adds_a_order_by_on_an_attribute_value_in_the_query(Builder $queryBuilder, AbstractAttribute $sku) { $sku->getCode()->willReturn('sku'); $sku->isLocalizable()->willReturn(false); $sku->isScopable()->willReturn(false); $queryBuilder->sort('normalizedData.sku', 'desc')->willReturn($queryBuilder); $this->addAttributeSorter($sku, 'desc'); }
function it_adds_a_less_than_or_equals_filter_in_the_query(Builder $queryBuilder, AbstractAttribute $price) { $price->getCode()->willReturn('price'); $price->isLocalizable()->willReturn(true); $price->isScopable()->willReturn(true); $queryBuilder->field('normalizedData.price-en_US-mobile.EUR.data')->willReturn($queryBuilder); $queryBuilder->lte(22.5)->willReturn($queryBuilder); $this->addAttributeFilter($price, '<=', '22.5 EUR'); }
function it_adds_a_in_filter_on_an_attribute_value_in_the_query($qb, AbstractAttribute $color) { $color->getCode()->willReturn('color'); $color->isLocalizable()->willReturn(true); $color->isScopable()->willReturn(false); $qb->field('normalizedData.color-en_US.id')->willReturn($qb); $qb->in([1, 2])->willReturn($qb); $this->addAttributeFilter($color, 'IN', [1, 2]); }
function it_adds_a_like_filter_on_an_attribute_value_in_the_query(Builder $queryBuilder, AbstractAttribute $sku) { $sku->getCode()->willReturn('sku'); $sku->isLocalizable()->willReturn(false); $sku->isScopable()->willReturn(false); $queryBuilder->field('normalizedData.sku')->willReturn($queryBuilder); $queryBuilder->equals('my-sku')->willReturn($queryBuilder); $this->addAttributeFilter($sku, 'LIKE', 'my-sku'); }
function it_adds_a_less_than_or_equals_filter_in_the_query(Builder $queryBuilder, AbstractAttribute $metric) { $metric->getCode()->willReturn('weight'); $metric->isLocalizable()->willReturn(true); $metric->isScopable()->willReturn(true); $queryBuilder->field('normalizedData.weight-en_US-mobile.baseData')->willReturn($queryBuilder); $queryBuilder->lte(22.5)->willReturn($queryBuilder); $this->addAttributeFilter($metric, '<=', '22.5'); }
function it_normalizes_value_with_decimal_support_backend(ProductValueInterface $value, AbstractAttribute $attribute) { $attribute->getCode()->willReturn('code'); $attribute->getBackendType()->willReturn('decimal'); $attribute->isLocalizable()->willReturn(false); $attribute->isScopable()->willReturn(false); $value->getData()->willReturn('42.42'); $value->getAttribute()->willReturn($attribute); $this->normalize($value, 'mongodb_json', [])->shouldReturn(['code' => 42.42]); }
function it_returns_attribute_informations_from_field_name_with_price_attribute($managerRegistry, AttributeRepository $repository, AbstractAttribute $attribute) { $attribute->getCode()->willReturn('foo'); $attribute->isLocalizable()->willReturn(false); $attribute->isScopable()->willReturn(false); $attribute->getBackendType()->willReturn('prices'); $repository->findByReference('foo')->willReturn($attribute); $managerRegistry->getRepository(self::ATTRIBUTE_CLASS)->willReturn($repository); $this->extractAttributeFieldNameInfos('foo-USD')->shouldReturn(['attribute' => $attribute, 'locale_code' => null, 'scope_code' => null, 'price_currency' => 'USD']); }
/** * Get all channel codes * @param AbstractAttribute $attribute * * @return array */ public function getChannelCodes(AbstractAttribute $attribute = null) { $channelCodes = []; if (null === $attribute || $attribute->isScopable()) { foreach ($this->getChannels() as $channel) { $channelCodes[] = $channel->getCode(); } } return $channelCodes; }
/** * Prepare join to attribute condition with current locale and scope criterias * * @param AbstractAttribute $attribute the attribute * @param string $joinAlias the value join alias * * @return string */ public function prepareCondition(AbstractAttribute $attribute, $joinAlias) { $condition = $joinAlias . '.attribute = ' . $attribute->getId(); if ($attribute->isLocalizable()) { $condition .= ' AND ' . $joinAlias . '.locale = ' . $this->qb->expr()->literal($this->context->getLocaleCode()); } if ($attribute->isScopable()) { $condition .= ' AND ' . $joinAlias . '.scope = ' . $this->qb->expr()->literal($this->context->getScopeCode()); } return $condition; }
function it_adds_a_not_between_filter_on_an_attribute_value_in_the_query($queryBuilder, AbstractAttribute $date) { $date->getCode()->willReturn('release_date'); $date->isLocalizable()->willReturn(true); $date->isScopable()->willReturn(true); $queryBuilder->expr()->willReturn($queryBuilder); $queryBuilder->addAnd($queryBuilder)->willReturn($queryBuilder); $queryBuilder->addOr($queryBuilder)->willReturn($queryBuilder); $queryBuilder->addOr($queryBuilder)->willReturn($queryBuilder); $queryBuilder->lte(strtotime('2014-03-15'))->willReturn($queryBuilder); $queryBuilder->gte(strtotime('2014-03-20 23:59:59'))->willReturn($queryBuilder); $this->addAttributeFilter($date, ['from' => '<', 'to' => '>'], ['from' => '2014-03-15', 'to' => '2014-03-20']); }
function it_adds_a_like_filter_in_the_query(QueryBuilder $queryBuilder, AbstractAttribute $sku) { $sku->getId()->willReturn(42); $sku->getCode()->willReturn('sku'); $sku->getBackendType()->willReturn('varchar'); $sku->isLocalizable()->willReturn(false); $sku->isScopable()->willReturn(false); $queryBuilder->expr()->willReturn(new Expr()); $queryBuilder->getRootAlias()->willReturn('p'); $condition = "filtersku1.attribute = 42 AND filtersku1.varchar LIKE 'My Sku'"; $queryBuilder->innerJoin('p.values', 'filtersku1', 'WITH', $condition)->shouldBeCalled(); $this->addAttributeFilter($sku, 'LIKE', 'My Sku'); }
function it_adds_a_sorter_in_the_query(QueryBuilder $queryBuilder, AbstractAttribute $sku) { $sku->getId()->willReturn(42); $sku->getCode()->willReturn('sku'); $sku->getBackendType()->willReturn('varchar'); $sku->isLocalizable()->willReturn(false); $sku->isScopable()->willReturn(false); $queryBuilder->expr()->willReturn(new Expr()); $queryBuilder->getRootAlias()->willReturn('p'); $queryBuilder->getDQLPart('join')->willReturn([]); $queryBuilder->resetDQLPart('join')->shouldBeCalled(); $condition = "sorterVsku1.attribute = 42"; $queryBuilder->leftJoin('p.values', 'sorterVsku1', 'WITH', $condition)->shouldBeCalled(); $queryBuilder->addOrderBy('sorterVsku1.varchar', 'DESC')->shouldBeCalled(); $this->addAttributeSorter($sku, 'DESC'); }
/** * Extract informations from an attribute and exploded field name * This method is used from extractAttributeFieldNameInfos and can be redefine to add new rules * * @param AbstractAttribute $attribute * @param array $explodedFieldName * * @return array */ protected function extractAttributeInfos(AbstractAttribute $attribute, array $explodedFieldName) { if ($attribute->isLocalizable() && $attribute->isScopable()) { $localeCode = $explodedFieldName[1]; $scopeCode = $explodedFieldName[2]; $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[3] : null; } elseif ($attribute->isLocalizable()) { $localeCode = $explodedFieldName[1]; $scopeCode = null; $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[2] : null; } elseif ($attribute->isScopable()) { $localeCode = null; $scopeCode = $explodedFieldName[1]; $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[2] : null; } else { $localeCode = null; $scopeCode = null; $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[1] : null; } $priceArray = null === $priceCurrency ? [] : ['price_currency' => $priceCurrency]; return ['attribute' => $attribute, 'locale_code' => $localeCode, 'scope_code' => $scopeCode] + $priceArray; }
/** * Provides the potential column keys for this attribute * * @param AbstractAttribute $attribute * * @return array */ protected function getAttributeKeys(AbstractAttribute $attribute) { $keys = []; $keys[] = $attribute->getCode(); $updatedKeys = []; if ($attribute->isScopable() && $attribute->isLocalizable()) { foreach ($this->getLocales() as $locale) { foreach ($this->getChannels() as $channel) { foreach ($keys as $baseKey) { $key = $baseKey . '-' . $locale->getCode() . '-' . $channel->getCode(); $updatedKeys[] = $key; } } } $keys = $updatedKeys; } elseif ($attribute->isScopable() && !$attribute->isLocalizable()) { foreach ($this->getChannels() as $channel) { foreach ($keys as $baseKey) { $key = $baseKey . '-' . $channel->getCode(); $updatedKeys[] = $key; } } $keys = $updatedKeys; } elseif (!$attribute->isScopable() && $attribute->isLocalizable()) { foreach ($this->getLocales() as $locale) { foreach ($keys as $baseKey) { $key = $baseKey . '-' . $locale->getCode(); $updatedKeys[] = $key; } } $keys = $updatedKeys; } switch ($attribute->getBackendType()) { case 'prices': $updatedKeys = []; foreach ($keys as $key) { foreach ($this->getCurrencies() as $currency) { $updatedKeys[] = $key . '-' . $currency->getCode(); } } $keys = $updatedKeys; break; case 'metric': $updatedKeys = []; foreach ($keys as $key) { $updatedKeys[] = $key; $updatedKeys[] = $key . '-' . self::METRIC_UNIT; } $keys = $updatedKeys; break; } return $keys; }
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]); }
/** * Sets the attribute * * @param AbstractAttribute $attribute * * @throws ColumnLabelException */ public function setAttribute(AbstractAttribute $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 { $this->propertyPath = $attribute->getBackendType(); $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', array('%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', array('%column%' => $this->label)); } } $this->suffixes = $suffixes; } }
/** * Get the name of a normalized data field * * @param AbstractAttribute $attribute * @param Channel $channel * @param Locale $locale * * @return string */ protected function getNormalizedFieldName(AbstractAttribute $attribute, Channel $channel, Locale $locale) { $suffix = ''; if ($attribute->isLocalizable()) { $suffix = sprintf('-%s', $locale->getCode()); } if ($attribute->isScopable()) { $suffix .= sprintf('-%s', $channel->getCode()); } return $attribute->getCode() . $suffix; }
/** * Add all the values required by the given attribute * Locale is not present because we current locale is bound at the same time as values during form submission * * @param AbstractAttribute $attribute */ protected function addValues(AbstractAttribute $attribute) { $locale = $this->getLocale(); if ($attribute->isScopable()) { foreach ($locale->getChannels() as $channel) { $key = $attribute->getCode() . '_' . $channel->getCode(); $this->values[$key] = $this->createValue($attribute, $locale->getCode(), $channel->getCode()); } } else { $this->values[$attribute->getCode()] = $this->createValue($attribute, $locale->getCode()); } }
/** * Prepare attribute view * * @param AbstractAttribute $attribute * @param ProductValueInterface $value * @param FormView $view * * @return array */ protected function prepareAttributeView(AbstractAttribute $attribute, ProductValueInterface $value, FormView $view) { $attributeView = array('id' => $attribute->getId(), 'isRemovable' => $value->isRemovable(), 'code' => $attribute->getCode(), 'label' => $attribute->getLabel(), 'sortOrder' => $attribute->getSortOrder(), 'allowValueCreation' => in_array($attribute->getAttributeType(), $this->choiceAttributeTypes), 'locale' => $value->getLocale()); if ($attribute->isScopable()) { $attributeView['values'] = array_merge($this->getAttributeValues($attribute, $value->getLocale()), array($value->getScope() => $view)); ksort($attributeView['values']); } else { $attributeView['value'] = $view; } $classes = $this->getAttributeClasses($attribute); if (!empty($classes)) { $attributeView['classes'] = $classes; } return $attributeView; }
/** * Normalize the field name from attribute and catalog context * * @param AbstractAttribute $attribute * @param CatalogContext $context * * @return string */ public static function getNormalizedValueFieldFromAttribute(AbstractAttribute $attribute, CatalogContext $context) { return self::getNormalizedValueField($attribute->getCode(), $attribute->isLocalizable(), $attribute->isScopable(), $attribute->isLocalizable() ? $context->getLocaleCode() : null, $attribute->isScopable() ? $context->getScopeCode() : null); }
function it_generates_attribute_indexes_when_saving_filterable_scopable_and_localizable_attribute($collection, $namingUtility, AbstractAttribute $description) { $description->getCode()->willReturn('description'); $description->getBackendType()->willReturn('varchar'); $description->isLocalizable()->willReturn(true); $description->isScopable()->willReturn(true); $description->isUseableAsGridFilter()->willReturn(true); $description->getAttributeType()->willReturn('pim_catalog_simpleselect'); $namingUtility->getAttributeNormFields($description)->willReturn(['normalizedData.description-en_US-ecommerce', 'normalizedData.description-de_DE-ecommerce', 'normalizedData.description-en_US-mobile']); $this->ensureIndexesFromAttribute($description); }
/** * @param AbstractAttribute $attribute * @param string $locale * @param string $scope * * @return string */ protected function getValueCode(AbstractAttribute $attribute, $locale, $scope) { $valueCode = $attribute->getCode(); if ($attribute->isLocalizable()) { $valueCode .= '_' . $locale; } if ($attribute->isScopable()) { $valueCode .= '_' . $scope; } return $valueCode; }