/**
  * {@inheritdoc}
  */
 public function isComplete(ProductValueInterface $productValue, ChannelInterface $channel = null, LocaleInterface $locale = null)
 {
     $metric = $productValue->getMetric();
     if (!$metric || null === $metric->getData()) {
         return false;
     }
     return true;
 }
 function it_validates_product_value_with_metric_data($accessor, $context, ValidMetric $constraint, ProductValueInterface $value, MetricInterface $metric)
 {
     $value->getMetric()->willReturn($metric);
     $metric->getUnit()->willReturn('cm');
     $metric->getData()->willReturn(12);
     $accessor->getValue($metric, 'family')->shouldBeCalled()->willReturn('Weight');
     $accessor->getValue($metric, 'unit')->shouldBeCalled()->willReturn('kg');
     $context->addViolationAt(Argument::any(), Argument::any())->shouldNotBeCalled();
     $this->validate($value, $constraint)->shouldReturn(null);
 }
 /**
  * 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);
 }
 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);
 }
 /**
  * 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();
     }
 }
 /**
  * Check if the metric value is not empty
  *
  * @param ProductValueInterface $value
  * @param Constraint            $constraint
  */
 protected function validateMetric(ProductValueInterface $value, Constraint $constraint)
 {
     $metric = $value->getMetric();
     if (!$metric || $metric->getData() === null) {
         $this->context->addViolation($constraint->messageComplete);
     }
 }
 function it_returns_null_when_unknown_backendtype($context, ProductValueComplete $constraint, ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $constraint->getChannel()->willReturn($this->getChannel());
     $metric = new Metric();
     $productValue->getMetric()->willReturn($metric);
     $productValue->getData()->willReturn('data');
     $attribute->getBackendType()->willReturn('unknown_metric');
     $productValue->getAttribute()->willReturn($attribute);
     $context->buildViolation(Argument::any())->shouldNotBeCalled();
     $this->validate($productValue, $constraint);
 }
 function it_returns_a_metric_if_the_data_is_empty(ProductValueInterface $metricValue, MetricInterface $metric)
 {
     $metricValue->getMetric()->willReturn($metric);
     $this->denormalize('', 'className', null, ['value' => $metricValue])->shouldReturn($metric);
     $this->denormalize(null, 'className', null, ['value' => $metricValue])->shouldReturn($metric);
 }
 /**
  * @param ProductValueInterface $productValue
  * @param ProductValueInterface $value
  */
 protected function setProductMetric(ProductValueInterface $productValue, ProductValueInterface $value)
 {
     if (null === ($metric = $productValue->getMetric())) {
         $metric = $this->metricFactory->createMetric($value->getAttribute()->getMetricFamily());
         $productValue->setMetric($metric);
     }
     $metric->setUnit($value->getMetric()->getUnit());
     $metric->setData($value->getMetric()->getData());
 }