public function testGetValue() { $resultPrice = rand(1, 9); $customOptionOneQty = rand(1, 9); $customOptionTwoQty = rand(1, 9); $priceInfoBase = $this->getMockBuilder('Magento\\Framework\\Pricing\\PriceInfo\\Base')->disableOriginalConstructor()->getMock(); $priceInfoBase->expects($this->any())->method('getPrice')->with(FinalPrice::PRICE_CODE)->willReturn($this->price); $productOne = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productOne->expects($this->once())->method('getId')->willReturn(1); $productOne->expects($this->once())->method('getPriceInfo')->willReturn($priceInfoBase); $productTwo = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productTwo->expects($this->once())->method('getId')->willReturn(2); $productTwo->expects($this->once())->method('getPriceInfo')->willReturn($priceInfoBase); $this->price->expects($this->any())->method('getValue')->willReturn($resultPrice); $customOptionOne = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item\\Option')->disableOriginalConstructor()->getMock(); $customOptionOne->expects($this->any())->method('getValue')->willReturn($customOptionOneQty); $customOptionTwo = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item\\Option')->disableOriginalConstructor()->getMock(); $customOptionTwo->expects($this->any())->method('getValue')->willReturn($customOptionTwoQty); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $groupedProduct = $this->getMockBuilder('Magento\\GroupedProduct\\Model\\Product\\Type\\Grouped')->disableOriginalConstructor()->getMock(); $groupedProduct->expects($this->once())->method('setStoreFilter')->with($store, $this->saleableItem)->willReturnSelf(); $groupedProduct->expects($this->once())->method('getAssociatedProducts')->with($this->saleableItem)->willReturn([$productOne, $productTwo]); $this->saleableItem->expects($this->once())->method('getTypeInstance')->willReturn($groupedProduct); $this->saleableItem->expects($this->any())->method('getStore')->willReturn($store); $this->saleableItem->expects($this->any())->method('getCustomOption')->willReturnMap([['associated_product_' . 1, $customOptionOne], ['associated_product_' . 2, $customOptionTwo]]); $item = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Configuration\\Item\\ItemInterface')->getMock(); $this->model->setItem($item); $result = 0; foreach ([$customOptionOneQty, $customOptionTwoQty] as $qty) { $result += $resultPrice * $qty; } $this->assertEquals($result, $this->model->getValue()); }
/** * Retrieve Amount object based on given float amount, product and exclude option. * It is possible to pass "true" or adjustment code to exclude all or specific adjustment from an amount. * * @param float|string $amount * @param SaleableInterface $saleableItem * @param null|bool|string $exclude * @param null|array $context * @return \Magento\Framework\Pricing\Amount\AmountInterface */ public function getAmount($amount, SaleableInterface $saleableItem, $exclude = null, $context = []) { $baseAmount = $fullAmount = $amount; $previousAdjustments = 0; $adjustments = []; foreach ($saleableItem->getPriceInfo()->getAdjustments() as $adjustment) { $code = $adjustment->getAdjustmentCode(); $toExclude = false; if ($exclude === true || ($exclude !== null && $code === $exclude)) { $toExclude = true; } if ($adjustment->isIncludedInBasePrice()) { $adjust = $adjustment->extractAdjustment($baseAmount, $saleableItem, $context); $baseAmount -= $adjust; $fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context); $adjust = $fullAmount - $baseAmount - $previousAdjustments; if (!$toExclude) { $adjustments[$code] = $adjust; } } elseif ($adjustment->isIncludedInDisplayPrice($saleableItem)) { if ($toExclude) { continue; } $newAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context); $adjust = $newAmount - $fullAmount; $adjustments[$code] = $adjust; $fullAmount = $newAmount; $previousAdjustments += $adjust; } } return $this->amountFactory->create($fullAmount, $adjustments); }
/** * Create amount renderer * * @param AmountInterface $amount * @param SaleableInterface $saleableItem * @param PriceInterface $price * @param array $data * @return AmountRenderInterface * @throws \InvalidArgumentException */ public function createAmountRender(AmountInterface $amount, SaleableInterface $saleableItem = null, PriceInterface $price = null, array $data = []) { $type = self::DEFAULT_PRICE_GROUP_TYPE; if ($saleableItem) { $type = $saleableItem->getTypeId(); } $priceCode = null; $renderClassName = self::AMOUNT_RENDERER_DEFAULT; if ($price) { $priceCode = $price->getPriceCode(); // implement class resolving fallback $pattern = [$type . '/prices/' . $priceCode . '/amount_render_class', $type . '/default_amount_render_class', 'default/prices/' . $priceCode . '/amount_render_class', 'default/default_amount_render_class']; $renderClassName = $this->findDataByPattern($pattern); if (!$renderClassName) { throw new \InvalidArgumentException('There is no amount render class for price code "' . $priceCode . '"'); } } $arguments['data'] = $data; $arguments['rendererPool'] = $this; $arguments['amount'] = $amount; if ($saleableItem) { $arguments['saleableItem'] = $saleableItem; if ($price) { $arguments['price'] = $price; } } /** @var \Magento\Framework\View\Element\Template $amountBlock */ $amountBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments); if (!$amountBlock instanceof AmountRenderInterface) { throw new \InvalidArgumentException('Block "' . $renderClassName . '" must implement \\Magento\\Framework\\Pricing\\Render\\AmountRenderInterface'); } $amountBlock->setTemplate($this->getAmountRenderBlockTemplate($type, $priceCode)); return $amountBlock; }
/** * @param $typeId * @param $quantity * @param $infoClass * @param $prices * @dataProvider createPriceInfoDataProvider */ public function testCreate($typeId, $quantity, $infoClass, $prices) { $this->saleableItemMock->expects($this->once())->method('getTypeId')->will($this->returnValue($typeId)); $this->saleableItemMock->expects($this->once())->method('getQty')->will($this->returnValue($quantity)); $this->objectManagerMock->expects($this->exactly(2))->method('create')->will($this->returnValueMap([[$prices, ['saleableItem' => $this->saleableItemMock, 'quantity' => $quantity], $this->pricesMock], [$infoClass, ['saleableItem' => $this->saleableItemMock, 'quantity' => $quantity, 'prices' => $this->pricesMock], $this->priceInfoMock]])); $this->assertEquals($this->priceInfoMock, $this->factory->create($this->saleableItemMock, [])); }
/** * @param SaleableInterface $saleableItem * @param float $quantity * @param CalculatorInterface $calculator * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency */ public function __construct(SaleableInterface $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency) { $this->product = $saleableItem; $this->quantity = $quantity; $this->calculator = $calculator; $this->priceCurrency = $priceCurrency; $this->priceInfo = $saleableItem->getPriceInfo(); }
/** * @param \Magento\Framework\Pricing\SaleableInterface $product * @return float */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { $selectedConfigurableOption = $product->getSelectedConfigurableOption(); if ($selectedConfigurableOption) { $price = $this->priceResolver->resolvePrice($selectedConfigurableOption); } else { $price = null; foreach ($this->configurable->getUsedProducts($product) as $subProduct) { $productPrice = $this->priceResolver->resolvePrice($subProduct); $price = $price ? min($price, $productPrice) : $productPrice; } } $priceInCurrentCurrency = $this->priceCurrency->convertAndRound($price); return $priceInCurrentCurrency ? (double) $priceInCurrentCurrency : false; }
public function testGetValueWithNoCustomOption() { $priceMock = $this->getMockBuilder('Magento\\Framework\\Pricing\\Price\\PriceInterface')->getMockForAbstractClass(); $priceMock->expects($this->once())->method('getValue')->willReturn(0); $this->priceInfoMock->expects($this->once())->method('getPrice')->with(BasePrice::PRICE_CODE)->willReturn($priceMock); $this->saleableItem->expects($this->once())->method('getLinksPurchasedSeparately')->willReturn(true); $this->saleableItem->expects($this->once())->method('getCustomOption')->with('downloadable_link_ids')->willReturn(null); $this->assertEquals(0, $this->model->getValue()); }
/** * @dataProvider showRangePriceDataProvider */ public function testShowRangePrice($optMinValue, $optMaxValue, $custMinValue, $custMaxValue, $expectedShowRange) { $enableCustomOptionMocks = $optMinValue == $optMaxValue; $priceInfo = $this->getMock('Magento\\Framework\\Pricing\\PriceInfo\\Base', [], [], '', false); $bundleOptionPrice = $this->getMockBuilder('Magento\\Bundle\\Pricing\\Price\\BundleOptionPrice')->disableOriginalConstructor()->getMock(); $customOptionPrice = $this->getMockBuilder('Magento\\Catalog\\Pricing\\Price\\CustomOptionPrice')->disableOriginalConstructor()->getMock(); $this->saleableItem->expects($this->atLeastOnce())->method('getPriceInfo')->will($this->returnValue($priceInfo)); $priceInfo->expects($this->at(0))->method('getPrice')->with(Price\BundleOptionPrice::PRICE_CODE)->will($this->returnValue($bundleOptionPrice)); if ($enableCustomOptionMocks) { $priceInfo->expects($this->at(1))->method('getPrice')->with(CustomOptionPrice::PRICE_CODE)->will($this->returnValue($customOptionPrice)); } $bundleOptionPrice->expects($this->once())->method('getValue')->will($this->returnValue($optMinValue)); $bundleOptionPrice->expects($this->once())->method('getMaxValue')->will($this->returnValue($optMaxValue)); if ($enableCustomOptionMocks) { $customOptionPrice->expects($this->at(0))->method('getCustomOptionRange')->will($this->returnValue($custMinValue)); $customOptionPrice->expects($this->at(1))->method('getCustomOptionRange')->will($this->returnValue($custMaxValue)); } $this->assertEquals($expectedShowRange, $this->model->showRangePrice()); }
/** * @param $regularPrice * @param $specialPrice * @param $isScopeDateInInterval * @param $value * @param $percent * @dataProvider getValueDataProvider */ public function testGetValue($regularPrice, $specialPrice, $isScopeDateInInterval, $value, $percent) { $specialFromDate = 'some date from'; $specialToDate = 'some date to'; $this->saleable->expects($this->once())->method('getSpecialPrice')->will($this->returnValue($specialPrice)); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->saleable->expects($this->once())->method('getStore')->will($this->returnValue($store)); $this->saleable->expects($this->once())->method('getSpecialFromDate')->will($this->returnValue($specialFromDate)); $this->saleable->expects($this->once())->method('getSpecialToDate')->will($this->returnValue($specialToDate)); $this->localeDate->expects($this->once())->method('isScopeDateInInterval')->with($store, $specialFromDate, $specialToDate)->will($this->returnValue($isScopeDateInInterval)); $this->priceCurrencyMock->expects($this->never())->method('convertAndRound'); if ($isScopeDateInInterval) { $price = $this->getMock('Magento\\Framework\\Pricing\\Price\\PriceInterface'); $this->priceInfo->expects($this->once())->method('getPrice')->with(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)->will($this->returnValue($price)); $price->expects($this->once())->method('getValue')->will($this->returnValue($regularPrice)); } $this->assertEquals($value, $this->model->getValue()); //check that the second call will get data from cache the same as in first call $this->assertEquals($value, $this->model->getValue()); $this->assertEquals($percent, $this->model->getDiscountPercent()); }
/** * Create Price Info object for particular product * * @param SaleableInterface $saleableItem * @param array $arguments * @return \Magento\Framework\Pricing\PriceInfoInterface * @throws \InvalidArgumentException */ public function create(SaleableInterface $saleableItem, array $arguments = []) { $type = $saleableItem->getTypeId(); if (isset($this->types[$type]['infoClass'])) { $priceInfo = $this->types[$type]['infoClass']; } else { $priceInfo = $this->types['default']['infoClass']; } if (isset($this->types[$type]['prices'])) { $priceCollection = $this->types[$type]['prices']; } else { $priceCollection = $this->types['default']['prices']; } $arguments['saleableItem'] = $saleableItem; $quantity = $saleableItem->getQty(); if ($quantity) { $arguments['quantity'] = $quantity; } $arguments['prices'] = $this->objectManager->create($priceCollection, ['saleableItem' => $arguments['saleableItem'], 'quantity' => $quantity]); return $this->objectManager->create($priceInfo, $arguments); }
public function testGetMinimalPriceFixedBundleWithOption() { $optionMaxPrice = 2; $this->baseAmount = 5; $result = 7; $this->prepareMock(); $this->saleableInterfaceMock->expects($this->once())->method('getPriceType')->willReturn(Price::PRICE_TYPE_FIXED); $this->customOptionPriceMock->expects($this->once())->method('getCustomOptionRange')->with(true)->willReturn($optionMaxPrice); $this->bundleCalculatorMock->expects($this->once())->method('getAmount')->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))->will($this->returnValue($result)); $this->assertSame($result, $this->finalPrice->getMinimalPrice()); //The second call should use cached value $this->assertSame($result, $this->finalPrice->getMinimalPrice()); }
/** * Test getMinimalPrice() */ public function testGetMinimalPriceWithMinimalPrice() { $minimalPrice = 5.234; $convertedPrice = 3.98; $finalPrice = 3.89; $this->priceCurrencyMock->expects($this->once())->method('convertAndRound')->with($minimalPrice)->will($this->returnValue($convertedPrice)); $this->basePriceMock->expects($this->never())->method('getValue'); $this->calculatorMock->expects($this->once())->method('getAmount')->with($this->equalTo($convertedPrice))->will($this->returnValue($finalPrice)); $this->saleableMock->expects($this->once())->method('getMinimalPrice')->will($this->returnValue($minimalPrice)); $result = $this->model->getMinimalPrice(); $this->assertEquals($finalPrice, $result); //The second time will return cached value $result = $this->model->getMinimalPrice(); $this->assertEquals($finalPrice, $result); }
public function testGetMinimalPriceFixedBundleWithOption() { $optionMaxPrice = 2; $this->baseAmount = 5; $result = 7; $this->prepareMock(); $customOptions = [$this->getMockBuilder(\Magento\Catalog\Api\Data\ProductCustomOptionInterface::class)->setMethods(['setProduct'])->getMockForAbstractClass()]; $this->productOptionRepositoryMock->expects(static::once())->method('getProductOptions')->with($this->saleableInterfaceMock)->willReturn($customOptions); $this->saleableInterfaceMock->expects($this->once())->method('getPriceType')->willReturn(Price::PRICE_TYPE_FIXED); $this->customOptionPriceMock->expects($this->once())->method('getCustomOptionRange')->with(true)->willReturn($optionMaxPrice); $this->bundleCalculatorMock->expects($this->once())->method('getAmount')->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))->will($this->returnValue($result)); $this->assertSame($result, $this->finalPrice->getMinimalPrice()); //The second call should use cached value $this->assertSame($result, $this->finalPrice->getMinimalPrice()); }
public function testGetMinimalPriceForFixedPricedBundleWithOptions() { $price = 5; $minOptionPrice = 1; $expectedValue = $price + $minOptionPrice; $this->saleableInterfaceMock->expects($this->once())->method('getPrice')->will($this->returnValue($price)); $this->saleableInterfaceMock->expects($this->once())->method('getPriceType')->willReturn(Price::PRICE_TYPE_FIXED); $this->priceInfoMock->expects($this->atLeastOnce())->method('getPrice')->with(CustomOptionPrice::PRICE_CODE)->willReturn($this->customOptionPriceMock); $this->customOptionPriceMock->expects($this->once())->method('getCustomOptionRange')->with(true)->willReturn($minOptionPrice); $this->priceCurrencyMock->expects($this->once())->method('convertAndRound')->will($this->returnArgument(0)); $this->bundleCalculatorMock->expects($this->once())->method('getMinRegularAmount')->with($expectedValue, $this->saleableInterfaceMock)->will($this->returnValue($expectedValue)); $result = $this->regularPrice->getMinimalPrice(); $this->assertEquals($expectedValue, $result, 'Incorrect amount'); //Calling a second time, should use cached value $result = $this->regularPrice->getMinimalPrice(); $this->assertEquals($expectedValue, $result, 'Incorrect amount the second time'); }
/** * Test get Value */ public function testGetValue() { $coreStoreId = 1; $coreWebsiteId = 1; $productId = 1; $customerGroupId = 1; $dateTime = time(); $catalogRulePrice = 55.12; $convertedPrice = 45.34; $this->coreStoreMock->expects($this->once())->method('getId')->will($this->returnValue($coreStoreId)); $this->coreStoreMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($coreWebsiteId)); $this->dataTimeMock->expects($this->once())->method('scopeDate')->with($this->equalTo($coreStoreId))->will($this->returnValue($dateTime)); $this->customerSessionMock->expects($this->once())->method('getCustomerGroupId')->will($this->returnValue($customerGroupId)); $this->catalogRuleResourceMock->expects($this->once())->method('getRulePrice')->will($this->returnValue($catalogRulePrice)); $this->saleableItemMock->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->priceCurrencyMock->expects($this->any())->method('convertAndRound')->with($catalogRulePrice)->will($this->returnValue($convertedPrice)); $this->assertEquals($convertedPrice, $this->object->getValue()); }
/** * @dataProvider getPriceIdProvider * @param string $prefix * @param string $suffix * @param string $defaultPrefix * @param string $defaultSuffix */ public function testGetPriceId($prefix, $suffix, $defaultPrefix, $defaultSuffix) { $priceId = 'price_id'; $this->saleable->expects($this->once())->method('getId')->will($this->returnValue($priceId)); if (!empty($prefix)) { $this->model->setData('price_id_prefix', $prefix); $expectedPriceId = $prefix . $priceId; } else { $expectedPriceId = $defaultPrefix . $priceId; } if (!empty($suffix)) { $this->model->setData('price_id_suffix', $suffix); $expectedPriceId = $expectedPriceId . $suffix; } else { $expectedPriceId = $expectedPriceId . $defaultSuffix; } $this->assertEquals($expectedPriceId, $this->model->getPriceId($defaultPrefix, $defaultSuffix)); }
/** * @dataProvider dataProviderForGetterAmount */ public function testGetterAmount($amountForBundle, $optionList, $expectedResult) { $this->baseCalculator->expects($this->atLeastOnce())->method('getAmount')->with($this->baseAmount, $this->saleableItem)->will($this->returnValue($this->createAmountMock($amountForBundle))); $options = []; foreach ($optionList as $optionData) { $options[] = $this->createOptionMock($optionData); } $price = $this->getMock('Magento\\Bundle\\Pricing\\Price\\BundleOptionPrice', [], [], '', false); $price->expects($this->atLeastOnce())->method('getOptions')->will($this->returnValue($options)); $this->priceMocks[Price\BundleOptionPrice::PRICE_CODE] = $price; // Price type of saleable items $this->saleableItem->expects($this->any())->method('getPriceType')->will($this->returnValue(ProductPrice::PRICE_TYPE_DYNAMIC)); $this->amountFactory->expects($this->atLeastOnce())->method('create')->with($expectedResult['fullAmount'], $expectedResult['adjustments']); if ($expectedResult['isMinAmount']) { $this->model->getAmount($this->baseAmount, $this->saleableItem); } else { $this->model->getMaxAmount($this->baseAmount, $this->saleableItem); } }
/** * @dataProvider getTestDataForCalculation */ public function testCalculation($optionList, $expected) { $storeId = 1; $this->saleableItemMock->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $this->selectionFactoryMock->expects($this->any())->method('create')->will($this->returnArgument(1)); $this->baseCalculator->expects($this->atLeastOnce())->method('getAmount')->will($this->returnValue($this->createAmountMock(['amount' => 0.0]))); $options = []; foreach ($optionList as $optionData) { $options[] = $this->createOptionMock($optionData); } /** @var \PHPUnit_Framework_MockObject_MockObject $optionsCollection */ $optionsCollection = $this->getMock('Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection', [], [], '', false); $optionsCollection->expects($this->atLeastOnce())->method('appendSelections')->will($this->returnSelf()); $optionsCollection->expects($this->atLeastOnce())->method('getIterator')->will($this->returnValue(new \ArrayIterator($options))); /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Type\AbstractType $typeMock */ $typeMock = $this->getMock('Magento\\Bundle\\Model\\Product\\Type', [], [], '', false); $typeMock->expects($this->any())->method('setStoreFilter')->with($storeId, $this->saleableItemMock); $typeMock->expects($this->any())->method('getOptionsCollection')->with($this->saleableItemMock)->will($this->returnValue($optionsCollection)); $this->saleableItemMock->expects($this->any())->method('getTypeInstance')->will($this->returnValue($typeMock)); $this->assertEquals($expected['min'], $this->bundleOptionPrice->getValue()); $this->assertEquals($expected['max'], $this->bundleOptionPrice->getMaxValue()); }
public function testGetValueWithNoCustomOption() { $this->saleableItem->expects($this->once())->method('getCustomOption')->with('simple_product')->willReturn(null); $this->assertEquals(0, $this->model->getValue()); }
public function testGetValueFromData() { $this->saleableItemMock->expects($this->once())->method('hasData')->with('catalog_rule_price')->willReturn(true); $this->saleableItemMock->expects($this->once())->method('getData')->with('catalog_rule_price')->willReturn('7.1'); $this->assertEquals(7.1, $this->object->getValue()); }
/** * Retrieve price object of given type and quantity * * @param string $priceCode * @return PriceInterface */ public function getPriceType($priceCode) { return $this->saleableItem->getPriceInfo()->getPrice($priceCode); }
/** * @param \Magento\Framework\Pricing\SaleableInterface $product * @return float */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { return $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)->getAmount()->getValue(); }
/** * @param \Magento\Framework\Pricing\SaleableInterface $product * @return float */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { return $product->getPrice(); }