@@ -70,6 +70,16 @@ class ProductStockTest extends TestCase
7070 */
7171 private $ stockStatusMock ;
7272
73+ /**
74+ * @var ProductInterface|MockObject
75+ */
76+ private $ optionProductMock ;
77+
78+ /**
79+ * @var Option|MockObject
80+ */
81+ private $ qtyOptionMock ;
82+
7383 /**
7484 * Set up mocks and initialize the ProductStock class
7585 */
@@ -87,15 +97,25 @@ protected function setUp(): void
8797 );
8898 $ this ->stockStatusMock = $ this ->getMockBuilder (StockStatusInterface::class)
8999 ->disableOriginalConstructor ()
90- ->addMethods (['getHasError ' ])
100+ ->onlyMethods (['getQty ' , ' getStockStatus ' ])
91101 ->getMockForAbstractClass ();
92102 $ this ->cartItemMock = $ this ->getMockBuilder (Item::class)
93103 ->addMethods (['getQtyToAdd ' , 'getPreviousQty ' ])
94104 ->onlyMethods (['getStore ' , 'getProductType ' , 'getProduct ' , 'getChildren ' , 'getQtyOptions ' ])
95105 ->disableOriginalConstructor ()
96106 ->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 ();
98117 $ this ->storeMock = $ this ->createMock (StoreInterface::class);
118+ $ this ->qtyOptionMock = $ this ->createMock (Option::class);
99119 }
100120
101121 /**
@@ -121,16 +141,25 @@ public function testIsProductAvailableForSimpleProductWithStock(): void
121141 $ this ->storeMock ->expects ($ this ->once ())
122142 ->method ('getId ' )
123143 ->willReturn (1 );
124- $ this ->productMock ->expects ($ this ->once ( ))
144+ $ this ->productMock ->expects ($ this ->exactly ( 3 ))
125145 ->method ('getId ' )
126146 ->willReturn (123 );
147+ $ this ->productMock ->expects ($ this ->exactly (2 ))
148+ ->method ('getStore ' )
149+ ->willReturn ($ this ->storeMock );
127150 $ 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 );
130156 $ this ->stockStateMock ->expects ($ this ->once ())
131157 ->method ('checkQuoteItemQty ' )
132158 ->with (123 , 2.0 , 3.0 , 1.0 , 1 )
133159 ->willReturn ($ this ->stockStatusMock );
160+ $ this ->stockRegistryMock ->expects ($ this ->exactly (2 ))
161+ ->method ('getStockStatus ' )
162+ ->willReturn ($ this ->stockStatusMock );
134163 $ this ->cartItemMock ->expects ($ this ->never ())->method ('getChildren ' );
135164 $ result = $ this ->productStock ->isProductAvailable ($ this ->cartItemMock );
136165 $ this ->assertTrue ($ result );
@@ -159,16 +188,22 @@ public function testIsProductAvailableForSimpleProductWithoutStock()
159188 $ this ->storeMock ->expects ($ this ->once ())
160189 ->method ('getId ' )
161190 ->willReturn (1 );
162- $ this ->productMock ->expects ($ this ->once ( ))
191+ $ this ->productMock ->expects ($ this ->exactly ( 2 ))
163192 ->method ('getId ' )
164193 ->willReturn (123 );
194+ $ this ->productMock ->expects ($ this ->once ())
195+ ->method ('getStore ' )
196+ ->willReturn ($ this ->storeMock );
165197 $ this ->stockStateMock ->expects ($ this ->once ())
166198 ->method ('checkQuoteItemQty ' )
167199 ->with (123 , 2.0 , 3.0 , 1.0 , 1 )
168200 ->willReturn ($ this ->stockStatusMock );
169201 $ 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 );
172207 $ this ->cartItemMock ->expects ($ this ->never ())->method ('getChildren ' );
173208 $ result = $ this ->productStock ->isProductAvailable ($ this ->cartItemMock );
174209 $ this ->assertFalse ($ result );
@@ -179,33 +214,40 @@ public function testIsProductAvailableForSimpleProductWithoutStock()
179214 */
180215 public function testIsStockAvailableBundleStockAvailable ()
181216 {
182- $ qtyOptionMock = $ this ->createMock (Option::class);
183- $ qtyOptionMock ->expects ($ this ->once ())
217+ $ this ->qtyOptionMock ->expects ($ this ->once ())
184218 ->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 ())
188221 ->method ('getProduct ' )
189- ->willReturn ($ optionProductMock );
222+ ->willReturn ($ this -> optionProductMock );
190223 $ this ->cartItemMock ->expects ($ this ->once ())
191224 ->method ('getQtyOptions ' )
192- ->willReturn ([$ qtyOptionMock ]);
225+ ->willReturn ([$ this -> qtyOptionMock ]);
193226 $ this ->cartItemMock ->expects ($ this ->once ())
194227 ->method ('getStore ' )
195228 ->willReturn ($ this ->storeMock );
196229 $ this ->storeMock ->expects ($ this ->once ())
197230 ->method ('getId ' )
198231 ->willReturn (1 );
199- $ optionProductMock ->expects ($ this ->once ( ))
232+ $ this -> optionProductMock ->expects ($ this ->exactly ( 3 ))
200233 ->method ('getId ' )
201234 ->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 );
205238 $ this ->stockStateMock ->expects ($ this ->once ())
206239 ->method ('checkQuoteItemQty ' )
207- ->with (789 , 2.0 , 6 .0 , 1.0 , 1 )
240+ ->with (789 , 2.0 , 3 .0 , 1.0 , 1 )
208241 ->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 );
209251 $ result = $ this ->productStock ->isStockAvailableBundle ($ this ->cartItemMock , 1 , 2.0 );
210252 $ this ->assertTrue ($ result );
211253 }
@@ -215,33 +257,37 @@ public function testIsStockAvailableBundleStockAvailable()
215257 */
216258 public function testIsStockAvailableBundleStockNotAvailable ()
217259 {
218- $ qtyOptionMock = $ this ->createMock (\Magento \Quote \Model \Quote \Item \Option::class);
219- $ qtyOptionMock ->expects ($ this ->once ())
260+ $ this ->qtyOptionMock ->expects ($ this ->once ())
220261 ->method ('getValue ' )
221262 ->willReturn (2.0 );
222- $ optionProductMock = $ this ->createMock (ProductInterface::class);
223- $ qtyOptionMock ->expects ($ this ->once ())
263+ $ this ->qtyOptionMock ->expects ($ this ->once ())
224264 ->method ('getProduct ' )
225- ->willReturn ($ optionProductMock );
265+ ->willReturn ($ this -> optionProductMock );
226266 $ this ->cartItemMock ->expects ($ this ->once ())
227267 ->method ('getQtyOptions ' )
228- ->willReturn ([$ qtyOptionMock ]);
268+ ->willReturn ([$ this -> qtyOptionMock ]);
229269 $ this ->cartItemMock ->expects ($ this ->once ())
230270 ->method ('getStore ' )
231271 ->willReturn ($ this ->storeMock );
232272 $ this ->storeMock ->expects ($ this ->once ())
233273 ->method ('getId ' )
234274 ->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 ))
239276 ->method ('getId ' )
240277 ->willReturn (789 );
278+ $ this ->optionProductMock ->expects ($ this ->once ())
279+ ->method ('getStore ' )
280+ ->willReturn ($ this ->storeMock );
241281 $ this ->stockStateMock ->expects ($ this ->once ())
242282 ->method ('checkQuoteItemQty ' )
243283 ->with (789 , 2.0 , 6.0 , 1.0 , 1 )
244284 ->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 );
245291 $ result = $ this ->productStock ->isStockAvailableBundle ($ this ->cartItemMock , 1 , 2.0 );
246292 $ this ->assertFalse ($ result );
247293 }
0 commit comments