66
77namespace Magento \ConfigurableProduct \Test \Unit \Model \Product \Type \Configurable ;
88
9+ use Magento \Catalog \Model \Product ;
10+ use Magento \Catalog \Model \Product \Configuration \Item \Option ;
11+ use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Price as ConfigurablePrice ;
12+ use Magento \Framework \Event \ManagerInterface ;
13+ use Magento \Framework \Pricing \Amount \AmountInterface ;
14+ use Magento \Framework \Pricing \Price \PriceInterface ;
15+ use Magento \Framework \Pricing \PriceInfo \Base as PriceInfoBase ;
916use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
17+ use PHPUnit \Framework \MockObject \MockObject ;
1018
1119class PriceTest extends \PHPUnit \Framework \TestCase
1220{
13- /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price */
21+ /**
22+ * @var ObjectManagerHelper
23+ */
24+ protected $ objectManagerHelper ;
25+
26+ /**
27+ * @var ConfigurablePrice
28+ */
1429 protected $ model ;
1530
16- /** @var ObjectManagerHelper */
17- protected $ objectManagerHelper ;
31+ /**
32+ * @var ManagerInterface|MockObject
33+ */
34+ private $ eventManagerMock ;
1835
36+ /**
37+ * @inheritdoc
38+ */
1939 protected function setUp ()
2040 {
2141 $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
2242
43+ $ this ->eventManagerMock = $ this ->createPartialMock (
44+ ManagerInterface::class,
45+ ['dispatch ' ]
46+ );
2347 $ this ->model = $ this ->objectManagerHelper ->getObject (
24- \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Price::class
48+ ConfigurablePrice::class,
49+ ['eventManager ' => $ this ->eventManagerMock ]
2550 );
2651 }
2752
2853 public function testGetFinalPrice ()
2954 {
3055 $ finalPrice = 10 ;
3156 $ qty = 1 ;
32- $ configurableProduct = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
33- ->disableOriginalConstructor ()
34- ->setMethods (['getCustomOption ' , 'getPriceInfo ' , 'setFinalPrice ' , '__wakeUp ' ])
35- ->getMock ();
36- $ customOption = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Configuration \Item \Option::class)
57+
58+ /** @var Product|MockObject $configurableProduct */
59+ $ configurableProduct = $ this ->getMockBuilder (Product::class)
3760 ->disableOriginalConstructor ()
38- ->setMethods (['getProduct ' ])
61+ ->setMethods (['getCustomOption ' , ' getPriceInfo ' , ' setFinalPrice ' ])
3962 ->getMock ();
40- $ priceInfo = $ this ->getMockBuilder (\Magento \Framework \Pricing \PriceInfo \Base::class)
63+ /** @var PriceInfoBase|MockObject $priceInfo */
64+ $ priceInfo = $ this ->getMockBuilder (PriceInfoBase::class)
4165 ->disableOriginalConstructor ()
4266 ->setMethods (['getPrice ' ])
4367 ->getMock ();
44- $ price = $ this ->getMockBuilder (\Magento \Framework \Pricing \Price \PriceInterface::class)
68+ /** @var PriceInterface|MockObject $price */
69+ $ price = $ this ->getMockBuilder (PriceInterface::class)
4570 ->disableOriginalConstructor ()
4671 ->getMock ();
47- $ amount = $ this ->getMockBuilder (\Magento \Framework \Pricing \Amount \AmountInterface::class)
72+ /** @var AmountInterface|MockObject $amount */
73+ $ amount = $ this ->getMockBuilder (AmountInterface::class)
4874 ->disableOriginalConstructor ()
4975 ->getMock ();
5076
5177 $ configurableProduct ->expects ($ this ->any ())
5278 ->method ('getCustomOption ' )
5379 ->willReturnMap ([['simple_product ' , false ], ['option_ids ' , false ]]);
54- $ customOption ->expects ($ this ->never ())->method ('getProduct ' );
5580 $ configurableProduct ->expects ($ this ->once ())->method ('getPriceInfo ' )->willReturn ($ priceInfo );
5681 $ priceInfo ->expects ($ this ->once ())->method ('getPrice ' )->with ('final_price ' )->willReturn ($ price );
5782 $ price ->expects ($ this ->once ())->method ('getAmount ' )->willReturn ($ amount );
@@ -60,4 +85,60 @@ public function testGetFinalPrice()
6085
6186 $ this ->assertEquals ($ finalPrice , $ this ->model ->getFinalPrice ($ qty , $ configurableProduct ));
6287 }
88+
89+ public function testGetFinalPriceWithSimpleProduct ()
90+ {
91+ $ finalPrice = 10 ;
92+ $ qty = 1 ;
93+ $ customerGroupId = 1 ;
94+
95+ /** @var Product|MockObject $configurableProduct */
96+ $ configurableProduct = $ this ->createPartialMock (
97+ Product::class,
98+ ['getCustomOption ' , 'setFinalPrice ' , 'getCustomerGroupId ' ]
99+ );
100+ /** @var Option|MockObject $customOption */
101+ $ customOption = $ this ->createPartialMock (
102+ Option::class,
103+ ['getProduct ' ]
104+ );
105+ /** @var Product|MockObject $simpleProduct */
106+ $ simpleProduct = $ this ->createPartialMock (
107+ Product::class,
108+ ['setCustomerGroupId ' , 'setFinalPrice ' , 'getPrice ' , 'getTierPrice ' , 'getData ' , 'getCustomOption ' ]
109+ );
110+
111+ $ configurableProduct ->method ('getCustomOption ' )
112+ ->willReturnMap ([
113+ ['simple_product ' , $ customOption ],
114+ ['option_ids ' , false ]
115+ ]);
116+ $ configurableProduct ->method ('getCustomerGroupId ' )->willReturn ($ customerGroupId );
117+ $ configurableProduct ->expects ($ this ->atLeastOnce ())
118+ ->method ('setFinalPrice ' )
119+ ->with ($ finalPrice )
120+ ->willReturnSelf ();
121+ $ customOption ->method ('getProduct ' )->willReturn ($ simpleProduct );
122+ $ simpleProduct ->expects ($ this ->atLeastOnce ())
123+ ->method ('setCustomerGroupId ' )
124+ ->with ($ customerGroupId )
125+ ->willReturnSelf ();
126+ $ simpleProduct ->method ('getPrice ' )->willReturn ($ finalPrice );
127+ $ simpleProduct ->method ('getTierPrice ' )->with ($ qty )->willReturn ($ finalPrice );
128+ $ simpleProduct ->expects ($ this ->atLeastOnce ())
129+ ->method ('setFinalPrice ' )
130+ ->with ($ finalPrice )
131+ ->willReturnSelf ();
132+ $ simpleProduct ->method ('getData ' )->with ('final_price ' )->willReturn ($ finalPrice );
133+ $ simpleProduct ->method ('getCustomOption ' )->with ('option_ids ' )->willReturn (false );
134+ $ this ->eventManagerMock ->expects ($ this ->once ())
135+ ->method ('dispatch ' )
136+ ->with ('catalog_product_get_final_price ' , ['product ' => $ simpleProduct , 'qty ' => $ qty ]);
137+
138+ $ this ->assertEquals (
139+ $ finalPrice ,
140+ $ this ->model ->getFinalPrice ($ qty , $ configurableProduct ),
141+ 'The final price calculation is wrong '
142+ );
143+ }
63144}
0 commit comments