From f649582fcefe166fc20d365a017d3cb7edc22aa9 Mon Sep 17 00:00:00 2001 From: monteshot Date: Fri, 9 Jul 2021 11:30:35 +0300 Subject: [PATCH 01/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Changed PRICE_CODE constant from non-exist class to $this class --- app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 4b5ec32bf61aa..a89828bb27e4b 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -148,7 +148,7 @@ public function getValue() $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount(); return parent::getValue() + $this->priceInfo - ->getPrice(BundleDiscountPrice::PRICE_CODE) + ->getPrice(self::PRICE_CODE) ->calculateDiscount($configuredOptionsAmount); } return parent::getValue(); From a1575f70569a14012c1146aa832f3572963e3497 Mon Sep 17 00:00:00 2001 From: monteshot Date: Fri, 9 Jul 2021 16:45:49 +0300 Subject: [PATCH 02/13] magento/magento2#33334:Inheriting from a class that doesn't exist - calculateDiscount method must be point as FinalPrice to its class but in ConfiguredPrice.php this method not exist. Also in FinalPrice invokes the \Magento\Bundle\Pricing\Price\DiscountCalculator::calculateDiscount - added unit test for method getValue --- .../Bundle/Pricing/Price/ConfiguredPrice.php | 13 +- .../Unit/Pricing/Price/BundlePriceTest.php | 156 ++++++++++++++++++ 2 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index a89828bb27e4b..693a63f6bcc4c 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -48,6 +48,10 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice * @var ConfiguredPriceSelection */ private $configuredPriceSelection; + /** + * @var \Magento\Bundle\Pricing\Price\DiscountCalculator + */ + private $discountCalculator; /** * @param Product $saleableItem @@ -63,6 +67,7 @@ public function __construct( $quantity, BundleCalculatorInterface $calculator, PriceCurrencyInterface $priceCurrency, + DiscountCalculator $discountCalculator, ItemInterface $item = null, JsonSerializer $serializer = null, ConfiguredPriceSelection $configuredPriceSelection = null @@ -73,6 +78,7 @@ public function __construct( $this->configuredPriceSelection = $configuredPriceSelection ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(ConfiguredPriceSelection::class); + $this->discountCalculator = $discountCalculator; parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency); } @@ -146,10 +152,9 @@ public function getValue() { if ($this->item) { $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount(); - return parent::getValue() + - $this->priceInfo - ->getPrice(self::PRICE_CODE) - ->calculateDiscount($configuredOptionsAmount); + if (!empty($this->item->getProduct())) { + return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); + } } return parent::getValue(); } diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php new file mode 100644 index 0000000000000..55c8d85635b1c --- /dev/null +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php @@ -0,0 +1,156 @@ +getMockForAbstractClass(PriceInterface::class); + $basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue); + + $this->priceInfo = $this->createMock(Base::class); + $this->priceInfo->expects($this->any())->method('getPrice')->willReturn($basePrice); + $this->product = $this->getMockBuilder(Product::class) + ->setMethods(['getPriceInfo', 'getOptionById', 'getResource']) + ->disableOriginalConstructor() + ->getMock(); + $this->product->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfo); + + $this->item = $this->getMockBuilder(ItemInterface::class) + ->getMock(); + $this->item->expects($this->any())->method('getProduct')->willReturn($this->product); + + $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.0); + $this->calculator = $this->getMockBuilder(Calculator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->calculator->expects($this->any())->method('calculateBundleAmount') + ->willReturn($this->amountInterfaceMock); + $this->discountCalculator = $this->getMockBuilder(DiscountCalculator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->discountCalculator->expects($this->any())->method('calculateDiscount') + ->willReturn(-5.0); + $this->model = new ConfiguredPrice($this->product, 1, $this->calculator, $this->priceCurrencyMock, $this->discountCalculator,null, $this->jsonSerializerMock, $this->configuredPriceSelectionMock); + $this->model->setItem($this->item); + } + + private function prepareAndReturnSelectionPriceDataStub() + { + $first = new DataObject(); + $first->setValue(2); + $first->setQuantity(1); + $second = new DataObject(); + $second->setValue(3); + $second->setQuantity(1); + return [ + $first, + $second + ]; + } + /** + * Test of value getter + */ + public function testGetValueMethod() + { + $valueFromMock = $this->model->getValue(); + $this->assertEquals(95., $valueFromMock); + } +} From 3a92f3e78b15b54268b1ed8ac7eb33758a4e2843 Mon Sep 17 00:00:00 2001 From: monteshot Date: Mon, 12 Jul 2021 09:19:08 +0300 Subject: [PATCH 03/13] Removed unnecessary nesting in if statements according to the interface --- app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 693a63f6bcc4c..2be281757fb52 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -150,11 +150,9 @@ public function getConfiguredAmount($baseValue = 0.) */ public function getValue() { - if ($this->item) { + if ($this->item && $this->item->getProduct()->getId()) { $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount(); - if (!empty($this->item->getProduct())) { - return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); - } + return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); } return parent::getValue(); } From c717b3466b3bf2fc2adaf7335186602f840defcf Mon Sep 17 00:00:00 2001 From: monteshot Date: Mon, 12 Jul 2021 09:19:08 +0300 Subject: [PATCH 04/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Removed unnecessary nesting in if statements according to the interface --- app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 693a63f6bcc4c..2be281757fb52 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -150,11 +150,9 @@ public function getConfiguredAmount($baseValue = 0.) */ public function getValue() { - if ($this->item) { + if ($this->item && $this->item->getProduct()->getId()) { $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount(); - if (!empty($this->item->getProduct())) { - return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); - } + return parent::getValue() + $this->discountCalculator->calculateDiscount($this->item->getProduct(), $configuredOptionsAmount); } return parent::getValue(); } From b0c21333b445699de8468940a3535c4d98c39fe8 Mon Sep 17 00:00:00 2001 From: monteshot Date: Mon, 12 Jul 2021 09:42:18 +0300 Subject: [PATCH 05/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Removed unnecessary properties from test class --- .../Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php index 55c8d85635b1c..b1910823dc84c 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php @@ -72,10 +72,6 @@ class BundlePriceTest extends TestCase * @var ConfiguredPriceSelection|MockObject */ private $configuredPriceSelectionMock; - /** - * @var array - */ - private $selectionPriceDataSampleData; /** * @var AmountInterface|MockObject */ From 1aa93e06373dc6aafe299d37c4e76aa5fbbc868b Mon Sep 17 00:00:00 2001 From: monteshot Date: Mon, 12 Jul 2021 11:12:07 +0300 Subject: [PATCH 06/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Update the unit test for getter in case non-existing the item --- .../Unit/Pricing/Price/BundlePriceTest.php | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php index b1910823dc84c..41ca4172fb86c 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php @@ -92,10 +92,11 @@ protected function setUp(): void $this->priceInfo = $this->createMock(Base::class); $this->priceInfo->expects($this->any())->method('getPrice')->willReturn($basePrice); $this->product = $this->getMockBuilder(Product::class) - ->setMethods(['getPriceInfo', 'getOptionById', 'getResource']) + ->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId']) ->disableOriginalConstructor() ->getMock(); $this->product->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfo); + $this->product->expects($this->any())->method('getId')->willReturn(123); $this->item = $this->getMockBuilder(ItemInterface::class) ->getMock(); @@ -124,7 +125,16 @@ protected function setUp(): void ->getMock(); $this->discountCalculator->expects($this->any())->method('calculateDiscount') ->willReturn(-5.0); - $this->model = new ConfiguredPrice($this->product, 1, $this->calculator, $this->priceCurrencyMock, $this->discountCalculator,null, $this->jsonSerializerMock, $this->configuredPriceSelectionMock); + $this->model = new ConfiguredPrice( + $this->product, + 1, + $this->calculator, + $this->priceCurrencyMock, + $this->discountCalculator, + null, + $this->jsonSerializerMock, + $this->configuredPriceSelectionMock + ); $this->model->setItem($this->item); } @@ -141,6 +151,7 @@ private function prepareAndReturnSelectionPriceDataStub() $second ]; } + /** * Test of value getter */ @@ -149,4 +160,23 @@ public function testGetValueMethod() $valueFromMock = $this->model->getValue(); $this->assertEquals(95., $valueFromMock); } + + /** + * Test of value getter if no product item + */ + public function testGetValueMethodNoItem() + { + unset($this->item); + $this->product = $this->getMockBuilder(Product::class) + //->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId']) + ->disableOriginalConstructor() + ->getMock(); + $this->item = $this->getMockBuilder(ItemInterface::class) + ->getMock(); + $this->item->expects($this->any())->method('getProduct')->willReturn($this->product); + $this->product->expects($this->any())->method('getId')->willReturn(false); + $this->model->setItem($this->item); + $valueFromMock = $this->model->getValue(); + $this->assertEquals(100., $valueFromMock); + } } From 33914c154dd2f04ff29d9a547c81b02e0dcd428f Mon Sep 17 00:00:00 2001 From: monteshot Date: Mon, 12 Jul 2021 17:06:58 +0300 Subject: [PATCH 07/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Made changes backwards compatible. --- app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php | 7 ++++--- .../Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 2be281757fb52..9471868af63c0 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -67,10 +67,10 @@ public function __construct( $quantity, BundleCalculatorInterface $calculator, PriceCurrencyInterface $priceCurrency, - DiscountCalculator $discountCalculator, 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() @@ -78,7 +78,8 @@ public function __construct( $this->configuredPriceSelection = $configuredPriceSelection ?: \Magento\Framework\App\ObjectManager::getInstance() ->get(ConfiguredPriceSelection::class); - $this->discountCalculator = $discountCalculator; + $this->discountCalculator = $discountCalculator + ?: \Magento\Framework\App\ObjectManager::getInstance()->get(DiscountCalculator::class); parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency); } diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php index 41ca4172fb86c..2dd42212548b3 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php @@ -130,10 +130,10 @@ protected function setUp(): void 1, $this->calculator, $this->priceCurrencyMock, - $this->discountCalculator, null, $this->jsonSerializerMock, - $this->configuredPriceSelectionMock + $this->configuredPriceSelectionMock, + $this->discountCalculator, ); $this->model->setItem($this->item); } From 0f32c4d1f2dd3177bf0eef2dc7f7f27921c9d9e1 Mon Sep 17 00:00:00 2001 From: Sergey Kusch Date: Mon, 12 Jul 2021 18:54:22 +0300 Subject: [PATCH 08/13] magento#33334:Inheriting from a class that doesn't exist - Renamed Test class according to the recommendations. --- .../Price/{BundlePriceTest.php => ConfiguredPriceTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename app/code/Magento/Bundle/Test/Unit/Pricing/Price/{BundlePriceTest.php => ConfiguredPriceTest.php} (99%) diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php similarity index 99% rename from app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php rename to app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php index 2dd42212548b3..5ef1283dde394 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -28,7 +28,7 @@ /** * Test for \Magento\Bundle\Pricing\Price\ConfiguredPrice */ -class BundlePriceTest extends TestCase +class ConfiguredPriceTest extends TestCase { /** * @var float From 72facef7b43d5999d9848f5aa428cbf6559fc0b0 Mon Sep 17 00:00:00 2001 From: Sergey Kusch Date: Mon, 12 Jul 2021 18:54:22 +0300 Subject: [PATCH 09/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Renamed Test class according to the recommendations. --- .../Price/{BundlePriceTest.php => ConfiguredPriceTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename app/code/Magento/Bundle/Test/Unit/Pricing/Price/{BundlePriceTest.php => ConfiguredPriceTest.php} (99%) diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php similarity index 99% rename from app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php rename to app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php index 2dd42212548b3..5ef1283dde394 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/BundlePriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -28,7 +28,7 @@ /** * Test for \Magento\Bundle\Pricing\Price\ConfiguredPrice */ -class BundlePriceTest extends TestCase +class ConfiguredPriceTest extends TestCase { /** * @var float From 962543624be48b904af16ca2495eab12fff44a31 Mon Sep 17 00:00:00 2001 From: monteshot Date: Tue, 13 Jul 2021 08:23:11 +0300 Subject: [PATCH 10/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Clarified comment for main test method --- .../Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php index 5ef1283dde394..041ef025660a9 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -153,7 +153,7 @@ private function prepareAndReturnSelectionPriceDataStub() } /** - * Test of value getter + * Test of value getter when item presented */ public function testGetValueMethod() { From f34f5c8acfc60daeff03a419c1e1d291fdf3e0f9 Mon Sep 17 00:00:00 2001 From: monteshot Date: Tue, 13 Jul 2021 09:36:08 +0300 Subject: [PATCH 11/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Adjusted code style for the unit test - Replaced FQPN using import --- .../Bundle/Pricing/Price/ConfiguredPrice.php | 2 +- .../Pricing/Price/ConfiguredPriceTest.php | 101 +++++++++--------- 2 files changed, 53 insertions(+), 50 deletions(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 9471868af63c0..aa5ecc3ba173e 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -49,7 +49,7 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice */ private $configuredPriceSelection; /** - * @var \Magento\Bundle\Pricing\Price\DiscountCalculator + * @var DiscountCalculator */ private $discountCalculator; diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php index 041ef025660a9..07793a50fe992 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -10,9 +10,6 @@ use Magento\Bundle\Pricing\Price\DiscountCalculator; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface; -use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface; -use Magento\Catalog\Model\Product\Option; -use Magento\Catalog\Model\Product\Option\Type\DefaultType; use Magento\Bundle\Pricing\Price\ConfiguredPrice; use Magento\Bundle\Pricing\Adjustment\Calculator; use Magento\Catalog\Pricing\Price\ConfiguredPriceSelection; @@ -33,53 +30,57 @@ class ConfiguredPriceTest extends TestCase /** * @var float */ - protected $basePriceValue = 100.; + private $basePriceValue = 100.; /** - * @var MockObject + * @var ItemInterface|MockObject */ - protected $item; + private $itemMock; /** - * @var MockObject + * @var Product|MockObject */ - protected $product; + private $productMock; /** * @var MockObject */ - protected $calculator; + private $calculator; /** - * @var MockObject + * @var Base|MockObject */ - protected $priceInfo; + private $priceInfoMock; /** * @var ConfiguredPrice */ - protected $model; + private $model; /** * @var PriceCurrencyInterface|MockObject */ - protected $priceCurrencyMock; + private $priceCurrencyMock; + /** * @var Json|MockObject */ private $jsonSerializerMock; + /** * @var ConfiguredPriceSelection|MockObject */ private $configuredPriceSelectionMock; + /** * @var AmountInterface|MockObject */ private $amountInterfaceMock; + /** * @var DiscountCalculator|MockObject */ - private $discountCalculator; + private $discountCalculatorMock; /** * Initialize base dependencies @@ -89,18 +90,18 @@ protected function setUp(): void $basePrice = $this->getMockForAbstractClass(PriceInterface::class); $basePrice->expects($this->any())->method('getValue')->willReturn($this->basePriceValue); - $this->priceInfo = $this->createMock(Base::class); - $this->priceInfo->expects($this->any())->method('getPrice')->willReturn($basePrice); - $this->product = $this->getMockBuilder(Product::class) + $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->product->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfo); - $this->product->expects($this->any())->method('getId')->willReturn(123); + $this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock); + $this->productMock->expects($this->any())->method('getId')->willReturn(123); - $this->item = $this->getMockBuilder(ItemInterface::class) + $this->itemMock = $this->getMockBuilder(ItemInterface::class) ->getMock(); - $this->item->expects($this->any())->method('getProduct')->willReturn($this->product); + $this->itemMock->expects($this->any())->method('getProduct')->willReturn($this->productMock); $this->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class); @@ -120,42 +121,28 @@ protected function setUp(): void ->getMock(); $this->calculator->expects($this->any())->method('calculateBundleAmount') ->willReturn($this->amountInterfaceMock); - $this->discountCalculator = $this->getMockBuilder(DiscountCalculator::class) + $this->discountCalculatorMock = $this->getMockBuilder(DiscountCalculator::class) ->disableOriginalConstructor() ->getMock(); - $this->discountCalculator->expects($this->any())->method('calculateDiscount') + $this->discountCalculatorMock->expects($this->any())->method('calculateDiscount') ->willReturn(-5.0); $this->model = new ConfiguredPrice( - $this->product, + $this->productMock, 1, $this->calculator, $this->priceCurrencyMock, null, $this->jsonSerializerMock, $this->configuredPriceSelectionMock, - $this->discountCalculator, + $this->discountCalculatorMock, ); - $this->model->setItem($this->item); - } - - private function prepareAndReturnSelectionPriceDataStub() - { - $first = new DataObject(); - $first->setValue(2); - $first->setQuantity(1); - $second = new DataObject(); - $second->setValue(3); - $second->setQuantity(1); - return [ - $first, - $second - ]; + $this->model->setItem($this->itemMock); } /** * Test of value getter when item presented */ - public function testGetValueMethod() + public function testGetValueMethod(): void { $valueFromMock = $this->model->getValue(); $this->assertEquals(95., $valueFromMock); @@ -164,19 +151,35 @@ public function testGetValueMethod() /** * Test of value getter if no product item */ - public function testGetValueMethodNoItem() + public function testGetValueMethodNoItem(): void { - unset($this->item); - $this->product = $this->getMockBuilder(Product::class) - //->setMethods(['getPriceInfo', 'getOptionById', 'getResource', 'getId']) + $this->productMock = $this->getMockBuilder(Product::class) ->disableOriginalConstructor() ->getMock(); - $this->item = $this->getMockBuilder(ItemInterface::class) + $this->itemMock = $this->getMockBuilder(ItemInterface::class) ->getMock(); - $this->item->expects($this->any())->method('getProduct')->willReturn($this->product); - $this->product->expects($this->any())->method('getId')->willReturn(false); - $this->model->setItem($this->item); + $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., $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 + ]; + } } From 2ec38bc83d89542435ce024de2b209dd1c999052 Mon Sep 17 00:00:00 2001 From: monteshot Date: Wed, 14 Jul 2021 18:36:22 +0300 Subject: [PATCH 12/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Adjusted code style for configuredPriceSelection propery in the target code - Added mock suffice to last property in unit test - Added decimals after point in unit test - Transferred setting up some mocks to the specific test case --- .../Bundle/Pricing/Price/ConfiguredPrice.php | 1 + .../Pricing/Price/ConfiguredPriceTest.php | 32 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index aa5ecc3ba173e..9a326de7453d0 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -48,6 +48,7 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice * @var ConfiguredPriceSelection */ private $configuredPriceSelection; + /** * @var DiscountCalculator */ diff --git a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php index 07793a50fe992..774ff5cc4df76 100644 --- a/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Pricing/Price/ConfiguredPriceTest.php @@ -30,7 +30,7 @@ class ConfiguredPriceTest extends TestCase /** * @var float */ - private $basePriceValue = 100.; + private $basePriceValue = 100.00; /** * @var ItemInterface|MockObject @@ -43,9 +43,9 @@ class ConfiguredPriceTest extends TestCase private $productMock; /** - * @var MockObject + * @var Calculator|MockObject */ - private $calculator; + private $calculatorMock; /** * @var Base|MockObject @@ -97,12 +97,6 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); $this->productMock->expects($this->once())->method('getPriceInfo')->willReturn($this->priceInfoMock); - $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->priceCurrencyMock = $this->getMockForAbstractClass(PriceCurrencyInterface::class); $this->jsonSerializerMock = $this->getMockBuilder(Json::class) @@ -115,28 +109,27 @@ protected function setUp(): void ->willReturn($this->prepareAndReturnSelectionPriceDataStub()); $this->amountInterfaceMock = $this->getMockBuilder(AmountInterface::class)->getMock(); $this->amountInterfaceMock->expects($this->any())->method('getBaseAmount') - ->willReturn(100.0); - $this->calculator = $this->getMockBuilder(Calculator::class) + ->willReturn(100.00); + $this->calculatorMock = $this->getMockBuilder(Calculator::class) ->disableOriginalConstructor() ->getMock(); - $this->calculator->expects($this->any())->method('calculateBundleAmount') + $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.0); + ->willReturn(-5.00); $this->model = new ConfiguredPrice( $this->productMock, 1, - $this->calculator, + $this->calculatorMock, $this->priceCurrencyMock, null, $this->jsonSerializerMock, $this->configuredPriceSelectionMock, $this->discountCalculatorMock, ); - $this->model->setItem($this->itemMock); } /** @@ -144,8 +137,13 @@ protected function setUp(): void */ 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., $valueFromMock); + $this->assertEquals(95.00, $valueFromMock); } /** @@ -162,7 +160,7 @@ public function testGetValueMethodNoItem(): void $this->productMock->expects($this->any())->method('getId')->willReturn(false); $this->model->setItem($this->itemMock); $valueFromMock = $this->model->getValue(); - $this->assertEquals(100., $valueFromMock); + $this->assertEquals(100.00, $valueFromMock); } /** From 0669115786d5819b235f975cd932a44f748600dd Mon Sep 17 00:00:00 2001 From: monteshot Date: Sun, 18 Jul 2021 14:16:08 +0300 Subject: [PATCH 13/13] magento/magento2#33334:Inheriting from a class that doesn't exist - Added space between $discountCalculator and $configuredPriceSelection properties - Updated PHPdoc in constructor --- app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php index 9a326de7453d0..ce2946df5d156 100644 --- a/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php +++ b/app/code/Magento/Bundle/Pricing/Price/ConfiguredPrice.php @@ -59,9 +59,10 @@ class ConfiguredPrice extends CatalogPrice\FinalPrice implements ConfiguredPrice * @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,