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); }
/** * Convert all the products metric values into the channel configured conversion units * * @param ProductInterface $product * @param ChannelInterface $channel */ public function convert(ProductInterface $product, ChannelInterface $channel) { $channelUnits = $channel->getConversionUnits(); foreach ($product->getValues() as $value) { $data = $value->getData(); $attribute = $value->getAttribute(); if ($data instanceof MetricInterface && isset($channelUnits[$attribute->getCode()])) { $channelUnit = $channelUnits[$attribute->getCode()]; $this->converter->setFamily($data->getFamily()); $data->setData($this->converter->convert($data->getUnit(), $channelUnit, $data->getData())); $data->setUnit($channelUnit); } } }
function it_normalizes_channel(ChannelInterface $channel, CurrencyInterface $eur, CurrencyInterface $usd, LocaleInterface $en, LocaleInterface $fr, CategoryInterface $category) { $channel->getCode()->willReturn('ecommerce'); $channel->getLabel()->willReturn('Ecommerce'); $channel->getCurrencies()->willReturn([$eur, $usd]); $eur->getCode()->willReturn('EUR'); $usd->getCode()->willReturn('USD'); $channel->getLocales()->willReturn([$en, $fr]); $en->getCode()->willReturn('en_US'); $fr->getCode()->willReturn('fr_FR'); $channel->getCategory()->willReturn($category); $category->getCode()->willReturn('Master catalog'); $channel->getConversionUnits()->willReturn(['Weight' => 'Kilogram', 'Size' => 'Centimeter']); $this->normalize($channel)->shouldReturn(['code' => 'ecommerce', 'label' => 'Ecommerce', 'currencies' => ['EUR', 'USD'], 'locales' => ['en_US', 'fr_FR'], 'category' => 'Master catalog', 'conversion_units' => 'Weight: Kilogram, Size: Centimeter']); }
function it_does_not_convert_null_metric_values_in_the_channel($converter, ProductValueInterface $weightValue, AttributeInterface $weight, MetricInterface $weightMetric, ProductInterface $product, ChannelInterface $channel) { $weightValue->getAttribute()->willReturn($weight); $weightValue->getData()->willReturn($weightMetric); $weight->getCode()->willReturn('weight'); $weightMetric->getFamily()->willReturn('Weight'); $weightMetric->getUnit()->willReturn(null); $weightMetric->getData()->willReturn(null); $product->getValues()->willReturn(array($weightValue)); $channel->getConversionUnits()->willReturn(array('weight' => 'GRAM')); $converter->setFamily('Weight')->shouldNotBeCalled(); $converter->convert('KILOGRAM', 'GRAM', 1)->shouldNotBeCalled(); $weightMetric->setData(null)->shouldNotBeCalled(); $weightMetric->setUnit('GRAM')->shouldNotBeCalled(); $this->convert($product, $channel); }
/** * Returns conversion units * * @param ChannelInterface $channel * * @return string */ protected function normalizeConversionUnits(ChannelInterface $channel) { $result = array(); foreach ($channel->getConversionUnits() as $family => $unit) { $result[] = sprintf('%s: %s', $family, $unit); } return implode(', ', $result); }