6
6
7
7
namespace Magento \ConfigurableProduct \Test \Unit \Model \Product \Type \Configurable ;
8
8
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 ;
9
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
17
+ use PHPUnit \Framework \MockObject \MockObject ;
10
18
11
19
class PriceTest extends \PHPUnit \Framework \TestCase
12
20
{
13
- /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price */
21
+ /**
22
+ * @var ObjectManagerHelper
23
+ */
24
+ protected $ objectManagerHelper ;
25
+
26
+ /**
27
+ * @var ConfigurablePrice
28
+ */
14
29
protected $ model ;
15
30
16
- /** @var ObjectManagerHelper */
17
- protected $ objectManagerHelper ;
31
+ /**
32
+ * @var ManagerInterface|MockObject
33
+ */
34
+ private $ eventManagerMock ;
18
35
36
+ /**
37
+ * @inheritdoc
38
+ */
19
39
protected function setUp ()
20
40
{
21
41
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
22
42
43
+ $ this ->eventManagerMock = $ this ->createPartialMock (
44
+ ManagerInterface::class,
45
+ ['dispatch ' ]
46
+ );
23
47
$ this ->model = $ this ->objectManagerHelper ->getObject (
24
- \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Price::class
48
+ ConfigurablePrice::class,
49
+ ['eventManager ' => $ this ->eventManagerMock ]
25
50
);
26
51
}
27
52
28
53
public function testGetFinalPrice ()
29
54
{
30
55
$ finalPrice = 10 ;
31
56
$ 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)
37
60
->disableOriginalConstructor ()
38
- ->setMethods (['getProduct ' ])
61
+ ->setMethods (['getCustomOption ' , ' getPriceInfo ' , ' setFinalPrice ' ])
39
62
->getMock ();
40
- $ priceInfo = $ this ->getMockBuilder (\Magento \Framework \Pricing \PriceInfo \Base::class)
63
+ /** @var PriceInfoBase|MockObject $priceInfo */
64
+ $ priceInfo = $ this ->getMockBuilder (PriceInfoBase::class)
41
65
->disableOriginalConstructor ()
42
66
->setMethods (['getPrice ' ])
43
67
->getMock ();
44
- $ price = $ this ->getMockBuilder (\Magento \Framework \Pricing \Price \PriceInterface::class)
68
+ /** @var PriceInterface|MockObject $price */
69
+ $ price = $ this ->getMockBuilder (PriceInterface::class)
45
70
->disableOriginalConstructor ()
46
71
->getMock ();
47
- $ amount = $ this ->getMockBuilder (\Magento \Framework \Pricing \Amount \AmountInterface::class)
72
+ /** @var AmountInterface|MockObject $amount */
73
+ $ amount = $ this ->getMockBuilder (AmountInterface::class)
48
74
->disableOriginalConstructor ()
49
75
->getMock ();
50
76
51
77
$ configurableProduct ->expects ($ this ->any ())
52
78
->method ('getCustomOption ' )
53
79
->willReturnMap ([['simple_product ' , false ], ['option_ids ' , false ]]);
54
- $ customOption ->expects ($ this ->never ())->method ('getProduct ' );
55
80
$ configurableProduct ->expects ($ this ->once ())->method ('getPriceInfo ' )->willReturn ($ priceInfo );
56
81
$ priceInfo ->expects ($ this ->once ())->method ('getPrice ' )->with ('final_price ' )->willReturn ($ price );
57
82
$ price ->expects ($ this ->once ())->method ('getAmount ' )->willReturn ($ amount );
@@ -60,4 +85,60 @@ public function testGetFinalPrice()
60
85
61
86
$ this ->assertEquals ($ finalPrice , $ this ->model ->getFinalPrice ($ qty , $ configurableProduct ));
62
87
}
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
+ }
63
144
}
0 commit comments