@@ -36,17 +36,26 @@ protected function setUp()
36
36
->setMethods ([
37
37
'getQuote ' ,
38
38
'getAllItems ' ,
39
- 'getFreeShipping '
39
+ 'getFreeShipping ' ,
40
+ 'setFreeShipping '
40
41
])
41
42
->getMock ();
42
43
43
44
$ this ->storeManagerMock = $ this ->getMockBuilder ('Magento\Store\Model\StoreManagerInterface ' )
44
45
->disableOriginalConstructor ()
45
46
->getMock ();
46
47
48
+ $ storeMock = $ this ->getMockBuilder ('Magento\Store\Model\Store ' )
49
+ ->disableOriginalConstructor ()
50
+ ->getMock ();
51
+
52
+ $ this ->storeManagerMock ->expects ($ this ->once ())
53
+ ->method ('getStore ' )
54
+ ->willReturn ($ storeMock );
55
+
47
56
$ this ->calculatorMock = $ this ->getMockBuilder ('Magento\OfflineShipping\Model\SalesRule\Calculator ' )
48
57
->disableOriginalConstructor ()
49
- // ->setMethods(['init'])
58
+ ->setMethods (['init ' , ' processFreeShipping ' ])
50
59
->getMock ();
51
60
52
61
$ this ->model = $ helper ->getObject ('Magento\OfflineShipping\Model\Quote\Freeshipping ' , [
@@ -75,59 +84,95 @@ public function testCollectWithEmptyAddressItems()
75
84
76
85
/**
77
86
* @dataProvider scenariosDataProvider
87
+ * @param $isNoDiscount
78
88
*/
79
- public function testCollectWithAddressItems (
80
- $ addressItemMockNoDiscountValue ,
81
- $ addressMockGetFreeShippingExpects
82
- ) {
89
+ public function testCollectWithAddressItems ($ isNoDiscount )
90
+ {
83
91
$ quoteMock = $ this ->getMockBuilder ('Magento\Sales\Model\Quote ' )
84
92
->disableOriginalConstructor ()
85
93
->getMock ();
86
94
87
- $ this ->addressMock ->expects ($ this ->any ())
95
+ $ this ->addressMock ->expects ($ this ->once ())
88
96
->method ('getQuote ' )
89
97
->willReturn ($ quoteMock );
90
98
91
99
$ addressItemMock = $ this ->getMockBuilder ('Magento\Sales\Model\Quote\Address\Item ' )
92
100
->disableOriginalConstructor ()
93
- ->setMethods (['getNoDiscount ' , 'getFreeShipping ' ])
101
+ ->setMethods ([
102
+ 'getNoDiscount ' ,
103
+ 'setFreeShipping ' ,
104
+ 'getParentItemId ' ,
105
+ 'getFreeShipping ' ,
106
+ 'getHasChildren ' ,
107
+ 'isChildrenCalculated ' ,
108
+ 'getChildren '
109
+ ])
94
110
->getMock ();
95
111
112
+ $ this ->addressMock ->expects ($ this ->once ())
113
+ ->method ('getAllItems ' )
114
+ ->willReturn ([$ addressItemMock ]);
115
+
116
+ $ this ->calculatorMock ->expects ($ this ->once ())
117
+ ->method ('init ' );
118
+
96
119
$ addressItemMock ->expects ($ this ->once ())
97
120
->method ('getNoDiscount ' )
98
- ->willReturn ($ addressItemMockNoDiscountValue );
121
+ ->willReturn ($ isNoDiscount );
99
122
100
- $ addressItemMock ->expects ($ this ->any ())
123
+ $ addressItemMock ->expects ($ isNoDiscount ? $ this ->once () : $ this ->never ())
124
+ ->method ('setFreeShipping ' );
125
+
126
+ $ addressItemMock ->expects ($ isNoDiscount ? $ this ->never () : $ this ->once ())
127
+ ->method ('getParentItemId ' )
128
+ ->willReturn (false );
129
+
130
+ $ this ->calculatorMock ->expects ($ isNoDiscount ? $ this ->never () : $ this ->exactly (2 ))
131
+ ->method ('processFreeShipping ' );
132
+
133
+ $ addressItemMock ->expects ($ isNoDiscount ? $ this ->never () : $ this ->once ())
101
134
->method ('getFreeShipping ' )
102
135
->willReturn (true );
103
136
104
- $ this -> addressMock -> expects ( $ addressMockGetFreeShippingExpects )
105
- ->method ('getFreeShipping ' )
106
- ->willReturn (false );
137
+ $ addressItemMock -> expects ( $ isNoDiscount ? $ this -> never () : $ this -> once () )
138
+ ->method ('getHasChildren ' )
139
+ ->willReturn (true );
107
140
108
- $ storeMock = $ this ->getMockBuilder ('Magento\Store\Model\Store ' )
141
+ $ addressItemMock ->expects ($ isNoDiscount ? $ this ->never () : $ this ->once ())
142
+ ->method ('isChildrenCalculated ' )
143
+ ->willReturn (true );
144
+
145
+ $ childMock = $ this ->getMockBuilder ('Magento\Sales\Model\Quote\Item\AbstractItem ' )
109
146
->disableOriginalConstructor ()
147
+ ->setMethods (['setFreeShipping ' , 'getQuote ' , 'getAddress ' , 'getOptionByCode ' ])
110
148
->getMock ();
111
149
112
- $ this -> storeManagerMock -> expects ( $ this ->once ())
113
- ->method ('getStore ' )
114
- ->willReturn ($ storeMock );
150
+ $ addressItemMock -> expects ( $ isNoDiscount ? $ this -> never () : $ this ->once ())
151
+ ->method ('getChildren ' )
152
+ ->willReturn ([ $ childMock ] );
115
153
116
- $ this -> calculatorMock -> expects ( $ this ->once ())
117
- ->method ('init ' );
154
+ $ childMock -> expects ( $ isNoDiscount ? $ this -> never () : $ this ->once ())
155
+ ->method ('setFreeShipping ' );
118
156
119
- $ this ->addressMock ->expects ($ this ->once ())
120
- ->method ('getAllItems ' )
121
- ->willReturn ([ $ addressItemMock ] );
157
+ $ this ->addressMock ->expects ($ isNoDiscount ? $ this -> never () : $ this ->once ())
158
+ ->method ('getFreeShipping ' )
159
+ ->willReturn (false );
122
160
123
- $ this ->model ->collect ($ this ->addressMock );
161
+ $ this ->addressMock ->expects ($ isNoDiscount ? $ this ->once () : $ this ->exactly (2 ))
162
+ ->method ('setFreeShipping ' );
163
+
164
+ $ this ->assertSame ($ this ->model ->collect ($ this ->addressMock ), $ this ->model );
124
165
}
125
166
126
167
public function scenariosDataProvider ()
127
168
{
128
169
return [
129
- [true , $ this ->never ()],
130
- [false , $ this ->once ()]
170
+ [
171
+ true , // there is no a discount
172
+ ],
173
+ [
174
+ false , // there is a discount
175
+ ]
131
176
];
132
177
}
133
178
}
0 commit comments