diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 4b5ec32bf61aa..ce2946df5d156 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -49,14 +49,20 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice */ private $configuredPriceSelection; + /** + * @var DiscountCalculator + */ + private $discountCalculator; + /** * @param Product $saleableItem * @param float $quantity * @param BundleCalculatorInterface $calculator * @param PriceCurrencyInterface $priceCurrency - * @param ItemInterface $item + * @param ItemInterface|null $item * @param JsonSerializer|null $serializer * @param ConfiguredPriceSelection|null $configuredPriceSelection + * @param DiscountCalculator|null $discountCalculator */ public function __construct( Product $saleableItem, @@ -65,7 +71,8 @@ public function __construct( PriceCurrencyInterface $priceCurrency, ItemInterface $item = null, JsonSerializer $serializer = null, - ConfiguredPriceSelection $configuredPriceSelection = null + ConfiguredPriceSelection $configuredPriceSelection = null, + DiscountCalculator $discountCalculator = null ) { $this->item = $item; $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() @@ -73,6 +80,8 @@ public function __construct( $this->configuredPriceSelection = $configuredPriceSelection ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(ConfiguredPriceSelection::class); + $this->discountCalculator = $discountCalculator + ?: \Magento\Framework\App\ObjectManager::getInstance()->get(DiscountCalculator::class); parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency); } @@ -144,12 +153,9 @@ public function getConfiguredAmount($baseValue = 0.) */ public function getValue() { - if ($this->item) { + if ($this->item && $this->item->getProduct()->getId()) { $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount(); - return parent::getValue() + - $this->priceInfo - ->getPrice(BundleDiscountPrice::PRICE_CODE) - ->calculateDiscount($configuredOptionsAmount); + return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); } return parent::getValue(); } diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php new file mode 100644 index 0000000000000..774ff5cc4df76 --- /dev/null +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -0,0 +1,183 @@ +getMockForAbstractClass(PriceInterface::class); + $basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue); + + $this->priceInfoMock = $this->createMock(Base::class); + $this->priceInfoMock->expects($this->any())->method('getPrice')->willReturn($basePrice); + $this->productMock = $this->getMockBuilder(Product::class) + ->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId']) + ->disableOriginalConstructor() + ->getMock(); + $this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock); + $this->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class); + + $this->jsonSerializerMock = $this->getMockBuilder(Json::class) + ->getMock(); + $this->configuredPriceSelectionMock = $this->getMockBuilder(ConfiguredPriceSelection::class) + ->setMethods(['getSelectionPriceList']) + ->disableOriginalConstructor() + ->getMock(); + $this->configuredPriceSelectionMock->expects($this->any())->method('getSelectionPriceList') + ->willReturn($this->prepareAndReturnSelectionPriceDataStub()); + $this->amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)->getMock(); + $this->amountInterfaceMock->expects($this->any())->method('getBaseAmount') + ->willReturn(100.00); + $this->calculatorMock = $this->getMockBuilder(Calculator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->calculatorMock->expects($this->any())->method('calculateBundleAmount') + ->willReturn($this->amountInterfaceMock); + $this->discountCalculatorMock = $this->getMockBuilder(DiscountCalculator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->discountCalculatorMock->expects($this->any())->method('calculateDiscount') + ->willReturn(-5.00); + $this->model = new ConfiguredPrice( + $this->productMock, + 1, + $this->calculatorMock, + $this->priceCurrencyMock, + null, + $this->jsonSerializerMock, + $this->configuredPriceSelectionMock, + $this->discountCalculatorMock, + ); + } + + /** + * Test of value getter when item presented + */ + public function testGetValueMethod(): void + { + $this->productMock->expects($this->any())->method('getId')->willReturn(123); + $this->itemMock = $this->getMockBuilder(ItemInterface::class) + ->getMock(); + $this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); + $this->model->setItem($this->itemMock); + $valueFromMock = $this->model->getValue(); + $this->assertEquals(95.00, $valueFromMock); + } + + /** + * Test of value getter if no product item + */ + public function testGetValueMethodNoItem(): void + { + $this->productMock = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->getMock(); + $this->itemMock = $this->getMockBuilder(ItemInterface::class) + ->getMock(); + $this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); + $this->productMock->expects($this->any())->method('getId')->willReturn(false); + $this->model->setItem($this->itemMock); + $valueFromMock = $this->model->getValue(); + $this->assertEquals(100.00, $valueFromMock); + } + + /** + * Stub data for calculation amount of bundle + * @return \Magento\Framework\DataObject[] + */ + private function prepareAndReturnSelectionPriceDataStub(): array + { + $first = new DataObject(); + $first->setValue(2); + $first->setQuantity(1); + $second = new DataObject(); + $second->setValue(3); + $second->setQuantity(1); + return [ + $first, + $second + ]; + } +}