function it_validates_product_metric($accessor, $context, ValidMetric $constraint, MetricInterface $metric) { $metric->getData()->willReturn(12); $accessor->getValue($metric, 'family')->shouldBeCalled()->willReturn('Weight'); $accessor->getValue($metric, 'unit')->shouldBeCalled()->willReturn('kg'); $context->buildViolation(Argument::cetera())->shouldNotBeCalled(); $this->validate($metric, $constraint)->shouldReturn(null); }
function it_marks_product_as_updated_when_a_product_metric_is_updated(EntityManager $em, UnitOfWork $uow, ProductInterface $product, ProductValueInterface $value, MetricInterface $metric) { $metric->getValue()->willReturn($value); $value->getEntity()->willReturn($product); $em->getUnitOfWork()->willReturn($uow); $uow->getEntityChangeSet($metric)->willReturn(['data' => ['20', '25']]); $this->guessUpdates($em, $metric, UpdateGuesserInterface::ACTION_UPDATE_ENTITY)->shouldReturn([$product]); }
/** * 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_returns_data_if_it_is_not_a_numeric(MetricInterface $metric, ProductValueInterface $productValue, AttributeInterface $attribute) { $metric->getValue()->willReturn($productValue); $productValue->getAttribute()->willReturn($attribute); $attribute->isDecimalsAllowed()->willReturn(false); $metric->getUnit()->willReturn('KILOGRAM'); $metric->getData()->willReturn('a_metric_data'); $this->normalize($metric, 'standard', ['is_decimals_allowed' => false])->shouldReturn(['amount' => 'a_metric_data', 'unit' => 'KILOGRAM']); }
function it_normalizes_empty_metric($metricNormalizer, $localizer, MetricInterface $metric) { $options = ['decimal_separator' => ',']; $metric->getData()->willReturn(''); $data = ['data' => '', 'unit' => '']; $metricNormalizer->normalize($metric, null, $options)->willReturn($data); $localizer->localize($data['data'], $options)->willReturn(''); $this->normalize($metric, null, $options)->shouldReturn(['data' => '', 'unit' => '']); }
public function it_succesfully_checks_complete_metric(ProductValueInterface $value, ChannelInterface $channel, LocaleInterface $locale, MetricInterface $metric) { $value->getMetric()->willReturn(null); $this->isComplete($value, $channel, $locale)->shouldReturn(false); $value->getMetric()->willReturn([]); $this->isComplete($value, $channel, $locale)->shouldReturn(false); $metric->getData()->willReturn(null); $value->getMetric()->willReturn($metric); $this->isComplete($value, $channel, $locale)->shouldReturn(false); $metric->getData()->willReturn('foobar'); $value->getMetric()->willReturn($metric); $this->isComplete($value, $channel, $locale)->shouldReturn(true); }
/** * Get the data stored in the metric * * @param MetricInterface $metric * @param bool $withUnit * @param bool $decimalsAllowed * * @return string */ public function getMetricData(MetricInterface $metric, $withUnit, $decimalsAllowed = true) { $data = $metric->getData(); if (null === $data || '' === $data) { return ''; } $pattern = $decimalsAllowed ? '%.4F' : '%d'; if ($withUnit) { $data = sprintf($pattern . ' %s', $metric->getData(), $metric->getUnit()); } else { $data = sprintf($pattern, $metric->getData()); } return $data; }
/** * Convert data in standard unit for metrics * * @param MetricInterface $metric */ protected function createMetricBaseValues(MetricInterface $metric) { $baseUnit = $this->manager->getStandardUnitForFamily($metric->getFamily()); if (is_numeric($metric->getData())) { $baseData = $this->converter->setFamily($metric->getFamily())->convertBaseToStandard($metric->getUnit(), $metric->getData()); } else { $baseData = null; } $metric->setBaseData($baseData); $metric->setBaseUnit($baseUnit); }
/** * Validate metric type and default metric unit * * @param AttributeInterface|MetricInterface|ProductValueInterface $object * @param Constraint $constraint */ public function validate($object, Constraint $constraint) { if ($object instanceof AttributeInterface) { $familyProperty = 'metricFamily'; $unitProperty = 'defaultMetricUnit'; } elseif ($object instanceof MetricInterface && null !== $object->getData()) { $familyProperty = 'family'; $unitProperty = 'unit'; } elseif ($object instanceof ProductValueInterface && null !== $object->getMetric() && (null !== $object->getMetric()->getUnit() || null !== $object->getMetric()->getData())) { $object = $object->getMetric(); $familyProperty = 'family'; $unitProperty = 'unit'; } else { return; } $family = $this->propertyAccessor->getValue($object, $familyProperty); $unit = $this->propertyAccessor->getValue($object, $unitProperty); if (!array_key_exists($family, $this->measures)) { $this->context->buildViolation($constraint->familyMessage)->atPath($familyProperty)->addViolation(); } elseif (!array_key_exists($unit, $this->measures[$family]['units'])) { $this->context->buildViolation($constraint->unitMessage)->atPath($unitProperty)->addViolation(); } }
function it_normalizes_metric_with_float_data_with_decimals_not_allowed(MetricInterface $metric) { $metric->getData()->willReturn('72.0000'); $metric->getUnit()->willReturn('KILOGRAM'); $this->normalize($metric, null, ['field_name' => 'weight', 'decimals_allowed' => false])->shouldReturn(['weight' => '72', 'weight-unit' => 'KILOGRAM']); }
function it_adds_violation_when_validating_non_numeric_metric_value($context, MetricInterface $metric, IsNumeric $numericConstraint, ConstraintViolationBuilderInterface $violation) { $metric->getData()->willReturn('a'); $context->buildViolation($numericConstraint->message)->shouldBeCalled()->willReturn($violation); $this->validate($metric, $numericConstraint); }
function it_does_not_validate_a_metric($context, NotDecimal $constraint, MetricInterface $metric, ConstraintViolationBuilderInterface $violation) { $metric->getData()->willReturn(82.25); $context->buildViolation($constraint->message)->shouldBeCalled()->willReturn($violation); $this->validate($metric, $constraint); }
function it_normalizes_empty_metric_simple_format($metricNormalizer, $localizer, MetricInterface $metric) { $options = ['decimal_separator' => ',', 'field_name' => 'metric']; $metric->getData()->willReturn(''); $localizer->localize(['data' => ''], $options)->willReturn(['data' => '']); $metricNormalizer->normalize($metric, null, $options)->willReturn(['metric' => '']); $this->normalize($metric, null, $options)->shouldReturn(['metric' => '']); }
function it_does_not_validate_metric($context, Range $constraint, MetricInterface $metric, ConstraintViolationBuilderInterface $violation) { $constraint->min = 0; $constraint->max = 100; $metric->getData()->willReturn(150); $context->buildViolation($constraint->maxMessage, ['{{ value }}' => 150, '{{ limit }}' => 100])->shouldBeCalled()->willReturn($violation); $violation->atPath('data')->shouldBeCalled()->willReturn($violation); $violation->addViolation()->shouldBeCalled(); $this->validate($metric, $constraint); }
function it_normalizes_metric_when_has_no_data(MetricInterface $metric) { $metric->getData()->willReturn(null); $this->normalize($metric, 'mongodb_json', [])->shouldReturn(null); }
function it_returns_flat_data_with_french_attribute($channelManager, $serializer, ChannelInterface $channel, ProductInterface $product, ProductValueInterface $number, AttributeInterface $attribute, MetricInterface $metric, ProductValueInterface $metricValue, ProductPriceInterface $price, ProductValueInterface $priceValue) { $this->setLocale('fr_FR'); $attribute->getAttributeType()->willReturn('pim_catalog_number'); $number->getDecimal('10.50'); $number->getAttribute()->willReturn($attribute); $attribute->getAttributeType()->willReturn('pim_catalog_metric'); $metric->getData()->willReturn('10.00'); $metric->getUnit()->willReturn('GRAM'); $metricValue->getAttribute()->willReturn($attribute); $metricValue->getData()->willReturn($metric); $attribute->getAttributeType()->willReturn('pim_catalog_price_collection'); $price->getData()->willReturn('10'); $price->getCurrency()->willReturn('EUR'); $priceValue->getAttribute()->willReturn($attribute); $priceValue->getData()->willReturn($price); $product->getValues()->willReturn([$number, $metricValue, $priceValue]); $serializer->normalize($product, 'flat', ['scopeCode' => 'mobile', 'localeCodes' => '', 'locale' => 'fr_FR'])->willReturn(['10,50', '10,00 GRAM', '10,00 EUR', '25/10/2015']); $channelManager->getChannelByCode('mobile')->willReturn($channel); $this->setChannelCode('mobile'); $this->process($product)->shouldReturn(['media' => [], 'product' => ['10,50', '10,00 GRAM', '10,00 EUR', '25/10/2015']]); }
/** * {@inheritdoc} */ public function setMetric(MetricInterface $metric) { $metric->setValue($this); $this->metric = $metric; return $this; }