/** * @param Product $product * @param string $unitCode * @param float|int $quantity * * @return float|int Rounded quantity */ public function roundProductQuantity(Product $product, $unitCode, $quantity) { $unitPrecision = $product->getUnitPrecision($unitCode); if ($unitPrecision) { $quantity = $this->roundingService->round($quantity, $unitPrecision->getPrecision()); } return $quantity; }
/** * Method testNoPrecision */ public function testNoPrecision() { $this->roundingService->expects($this->never())->method('round'); $lineItemManager = new LineItemManager($this->roundingService); $product = $this->getProductEntityWithPrecision('kg', 3); $quantity = $this->getRandomQuantity(); $roundedQuantity = $lineItemManager->roundProductQuantity($product, 'unit', $quantity); $this->assertEquals($quantity, $roundedQuantity); }
/** * @param ProductPrice $defaultData * @param array $submittedData * @param ProductPrice $expectedData * @param boolean $rounding * @dataProvider submitProvider */ public function testSubmit(ProductPrice $defaultData, array $submittedData, ProductPrice $expectedData, $rounding = false) { if ($rounding) { $this->roundingService->expects($this->once())->method('round')->willReturnCallback(function ($value, $precision) { return round($value, $precision); }); } $form = $this->factory->create($this->formType, $defaultData, []); $this->assertEquals($defaultData, $form->getData()); $form->submit($submittedData); $this->assertEquals([], $form->getErrors()); $this->assertTrue($form->isValid()); $this->assertEquals($expectedData, $form->getData()); }
protected function setUp() { $this->matcher = $this->getMockBuilder('OroB2B\\Bundle\\SaleBundle\\Model\\QuoteProductOfferMatcher')->disableOriginalConstructor()->getMock(); $this->matcher->expects($this->any())->method('match')->willReturnCallback(function (QuoteProduct $quoteProduct, $unitCode, $quantity) { // simple emulation of original match algorithm return $quoteProduct->getQuoteProductOffers()->filter(function (QuoteProductOffer $offer) use($quantity) { return $offer->getQuantity() === $quantity; })->first(); }); $this->roundingService = $this->getMockBuilder('OroB2B\\Bundle\\ProductBundle\\Rounding\\RoundingService')->disableOriginalConstructor()->getMock(); $this->roundingService->expects($this->any())->method('round')->willReturnCallback(function ($value, $precision) { return round($value, $precision, PHP_ROUND_HALF_UP); }); }
/** * @param FormEvent $event */ public function preSubmitData(FormEvent $event) { $data = $event->getData(); if (!isset($data['product'], $data['unit'], $data['quantity'])) { return; } /** @var Product $product */ $product = $this->registry->getRepository($this->productClass)->find($data['product']); if ($product) { $unitPrecision = $product->getUnitPrecision($data['unit']); if ($unitPrecision) { $data['quantity'] = $this->roundingService->round($data['quantity'], $unitPrecision->getPrecision()); $event->setData($data); } } }
/** * @param array $sourceData * @param array $expectedData * @dataProvider onPreSubmitDataProvider */ public function testOnPreSubmit(array $sourceData, array $expectedData) { $this->roundingService->expects($this->any())->method('round')->willReturnCallback(function ($quantity, $precision) { return round($quantity, $precision); }); $event = $this->createEvent($sourceData); $this->extension->onPreSubmit($event); $this->assertEquals($expectedData, $event->getData()); }
/** * @param float $quantity * @param string $unitCode * @return float */ protected function roundQuantity($quantity, $unitCode) { $precision = 0; $product = $this->quoteProduct->getProductReplacement() ?: $this->quoteProduct->getProduct(); if ($product) { $unitPrecision = $product->getUnitPrecision($unitCode); if ($unitPrecision) { $precision = $unitPrecision->getPrecision(); } } return $this->roundingService->round($quantity, $precision); }
/** * @param FormEvent $event */ public function onPreSubmit(FormEvent $event) { $data = $event->getData(); $unitPrecisions = []; if (isset($data['unitPrecisions'])) { foreach ($data['unitPrecisions'] as $unitPrecision) { $unitPrecisions[$unitPrecision['unit']] = $unitPrecision['precision']; } } if (isset($data['prices'])) { foreach ($data['prices'] as $key => &$price) { if (empty($price['unit']) || empty($price['quantity'])) { unset($data['prices'][$key]); continue; } if (array_key_exists($price['unit'], $unitPrecisions)) { $price['quantity'] = $this->roundingService->round($price['quantity'], $unitPrecisions[$price['unit']]); } } } $event->setData($data); }
/** * @throws \OroB2B\Bundle\ProductBundle\Exception\InvalidRoundingTypeException */ public function testInvalidRoundingTypeException() { $this->configManager->expects($this->once())->method('get')->with('orob2b_product.unit_rounding_type')->willReturn('test'); $this->setExpectedException('\\OroB2B\\Bundle\\ProductBundle\\Exception\\InvalidRoundingTypeException', 'The type of the rounding is not valid. Allowed the following types: half_up, half_down, ceil, floor.'); $this->service->round(1.15, 1); }