@@ -70,6 +70,16 @@ class ProductStockTest extends TestCase
70
70
*/
71
71
private $ stockStatusMock ;
72
72
73
+ /**
74
+ * @var ProductInterface|MockObject
75
+ */
76
+ private $ optionProductMock ;
77
+
78
+ /**
79
+ * @var Option|MockObject
80
+ */
81
+ private $ qtyOptionMock ;
82
+
73
83
/**
74
84
* Set up mocks and initialize the ProductStock class
75
85
*/
@@ -87,15 +97,25 @@ protected function setUp(): void
87
97
);
88
98
$ this ->stockStatusMock = $ this ->getMockBuilder (StockStatusInterface::class)
89
99
->disableOriginalConstructor ()
90
- ->addMethods (['getHasError ' ])
100
+ ->onlyMethods (['getQty ' , ' getStockStatus ' ])
91
101
->getMockForAbstractClass ();
92
102
$ this ->cartItemMock = $ this ->getMockBuilder (Item::class)
93
103
->addMethods (['getQtyToAdd ' , 'getPreviousQty ' ])
94
104
->onlyMethods (['getStore ' , 'getProductType ' , 'getProduct ' , 'getChildren ' , 'getQtyOptions ' ])
95
105
->disableOriginalConstructor ()
96
106
->getMock ();
97
- $ this ->productMock = $ this ->createMock (ProductInterface::class);
107
+ $ this ->productMock = $ this ->getMockBuilder (ProductInterface::class)
108
+ ->onlyMethods (['getId ' ])
109
+ ->addMethods (['getStore ' ])
110
+ ->disableOriginalConstructor ()
111
+ ->getMockForAbstractClass ();
112
+ $ this ->optionProductMock = $ this ->getMockBuilder (ProductInterface::class)
113
+ ->onlyMethods (['getId ' ])
114
+ ->addMethods (['getStore ' ])
115
+ ->disableOriginalConstructor ()
116
+ ->getMockForAbstractClass ();
98
117
$ this ->storeMock = $ this ->createMock (StoreInterface::class);
118
+ $ this ->qtyOptionMock = $ this ->createMock (Option::class);
99
119
}
100
120
101
121
/**
@@ -121,16 +141,25 @@ public function testIsProductAvailableForSimpleProductWithStock(): void
121
141
$ this ->storeMock ->expects ($ this ->once ())
122
142
->method ('getId ' )
123
143
->willReturn (1 );
124
- $ this ->productMock ->expects ($ this ->once ( ))
144
+ $ this ->productMock ->expects ($ this ->exactly ( 3 ))
125
145
->method ('getId ' )
126
146
->willReturn (123 );
147
+ $ this ->productMock ->expects ($ this ->exactly (2 ))
148
+ ->method ('getStore ' )
149
+ ->willReturn ($ this ->storeMock );
127
150
$ this ->stockStatusMock ->expects ($ this ->once ())
128
- ->method ('getHasError ' )
129
- ->willReturn (false );
151
+ ->method ('getStockStatus ' )
152
+ ->willReturn (true );
153
+ $ this ->stockStatusMock ->expects ($ this ->once ())
154
+ ->method ('getQty ' )
155
+ ->willReturn (10 );
130
156
$ this ->stockStateMock ->expects ($ this ->once ())
131
157
->method ('checkQuoteItemQty ' )
132
158
->with (123 , 2.0 , 3.0 , 1.0 , 1 )
133
159
->willReturn ($ this ->stockStatusMock );
160
+ $ this ->stockRegistryMock ->expects ($ this ->exactly (2 ))
161
+ ->method ('getStockStatus ' )
162
+ ->willReturn ($ this ->stockStatusMock );
134
163
$ this ->cartItemMock ->expects ($ this ->never ())->method ('getChildren ' );
135
164
$ result = $ this ->productStock ->isProductAvailable ($ this ->cartItemMock );
136
165
$ this ->assertTrue ($ result );
@@ -159,16 +188,22 @@ public function testIsProductAvailableForSimpleProductWithoutStock()
159
188
$ this ->storeMock ->expects ($ this ->once ())
160
189
->method ('getId ' )
161
190
->willReturn (1 );
162
- $ this ->productMock ->expects ($ this ->once ( ))
191
+ $ this ->productMock ->expects ($ this ->exactly ( 2 ))
163
192
->method ('getId ' )
164
193
->willReturn (123 );
194
+ $ this ->productMock ->expects ($ this ->once ())
195
+ ->method ('getStore ' )
196
+ ->willReturn ($ this ->storeMock );
165
197
$ this ->stockStateMock ->expects ($ this ->once ())
166
198
->method ('checkQuoteItemQty ' )
167
199
->with (123 , 2.0 , 3.0 , 1.0 , 1 )
168
200
->willReturn ($ this ->stockStatusMock );
169
201
$ this ->stockStatusMock ->expects ($ this ->once ())
170
- ->method ('getHasError ' )
171
- ->willReturn (true );
202
+ ->method ('getStockStatus ' )
203
+ ->willReturn (false );
204
+ $ this ->stockRegistryMock ->expects ($ this ->once ())
205
+ ->method ('getStockStatus ' )
206
+ ->willReturn ($ this ->stockStatusMock );
172
207
$ this ->cartItemMock ->expects ($ this ->never ())->method ('getChildren ' );
173
208
$ result = $ this ->productStock ->isProductAvailable ($ this ->cartItemMock );
174
209
$ this ->assertFalse ($ result );
@@ -179,33 +214,40 @@ public function testIsProductAvailableForSimpleProductWithoutStock()
179
214
*/
180
215
public function testIsStockAvailableBundleStockAvailable ()
181
216
{
182
- $ qtyOptionMock = $ this ->createMock (Option::class);
183
- $ qtyOptionMock ->expects ($ this ->once ())
217
+ $ this ->qtyOptionMock ->expects ($ this ->once ())
184
218
->method ('getValue ' )
185
- ->willReturn (2.0 );
186
- $ optionProductMock = $ this ->createMock (ProductInterface::class);
187
- $ qtyOptionMock ->expects ($ this ->once ())
219
+ ->willReturn (1.0 );
220
+ $ this ->qtyOptionMock ->expects ($ this ->once ())
188
221
->method ('getProduct ' )
189
- ->willReturn ($ optionProductMock );
222
+ ->willReturn ($ this -> optionProductMock );
190
223
$ this ->cartItemMock ->expects ($ this ->once ())
191
224
->method ('getQtyOptions ' )
192
- ->willReturn ([$ qtyOptionMock ]);
225
+ ->willReturn ([$ this -> qtyOptionMock ]);
193
226
$ this ->cartItemMock ->expects ($ this ->once ())
194
227
->method ('getStore ' )
195
228
->willReturn ($ this ->storeMock );
196
229
$ this ->storeMock ->expects ($ this ->once ())
197
230
->method ('getId ' )
198
231
->willReturn (1 );
199
- $ optionProductMock ->expects ($ this ->once ( ))
232
+ $ this -> optionProductMock ->expects ($ this ->exactly ( 3 ))
200
233
->method ('getId ' )
201
234
->willReturn (789 );
202
- $ this ->stockStatusMock ->expects ($ this ->once ( ))
203
- ->method ('getHasError ' )
204
- ->willReturn (false );
235
+ $ this ->optionProductMock ->expects ($ this ->exactly ( 2 ))
236
+ ->method ('getStore ' )
237
+ ->willReturn ($ this -> storeMock );
205
238
$ this ->stockStateMock ->expects ($ this ->once ())
206
239
->method ('checkQuoteItemQty ' )
207
- ->with (789 , 2.0 , 6 .0 , 1.0 , 1 )
240
+ ->with (789 , 2.0 , 3 .0 , 1.0 , 1 )
208
241
->willReturn ($ this ->stockStatusMock );
242
+ $ this ->stockStatusMock ->expects ($ this ->once ())
243
+ ->method ('getStockStatus ' )
244
+ ->willReturn (true );
245
+ $ this ->stockRegistryMock ->expects ($ this ->exactly (2 ))
246
+ ->method ('getStockStatus ' )
247
+ ->willReturn ($ this ->stockStatusMock );
248
+ $ this ->stockStatusMock ->expects ($ this ->once ())
249
+ ->method ('getQty ' )
250
+ ->willReturn (10 );
209
251
$ result = $ this ->productStock ->isStockAvailableBundle ($ this ->cartItemMock , 1 , 2.0 );
210
252
$ this ->assertTrue ($ result );
211
253
}
@@ -215,33 +257,37 @@ public function testIsStockAvailableBundleStockAvailable()
215
257
*/
216
258
public function testIsStockAvailableBundleStockNotAvailable ()
217
259
{
218
- $ qtyOptionMock = $ this ->createMock (\Magento \Quote \Model \Quote \Item \Option::class);
219
- $ qtyOptionMock ->expects ($ this ->once ())
260
+ $ this ->qtyOptionMock ->expects ($ this ->once ())
220
261
->method ('getValue ' )
221
262
->willReturn (2.0 );
222
- $ optionProductMock = $ this ->createMock (ProductInterface::class);
223
- $ qtyOptionMock ->expects ($ this ->once ())
263
+ $ this ->qtyOptionMock ->expects ($ this ->once ())
224
264
->method ('getProduct ' )
225
- ->willReturn ($ optionProductMock );
265
+ ->willReturn ($ this -> optionProductMock );
226
266
$ this ->cartItemMock ->expects ($ this ->once ())
227
267
->method ('getQtyOptions ' )
228
- ->willReturn ([$ qtyOptionMock ]);
268
+ ->willReturn ([$ this -> qtyOptionMock ]);
229
269
$ this ->cartItemMock ->expects ($ this ->once ())
230
270
->method ('getStore ' )
231
271
->willReturn ($ this ->storeMock );
232
272
$ this ->storeMock ->expects ($ this ->once ())
233
273
->method ('getId ' )
234
274
->willReturn (1 );
235
- $ this ->stockStatusMock ->expects ($ this ->once ())
236
- ->method ('getHasError ' )
237
- ->willReturn (true );
238
- $ optionProductMock ->expects ($ this ->once ())
275
+ $ this ->optionProductMock ->expects ($ this ->exactly (2 ))
239
276
->method ('getId ' )
240
277
->willReturn (789 );
278
+ $ this ->optionProductMock ->expects ($ this ->once ())
279
+ ->method ('getStore ' )
280
+ ->willReturn ($ this ->storeMock );
241
281
$ this ->stockStateMock ->expects ($ this ->once ())
242
282
->method ('checkQuoteItemQty ' )
243
283
->with (789 , 2.0 , 6.0 , 1.0 , 1 )
244
284
->willReturn ($ this ->stockStatusMock );
285
+ $ this ->stockStatusMock ->expects ($ this ->once ())
286
+ ->method ('getStockStatus ' )
287
+ ->willReturn (false );
288
+ $ this ->stockRegistryMock ->expects ($ this ->once ())
289
+ ->method ('getStockStatus ' )
290
+ ->willReturn ($ this ->stockStatusMock );
245
291
$ result = $ this ->productStock ->isStockAvailableBundle ($ this ->cartItemMock , 1 , 2.0 );
246
292
$ this ->assertFalse ($ result );
247
293
}
0 commit comments