/**
  * {@inheritdoc}
  */
 public function prepareValueFormData(ProductValueInterface $value)
 {
     if (!is_null($value->getData())) {
         return $value->getData();
     }
     $data = $this->metricFactory->createMetric($value->getAttribute()->getMetricFamily());
     $data->setData($value->getAttribute()->getDefaultValue());
     return $data;
 }
 /**
  * Validate a more complex value that doesn't store the data itself
  *
  * @param ProductValueInterface $value
  * @param Constraint            $constraint
  *
  * @return null
  */
 protected function validateComplexValue(ProductValueInterface $value, Constraint $constraint)
 {
     if ($value->getAttribute()) {
         $backendType = $value->getAttribute()->getBackendType();
         if ($backendType === 'prices') {
             $this->validatePrices($value, $constraint);
         } elseif ($backendType === 'media') {
             $this->validateMedia($value, $constraint);
         } elseif ($backendType === 'metric') {
             $this->validateMetric($value, $constraint);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function prepareValueFormData(ProductValueInterface $value)
 {
     if ($value->getData() && $value->getData()->isEmpty()) {
         return $value->getAttribute()->getDefaultValue();
     }
     $iterator = $value->getData()->getIterator();
     if (true === $value->getAttribute()->getProperty('autoOptionSorting')) {
         $iterator->uasort('strcasecmp');
     } else {
         $iterator->uasort(function ($first, $second) {
             return $first->getSortOrder() < $second->getSortOrder() ? -1 : 1;
         });
     }
     return new ArrayCollection(iterator_to_array($iterator));
 }
 /**
  * {@inheritdoc}
  */
 public function prepareValueFormOptions(ProductValueInterface $value)
 {
     $referenceDataConf = $this->referenceDataRegistry->get($value->getAttribute()->getReferenceDataName());
     $options = parent::prepareValueFormOptions($value);
     $options['class'] = $referenceDataConf->getClass();
     return $options;
 }
 function it_converts_metric_values_given_the_configured_base_unit_in_the_channel($converter, ProductValueInterface $weightValue, ProductValueInterface $surfaceValue, ProductValueInterface $nameValue, AttributeInterface $weight, AttributeInterface $surface, AttributeInterface $name, MetricInterface $weightMetric, MetricInterface $surfaceMetric, ProductInterface $product, ChannelInterface $channel)
 {
     $weightValue->getAttribute()->willReturn($weight);
     $weightValue->getData()->willReturn($weightMetric);
     $weight->getCode()->willReturn('weight');
     $weightMetric->getFamily()->willReturn('Weight');
     $weightMetric->getUnit()->willReturn('KILOGRAM');
     $weightMetric->getData()->willReturn(1);
     $surfaceValue->getAttribute()->willReturn($surface);
     $surfaceValue->getData()->willReturn($surfaceMetric);
     $surface->getCode()->willReturn('surface');
     $surfaceMetric->getFamily()->willReturn('Surface');
     $surfaceMetric->getUnit()->willReturn('METER_SQUARE');
     $surfaceMetric->getData()->willReturn(10);
     $nameValue->getAttribute()->willReturn($name);
     $nameValue->getData()->willReturn('foobar');
     $product->getValues()->willReturn(array($weightValue, $surfaceValue, $nameValue));
     $channel->getConversionUnits()->willReturn(array('weight' => 'GRAM'));
     $converter->setFamily('Weight')->shouldBeCalled();
     $converter->convert('KILOGRAM', 'GRAM', 1)->willReturn(0.001);
     $converter->setFamily('Surface')->shouldNotBeCalled();
     $weightMetric->setData(0.001)->shouldBeCalled();
     $weightMetric->setUnit('GRAM')->shouldBeCalled();
     $this->convert($product, $channel);
 }
 function let(AttributeConstraintGuesser $guesser, ProductValueInterface $value, AttributeInterface $color, AttributeOptionInterface $red)
 {
     $value->getAttribute()->willReturn($color);
     $value->getData()->willReturn(new ArrayCollection([$red]));
     $color->getBackendType()->willReturn(AbstractAttributeType::BACKEND_TYPE_OPTIONS);
     $this->beConstructedWith(AbstractAttributeType::BACKEND_TYPE_OPTIONS, 'pim_ajax_entity', $guesser);
 }
 /**
  * {@inheritdoc}
  */
 public function prepareValueFormAlias(ProductValueInterface $value)
 {
     if ($value->getAttribute()->isWysiwygEnabled()) {
         return 'pim_wysiwyg';
     }
     return parent::prepareValueFormAlias($value);
 }
 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_adds_multiple_product_values_children_in_the_same_group(ProductValueInterface $valueOne, AttributeInterface $attributeOne, ProductValueInterface $valueTwo, AttributeInterface $attributeTwo, AttributeGroup $group, FormView $valueFormView, $viewUpdaterRegistry)
 {
     $valueOne->getAttribute()->willReturn($attributeOne);
     $valueOne->isRemovable()->willReturn(true);
     $valueOne->getLocale()->willReturn(null);
     $valueOne->getEntity()->willReturn(null);
     $attributeOne->getGroup()->willReturn($group);
     $attributeOne->getId()->willReturn(42);
     $attributeOne->getCode()->willReturn('name');
     $attributeOne->getLabel()->willReturn('Name');
     $attributeOne->getSortOrder()->willReturn(10);
     $attributeOne->getAttributeType()->willReturn('pim_catalog_text');
     $attributeOne->isLocalizable()->willReturn(false);
     $attributeOne->isScopable()->willReturn(false);
     $valueTwo->getAttribute()->willReturn($attributeTwo);
     $valueTwo->isRemovable()->willReturn(true);
     $valueTwo->getLocale()->willReturn(null);
     $valueTwo->getEntity()->willReturn(null);
     $attributeTwo->getGroup()->willReturn($group);
     $attributeTwo->getId()->willReturn(47);
     $attributeTwo->getCode()->willReturn('description');
     $attributeTwo->getLabel()->willReturn('Description');
     $attributeTwo->getSortOrder()->willReturn(15);
     $attributeTwo->getAttributeType()->willReturn('pim_catalog_text');
     $attributeTwo->isLocalizable()->willReturn(false);
     $attributeTwo->isScopable()->willReturn(false);
     $group->getId()->willReturn(1);
     $group->getCode()->willReturn('general');
     $group->getLabel()->willReturn('General');
     $this->addChildren($valueOne, $valueFormView);
     $this->addChildren($valueTwo, $valueFormView);
     $viewUpdaterRegistry->getUpdaters()->willReturn([]);
     $resultView = [1 => ['label' => 'General', 'attributes' => ['name' => ['id' => 42, 'isRemovable' => true, 'code' => 'name', 'label' => 'Name', 'sortOrder' => 10, 'allowValueCreation' => false, 'locale' => null, 'value' => $valueFormView], 'description' => ['id' => 47, 'isRemovable' => true, 'code' => 'description', 'label' => 'Description', 'sortOrder' => 15, 'allowValueCreation' => false, 'locale' => null, 'value' => $valueFormView]]]];
     $this->getView()->shouldReturn($resultView);
 }
 function it_denormalizes_data_into_reference_data($resolver, AttributeInterface $attribute, ReferenceDataInterface $battlecruiser, ReferenceDataRepository $referenceDataRepo, ProductValueInterface $productValue)
 {
     $attribute->getReferenceDataName()->willReturn('starship');
     $productValue->getAttribute()->willReturn($attribute);
     $resolver->resolve('starship')->willReturn($referenceDataRepo);
     $referenceDataRepo->findOneBy(['code' => 'battlecruiser'])->willReturn($battlecruiser);
     $this->denormalize('battlecruiser', 'pim_reference_data_simpleselect', 'csv', ['value' => $productValue])->shouldReturn($battlecruiser);
 }
 public function it_suports_media_attribute(ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $productValue->getAttribute()->willReturn($attribute);
     $attribute->getBackendType()->willReturn('media');
     $this->supportsValue($productValue)->shouldReturn(true);
     $attribute->getBackendType()->willReturn('other');
     $this->supportsValue($productValue)->shouldReturn(false);
 }
 /**
  * @param ProductValueInterface $productValue
  *
  * @return string
  */
 protected function getUniqueValueCode(ProductValueInterface $productValue)
 {
     $attributeCode = $productValue->getAttribute()->getCode();
     $uniqueValueCode = $attributeCode;
     $uniqueValueCode .= null !== $productValue->getLocale() ? $productValue->getLocale() : '';
     $uniqueValueCode .= null !== $productValue->getScope() ? $productValue->getScope() : '';
     return $uniqueValueCode;
 }
 function it_denormalizes_a_collection_of_reference_data_values($refDataDenormalizer, AttributeInterface $attribute, ReferenceDataInterface $refData1, ReferenceDataInterface $refData2, ProductValueInterface $productValue)
 {
     $productValue->getAttribute()->willReturn($attribute);
     $context = ['value' => $productValue];
     $refDataDenormalizer->denormalize('battlecruiser', 'pim_reference_data_multiselect', null, $context)->shouldBeCalled()->willReturn($refData1);
     $refDataDenormalizer->denormalize('destroyer', 'pim_reference_data_multiselect', null, $context)->shouldBeCalled()->willReturn($refData2);
     $this->denormalize('battlecruiser,destroyer', 'pim_reference_data_multiselect', null, $context)->shouldHaveCount(2);
 }
 public function it_suports_metric_attribute(ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $productValue->getAttribute()->willReturn($attribute);
     $attribute->getAttributeType()->willReturn('pim_catalog_metric');
     $this->supportsValue($productValue)->shouldReturn(true);
     $attribute->getAttributeType()->willReturn('other');
     $this->supportsValue($productValue)->shouldReturn(false);
 }
 public function it_succesfully_checks_complete_attribute(ProductValueInterface $value, ChannelInterface $channel, LocaleInterface $locale, ProductValueCompleteCheckerInterface $completenessChecker, AttributeInterface $attribute)
 {
     $value->getAttribute()->willReturn($attribute);
     $value->getData()->willReturn('foo');
     $this->addProductValueChecker($completenessChecker);
     $completenessChecker->supportsValue($value)->willReturn(true);
     $completenessChecker->isComplete($value, $channel, $locale)->willReturn(true);
     $this->isComplete($value, $channel, $locale)->shouldReturn(true);
 }
 function it_doesnt_support_multi_targets_constraint($guesser, $factory, ClassMetadata $metadata, ProductValueInterface $value, AttributeInterface $attribute, Constraint $multiTargets, Constraint $validNumber)
 {
     $factory->createMetadata(Argument::any())->willReturn($metadata);
     $value->getAttribute()->willReturn($attribute);
     $attribute->getBackendType()->willReturn('varchar');
     $guesser->guessConstraints($attribute)->willReturn([$multiTargets]);
     $multiTargets->getTargets()->willReturn([Constraint::PROPERTY_CONSTRAINT, Constraint::CLASS_CONSTRAINT]);
     $this->shouldThrow(new \LogicException('No support provided for constraint on many targets'))->duringGetMetadataFor($value);
 }
 function it_adds_violation_when_validates_non_string_product_value($context, string $stringConstraint, ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $productValue->getAttribute()->willReturn($attribute);
     $attribute->getCode()->willReturn('foo');
     $attribute->getBackendType()->willReturn('integer');
     $productValue->getInteger()->willReturn(666);
     $context->addViolation($stringConstraint->message, ['%attribute%' => 'foo', '%givenType%' => 'integer'])->shouldBeCalled();
     $this->validate($productValue, $stringConstraint);
 }
 function it_doesnt_remove_value_when_the_attribute_is_not_locale_specific(FormEvent $event, FormInterface $form, FormInterface $field, FormInterface $rootForm, ProductValueInterface $nameValue, AttributeInterface $nameAttribute)
 {
     $event->getForm()->willReturn($form);
     $event->getData()->willReturn(['name' => $nameValue]);
     $nameValue->getAttribute()->willReturn($nameAttribute);
     $nameAttribute->isLocaleSpecific()->willReturn(false);
     $form->remove('name')->shouldNotBeCalled();
     $this->preSetData($event);
 }
 function it_generates_the_media_filename_prefix(ProductValueInterface $fileValue, AttributeInterface $file)
 {
     $fileValue->getAttribute()->willReturn($file);
     $file->getCode()->willReturn('file');
     $fileValue->getLocale()->willReturn('de_DE');
     $fileValue->getScope()->willReturn('print');
     $prefix = $this->generateFilenamePrefix($fileValue);
     $prefix->shouldMatch('/-file-de_DE-print-/');
 }
 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_normalizes_product_value_which_is_not_a_number($productValueNormalizer, $localizer, ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $options = ['decimal_separator' => ','];
     $productValue->getData()->willReturn('shoes');
     $attribute->getAttributeType()->willReturn(AttributeTypes::TEXT);
     $productValue->getAttribute()->willReturn($attribute);
     $productValueNormalizer->normalize($productValue, null, $options)->willReturn(['simple-select' => 'shoes']);
     $localizer->convertDefaultToLocalized('', $options)->shouldNotBeCalled();
     $this->normalize($productValue, null, $options)->shouldReturn(['simple-select' => 'shoes']);
 }
 function it_normalizes_product_values_into_json($serializer, ProductValueInterface $nameValue, ProductValueInterface $colorValue, AttributeInterface $name, AttributeInterface $color)
 {
     $nameValue->getAttribute()->willReturn($name);
     $colorValue->getAttribute()->willReturn($color);
     $name->getCode()->willReturn('name');
     $color->getCode()->willReturn('color');
     $serializer->normalize($nameValue, 'json', [])->shouldBeCalled()->willReturn(['locale' => null, 'scope' => null, 'value' => 'foo']);
     $serializer->normalize($colorValue, 'json', [])->shouldBeCalled()->willReturn(['locale' => 'en_US', 'scope' => 'ecommerce', 'value' => 'red']);
     $this->normalize([$nameValue, $colorValue], 'json')->shouldReturn(['name' => [['locale' => null, 'scope' => null, 'value' => 'foo']], 'color' => [['locale' => 'en_US', 'scope' => 'ecommerce', 'value' => 'red']]]);
 }
 function it_adds_violations_if_value_is_scopable_and_its_scope_does_not_exist($context, $channelRepository, ProductValueInterface $value, AttributeInterface $scopableAttribute, ScopableValue $constraint, ConstraintViolationBuilderInterface $violation)
 {
     $value->getAttribute()->willReturn($scopableAttribute);
     $scopableAttribute->isScopable()->willReturn(true);
     $value->getScope()->willReturn('inexistingChannel');
     $scopableAttribute->getCode()->willReturn('attributeCode');
     $channelRepository->findOneByIdentifier('inexistingChannel')->willReturn(null);
     $violationData = ['%attribute%' => 'attributeCode', '%channel%' => 'inexistingChannel'];
     $context->buildViolation($constraint->inexistingScopeMessage, $violationData)->shouldBeCalled()->willReturn($violation);
     $this->validate($value, $constraint);
 }
 /**
  * The metric is built by many ordered calls, one for the data column, one for the unit column
  *
  * @param ProductValueInterface $value
  * @param string                $dataOrUnit
  *
  * @return \Pim\Bundle\CatalogBundle\Model\MetricInterface
  */
 protected function addFromManyFields(ProductValueInterface $value, $dataOrUnit)
 {
     // TODO come from original implementation, really FRAGIL because depends on many ordered calls
     if (null === ($metric = $value->getMetric())) {
         $metric = $this->factory->createMetric($value->getAttribute()->getMetricFamily());
         $metric->setData($dataOrUnit);
     } else {
         $metric->setUnit($dataOrUnit);
     }
     return $metric;
 }
 function it_denormalizes_a_new_metric_from_a_single_field_and_set_data_and_unit(ProductValueInterface $metricValue, MetricInterface $metric, AttributeInterface $weight, $metricFactory)
 {
     $context = ['value' => $metricValue];
     $metricValue->getMetric()->willReturn(null);
     $metricValue->getAttribute()->willReturn($weight);
     $weight->getMetricFamily()->willReturn('Weight');
     $metricFactory->createMetric('Weight')->willReturn($metric);
     $metric->setData('100')->shouldBeCalled();
     $metric->setUnit('KILOGRAM')->shouldBeCalled();
     $this->denormalize('100 KILOGRAM', 'className', null, $context)->shouldReturn($metric);
 }
 function it_generates_the_path_when_the_value_is_localisable_and_scopable(ProductValueInterface $value, FileInfoInterface $fileInfo, AttributeInterface $attribute)
 {
     $value->getMedia()->willReturn($fileInfo);
     $value->getLocale()->willReturn('fr_FR');
     $value->getScope()->willReturn('ecommerce');
     $value->getAttribute()->willReturn($attribute);
     $fileInfo->getOriginalFilename()->willReturn('file.jpg');
     $attribute->getCode()->willReturn('picture');
     $attribute->isLocalizable()->willReturn(true);
     $attribute->isScopable()->willReturn(true);
     $this->generate($value, ['identifier' => 'sku001'])->shouldReturn('files/sku001/picture/fr_FR/ecommerce/file.jpg');
 }
 function it_throws_an_exception_if_no_file_is_found(ChannelInterface $channel, ProductInterface $product, ChannelManager $channelManager, Serializer $serializer, ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $product->getValues()->willReturn([$productValue]);
     $productValue->getAttribute()->willReturn($attribute);
     $attribute->getAttributeType()->willReturn('pim_catalog_image');
     $product->getIdentifier()->willReturn($productValue);
     $productValue->getData()->willReturn('data');
     $this->setChannel('foobar');
     $channelManager->getChannelByCode('foobar')->willReturn($channel);
     $serializer->normalize(['data'], 'flat', ['field_name' => 'media', 'prepare_copy' => true])->willThrow('Symfony\\Component\\HttpFoundation\\File\\Exception\\FileNotFoundException');
     $this->shouldThrow('Akeneo\\Bundle\\BatchBundle\\Item\\InvalidItemException')->during('process', [$product]);
 }
 /**
  * Get the internal key that is used to index
  * a product value in a collection of values
  *
  * @param ProductValueInterface $value
  *
  * @return string
  */
 public static function getKey(ProductValueInterface $value)
 {
     $attribute = $value->getAttribute();
     $key = $attribute->getCode();
     if ($attribute->isLocalizable()) {
         $key .= '_' . $value->getLocale();
     }
     if ($attribute->isScopable()) {
         $key .= '_' . $value->getScope();
     }
     return $key;
 }
 /**
  * @param ProductValueInterface $value
  * @param array                 $context
  *
  * @return FormInterface
  */
 public function createProductValueForm(ProductValueInterface $value, array $context)
 {
     $attributeTypeAlias = $value->getAttribute()->getAttributeType();
     $attributeType = $this->attTypeRegistry->get($attributeTypeAlias);
     $name = $attributeType->prepareValueFormName($value);
     $type = $attributeType->prepareValueFormAlias($value);
     $data = $attributeType->prepareValueFormData($value);
     $options = array_merge($attributeType->prepareValueFormConstraints($value), $attributeType->prepareValueFormOptions($value));
     $event = new CreateProductValueFormEvent($value, $type, $data, $options, $context);
     $this->eventDispatcher->dispatch(ProductEvents::CREATE_VALUE_FORM, $event);
     $valueForm = $this->formFactory->createNamed($name, $event->getFormType(), $event->getFormData(), $event->getFormOptions());
     return $valueForm;
 }
 function it_normalizes_a_product_value_into_mongodb_document($mongoFactory, $serializer, ProductValueInterface $value, AttributeInterface $attribute, \MongoDBRef $mongoDBRef, \MongoId $mongoId)
 {
     $context = ['_id' => $mongoId, 'collection_name' => 'product'];
     $mongoFactory->createMongoId()->willReturn($mongoId);
     $mongoFactory->createMongoDBRef('product', $mongoId)->willReturn($mongoDBRef);
     $attribute->getId()->willReturn(123);
     $attribute->getBackendType()->willReturn('text');
     $value->getAttribute()->willReturn($attribute);
     $value->getData()->willReturn('my description');
     $value->getLocale()->willReturn(null);
     $value->getScope()->willReturn(null);
     $this->normalize($value, 'mongodb_document', $context)->shouldReturn(['_id' => $mongoId, 'attribute' => 123, 'entity' => $mongoDBRef, 'text' => 'my description']);
 }