From 6ac10980c6322e949b13af43dfea320113586c2f Mon Sep 17 00:00:00 2001 From: Eden Date: Mon, 16 Dec 2019 23:13:53 +0700 Subject: [PATCH 1/3] [Msrp] Cover MsrpPriceCalculator by Unit Test --- .../Unit/Pricing/MsrpPriceCalculatorTest.php | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php diff --git a/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php new file mode 100644 index 0000000000000..613547943c9a4 --- /dev/null +++ b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php @@ -0,0 +1,103 @@ +getObject(MsrpPriceCalculator::class, + [ + 'msrpPriceCalculators' => $msrpPriceCalculators + ] + ); + + $this->assertEquals($expected, $pricing->getMsrpPriceValue($productMock)); + } + + /** + * Data Provider for test getMrspPriceValue() + * + * @return array + */ + public function getMsrpPriceValueDataProvider() + { + return [ + 'Get Mrsp Price with grouped product and price calculator is also grouped product type' => [ + [ + [ + 'productType' => GroupedType::TYPE_CODE, + 'priceCalculator' => $this->createPriceCalculatorMock( + MsrpGroupedCalculator::class, 23.50) + ] + ], + $this->createProductMock(GroupedType::TYPE_CODE, 0), + 23.50 + ], + 'Get Mrsp Price with simple product and price calculator is grouped product type' => [ + [ + [ + 'productType' => GroupedType::TYPE_CODE, + 'priceCalculator' => $this->createPriceCalculatorMock( + MsrpGroupedCalculator::class, 0) + ] + ], + $this->createProductMock(Type::TYPE_SIMPLE, 24.88), + 24.88 + ] + ]; + } + + /** + * Create Price Calculator Mock + * + * @param string $class + * @param float $msrpPriceValue + * @return MockObject + */ + private function createPriceCalculatorMock($class, $msrpPriceValue) + { + $priceCalculatorMock = $this->createMock($class); + $priceCalculatorMock->expects($this->any())->method('getMsrpPriceValue')->willReturn($msrpPriceValue); + return $priceCalculatorMock; + } + + /** + * Create Product Mock + * + * @param string $typeId + * @param float $msrp + * @return MockObject + */ + private function createProductMock($typeId, $msrp) + { + $productMock = $this->createPartialMock(Product::class, ['getTypeId', 'getMsrp']); + $productMock->expects($this->any())->method('getTypeId')->willReturn($typeId); + $productMock->expects($this->any())->method('getMsrp')->willReturn($msrp); + return $productMock; + } +} From fb7aa9c9c41ec300d697eb2fbb5b3398fbbd7a00 Mon Sep 17 00:00:00 2001 From: Eden Date: Mon, 16 Dec 2019 23:37:11 +0700 Subject: [PATCH 2/3] fix static test --- .../Test/Unit/Pricing/MsrpPriceCalculatorTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php index 613547943c9a4..aac6852b7000c 100644 --- a/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php +++ b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php @@ -30,7 +30,8 @@ class MsrpPriceCalculatorTest extends TestCase public function testGetMsrpPriceValue($msrpPriceCalculators, $productMock, $expected) { $objectManager = new ObjectManager($this); - $pricing = $objectManager->getObject(MsrpPriceCalculator::class, + $pricing = $objectManager->getObject( + MsrpPriceCalculator::class, [ 'msrpPriceCalculators' => $msrpPriceCalculators ] @@ -52,7 +53,9 @@ public function getMsrpPriceValueDataProvider() [ 'productType' => GroupedType::TYPE_CODE, 'priceCalculator' => $this->createPriceCalculatorMock( - MsrpGroupedCalculator::class, 23.50) + MsrpGroupedCalculator::class, + 23.50 + ) ] ], $this->createProductMock(GroupedType::TYPE_CODE, 0), @@ -63,7 +66,9 @@ public function getMsrpPriceValueDataProvider() [ 'productType' => GroupedType::TYPE_CODE, 'priceCalculator' => $this->createPriceCalculatorMock( - MsrpGroupedCalculator::class, 0) + MsrpGroupedCalculator::class, + 0 + ) ] ], $this->createProductMock(Type::TYPE_SIMPLE, 24.88), From 2be8374fe0262eccb232ff9614211c126d496a4b Mon Sep 17 00:00:00 2001 From: Eden Date: Tue, 17 Dec 2019 07:42:22 +0700 Subject: [PATCH 3/3] Refactor code --- .../Unit/Pricing/MsrpPriceCalculatorTest.php | 85 +++++++++---------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php index aac6852b7000c..24e10207ff14c 100644 --- a/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php +++ b/app/code/Magento/Msrp/Test/Unit/Pricing/MsrpPriceCalculatorTest.php @@ -9,7 +9,7 @@ namespace Magento\Msrp\Test\Unit\Pricing; use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\Product\Type as Type; +use Magento\Catalog\Model\Product\Type as ProductType; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\GroupedProduct\Model\Product\Type\Grouped as GroupedType; use Magento\Msrp\Pricing\MsrpPriceCalculator; @@ -19,6 +19,36 @@ class MsrpPriceCalculatorTest extends TestCase { + /** + * @var MsrpPriceCalculator + */ + private $pricing; + + /** + * @var MsrpGroupedCalculator|MockObject + */ + private $msrpGroupedCalculatorMock; + + /** + * Prepare environment to test + */ + protected function setUp() + { + $objectManager = new ObjectManager($this); + $this->msrpGroupedCalculatorMock = $this->createMock(MsrpGroupedCalculator::class); + $this->pricing = $objectManager->getObject( + MsrpPriceCalculator::class, + [ + 'msrpPriceCalculators' => [ + [ + 'productType' => GroupedType::TYPE_CODE, + 'priceCalculator' => $this->msrpGroupedCalculatorMock + ] + ] + ] + ); + } + /** * Test getMrspPriceValue() with the data provider below * @@ -27,17 +57,12 @@ class MsrpPriceCalculatorTest extends TestCase * @param float $expected * @dataProvider getMsrpPriceValueDataProvider */ - public function testGetMsrpPriceValue($msrpPriceCalculators, $productMock, $expected) + public function testGetMsrpPriceValue($msrpPriceCalculatorPrice, $productMock, $expected) { - $objectManager = new ObjectManager($this); - $pricing = $objectManager->getObject( - MsrpPriceCalculator::class, - [ - 'msrpPriceCalculators' => $msrpPriceCalculators - ] - ); + $this->msrpGroupedCalculatorMock->expects($this->any()) + ->method('getMsrpPriceValue')->willReturn($msrpPriceCalculatorPrice); - $this->assertEquals($expected, $pricing->getMsrpPriceValue($productMock)); + $this->assertEquals($expected, $this->pricing->getMsrpPriceValue($productMock)); } /** @@ -48,49 +73,19 @@ public function testGetMsrpPriceValue($msrpPriceCalculators, $productMock, $expe public function getMsrpPriceValueDataProvider() { return [ - 'Get Mrsp Price with grouped product and price calculator is also grouped product type' => [ - [ - [ - 'productType' => GroupedType::TYPE_CODE, - 'priceCalculator' => $this->createPriceCalculatorMock( - MsrpGroupedCalculator::class, - 23.50 - ) - ] - ], + 'Get Mrsp Price with product and msrp calculator and the same product type' => [ + 23.50, $this->createProductMock(GroupedType::TYPE_CODE, 0), 23.50 ], - 'Get Mrsp Price with simple product and price calculator is grouped product type' => [ - [ - [ - 'productType' => GroupedType::TYPE_CODE, - 'priceCalculator' => $this->createPriceCalculatorMock( - MsrpGroupedCalculator::class, - 0 - ) - ] - ], - $this->createProductMock(Type::TYPE_SIMPLE, 24.88), + 'Get Mrsp Price with product and msrp calculator and the different product type' => [ + 24.88, + $this->createProductMock(ProductType::TYPE_SIMPLE, 24.88), 24.88 ] ]; } - /** - * Create Price Calculator Mock - * - * @param string $class - * @param float $msrpPriceValue - * @return MockObject - */ - private function createPriceCalculatorMock($class, $msrpPriceValue) - { - $priceCalculatorMock = $this->createMock($class); - $priceCalculatorMock->expects($this->any())->method('getMsrpPriceValue')->willReturn($msrpPriceValue); - return $priceCalculatorMock; - } - /** * Create Product Mock *