/**
  * @dataProvider getSetValueData
  */
 public function testSetValue($columnSuffix, $data, $createMetric, $expectedData, $expectedUnit)
 {
     $columnInfo = $this->getMock('Pim\\Bundle\\TransformBundle\\Transformer\\ColumnInfo\\ColumnInfoInterface');
     $columnInfo->expects($this->any())->method('getSuffixes')->will($this->returnValue(array($columnSuffix)));
     $attribute = $this->getMockBuilder('Pim\\Bundle\\CatalogBundle\\Model\\AbstractAttribute')->setMethods(array('getMetricFamily'))->getMock();
     $columnInfo->expects($this->any())->method('getAttribute')->will($this->returnValue($attribute));
     $attribute->expects($this->any())->method('getMetricFamily')->will($this->returnValue('metric_family'));
     $object = $this->getMockBuilder('Pim\\Bundle\\CatalogBundle\\Model\\ProductValueInterface')->setMethods(array('getMetric', 'setMetric', '__toString', 'setData', 'getData', 'getAttribute', 'getEntity'))->getMock();
     $metric = null;
     if ($createMetric) {
         $object->expects($this->once())->method('setMetric')->will($this->returnCallback(function ($createdMetric) use(&$metric) {
             $metric = $createdMetric;
         }));
     } else {
         $metric = new Metric();
         $metric->setFamily('metric_family');
     }
     $object->expects($this->any())->method('getMetric')->will($this->returnCallback(function () use(&$metric) {
         return $metric;
     }));
     $metricFactory = new MetricFactory('Pim\\Bundle\\CatalogBundle\\Model\\Metric');
     $transformer = new MetricTransformer($metricFactory);
     $transformer->setValue($object, $columnInfo, $data);
     $this->assertInstanceOf('Pim\\Bundle\\CatalogBundle\\Model\\Metric', $metric);
     $this->assertEquals('metric_family', $metric->getFamily());
     $this->assertEquals($expectedData, $metric->getData());
     $this->assertEquals($expectedUnit, $metric->getUnit());
 }
 /**
  * @dataProvider getSetValueData
  */
 public function testSetValue($columnSuffix, $data, $createMetric, $expectedData, $expectedUnit)
 {
     $columnInfo = $this->getMock('Pim\\Bundle\\TransformBundle\\Transformer\\ColumnInfo\\ColumnInfoInterface');
     $columnInfo->expects($this->any())->method('getSuffixes')->will($this->returnValue([$columnSuffix]));
     $attribute = $this->getMockBuilder('Pim\\Bundle\\CatalogBundle\\Model\\AbstractAttribute')->setMethods(['getMetricFamily'])->getMock();
     $columnInfo->expects($this->any())->method('getAttribute')->will($this->returnValue($attribute));
     $attribute->expects($this->any())->method('getMetricFamily')->will($this->returnValue('metric_family'));
     $object = $this->getMockBuilder('Pim\\Bundle\\CatalogBundle\\Model\\ProductValueInterface')->setMethods(['setText', 'setDatetime', 'setInteger', 'setId', 'getOption', 'getMedia', 'getDecimal', 'setDecimal', 'setAttribute', 'addOption', 'getBoolean', 'setOptions', 'setPrices', 'getId', 'setVarchar', 'setBoolean', 'getData', 'getMetric', 'getDate', 'getAttribute', 'getEntity', 'setMedia', 'getPrices', 'getOptions', 'getLocale', 'setMetric', 'addPrice', 'getVarchar', 'removePrice', 'hasData', 'setScope', 'removeOption', 'getText', 'setData', 'setOption', 'getPrice', 'setDate', 'addData', 'setLocale', 'isRemovable', 'getScope', 'getDatetime', 'setEntity', 'getInteger', 'getProduct', 'setProduct', '__toString'])->getMock();
     $metric = null;
     if ($createMetric) {
         $object->expects($this->once())->method('setMetric')->will($this->returnCallback(function ($createdMetric) use(&$metric) {
             $metric = $createdMetric;
         }));
     } else {
         $metric = new Metric();
         $metric->setFamily('metric_family');
     }
     $object->expects($this->any())->method('getMetric')->will($this->returnCallback(function () use(&$metric) {
         return $metric;
     }));
     $metricFactory = new MetricFactory('Pim\\Bundle\\CatalogBundle\\Model\\Metric');
     $transformer = new MetricTransformer($metricFactory);
     $transformer->setValue($object, $columnInfo, $data);
     $this->assertInstanceOf('Pim\\Bundle\\CatalogBundle\\Model\\Metric', $metric);
     $this->assertEquals('metric_family', $metric->getFamily());
     $this->assertEquals($expectedData, $metric->getData());
     $this->assertEquals($expectedUnit, $metric->getUnit());
 }
 /**
  * Convert data in standard unit for metrics
  *
  * @param Metric $metric
  */
 protected function createMetricBaseValues(Metric $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);
 }
 function it_normalizes_a_metric_into_mongodb_document($mongoFactory, $converter, $manager, Metric $metric, \MongoId $mongoId)
 {
     $mongoFactory->createMongoId()->willReturn($mongoId);
     $metric->getUnit()->willReturn('Kg');
     $metric->getData()->willReturn(85);
     $metric->getFamily()->willReturn('weight');
     $converter->setFamily('weight')->willReturn($converter);
     $converter->convertBaseToStandard('Kg', 85)->willReturn(8500);
     $manager->getStandardUnitForFamily('weight')->willReturn('g');
     $metric->setBaseData(8500)->shouldBeCalled();
     $metric->getBaseData()->willReturn(8500);
     $metric->setBaseUnit('g')->shouldBeCalled();
     $metric->getBaseUnit()->willReturn('g');
     $this->normalize($metric, 'mongodb_document')->shouldReturn(['_id' => $mongoId, 'unit' => 'Kg', 'data' => 85, 'baseUnit' => 'g', 'baseData' => 8500, 'family' => 'weight']);
 }
 function it_validates_a_product_value_with_backendtype_as_metric($context, ProductValueComplete $constraint, ProductValueInterface $productValue, AttributeInterface $attribute)
 {
     $constraint->getChannel()->willReturn($this->getChannel());
     $metric = new Metric();
     $metric->setData('data');
     $productValue->getMetric()->willReturn($metric);
     $productValue->getData()->willReturn('data');
     $attribute->getBackendType()->willReturn('metric');
     $productValue->getAttribute()->willReturn($attribute);
     $context->buildViolation(Argument::any())->shouldNotBeCalled();
     $this->validate($productValue, $constraint);
 }
 /**
  * {@inheritDoc}
  */
 public function __toString()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, '__toString', array());
     return parent::__toString();
 }