@@ -145,7 +145,7 @@ public function testGetEmptyEntity()
145
145
$ model ->get ($ orderItemId );
146
146
}
147
147
148
- public function testGet ()
148
+ public function testGetAsParentWithChild ()
149
149
{
150
150
$ orderItemId = 1 ;
151
151
$ productType = 'configurable ' ;
@@ -154,18 +154,27 @@ public function testGet()
154
154
155
155
$ this ->getProductOptionExtensionMock ();
156
156
$ productOption = $ this ->getProductOptionMock ();
157
- $ orderItemMock = $ this ->getOrderMock ($ productType , $ productOption );
157
+ $ orderItemMock = $ this ->getOrderItemMock ($ productType , $ productOption );
158
+
159
+ $ orderItemCollectionMock = $ this ->createMock (\Magento \Sales \Model \ResourceModel \Order \Item \Collection::class);
160
+ $ orderItemCollectionMock ->expects ($ this ->once ())
161
+ ->method ('filterByParent ' )
162
+ ->with ($ orderItemId )
163
+ ->willReturnSelf ();
164
+ $ orderItemCollectionMock ->expects ($ this ->once ())
165
+ ->method ('getItems ' )
166
+ ->willReturn ([$ orderItemMock ]);
158
167
159
168
$ orderMock = $ this ->createMock (\Magento \Sales \Model \Order::class);
160
169
$ orderMock ->expects ($ this ->once ())
161
- ->method ('getAllItems ' )
162
- ->willReturn ([ $ orderItemMock ] );
170
+ ->method ('getItemsCollection ' )
171
+ ->willReturn ($ orderItemCollectionMock );
163
172
164
173
$ orderItemMock ->expects ($ this ->once ())
165
174
->method ('load ' )
166
175
->with ($ orderItemId )
167
176
->willReturn ($ orderItemMock );
168
- $ orderItemMock ->expects ($ this ->exactly (2 ))
177
+ $ orderItemMock ->expects ($ this ->exactly (3 ))
169
178
->method ('getItemId ' )
170
179
->willReturn ($ orderItemId );
171
180
$ orderItemMock ->expects ($ this ->once ())
@@ -183,6 +192,45 @@ public function testGet()
183
192
$ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
184
193
}
185
194
195
+ public function testGetAsChild ()
196
+ {
197
+ $ orderItemId = 1 ;
198
+ $ parentItemId = 66 ;
199
+ $ productType = 'configurable ' ;
200
+
201
+ $ this ->productOptionData = ['option1 ' => 'value1 ' ];
202
+
203
+ $ this ->getProductOptionExtensionMock ();
204
+ $ productOption = $ this ->getProductOptionMock ();
205
+ $ orderItemMock = $ this ->getOrderItemMock ($ productType , $ productOption );
206
+
207
+ $ orderItemMock ->expects ($ this ->once ())
208
+ ->method ('load ' )
209
+ ->with ($ orderItemId )
210
+ ->willReturn ($ orderItemMock );
211
+ $ orderItemMock ->expects ($ this ->once ())
212
+ ->method ('getItemId ' )
213
+ ->willReturn ($ orderItemId );
214
+ $ orderItemMock ->expects ($ this ->exactly (3 ))
215
+ ->method ('getParentItemId ' )
216
+ ->willReturn ($ parentItemId );
217
+
218
+ $ this ->metadata ->expects ($ this ->once ())
219
+ ->method ('getNewInstance ' )
220
+ ->willReturn ($ orderItemMock );
221
+
222
+ $ parentItemMock = $ this ->createMock (\Magento \Sales \Model \Order \Item::class);
223
+
224
+ $ model = $ this ->getModel ($ orderItemMock , $ productType );
225
+ $ reflectedRegistryProperty = new \ReflectionProperty ($ model , 'registry ' );
226
+ $ reflectedRegistryProperty ->setAccessible (true );
227
+ $ reflectedRegistryProperty ->setValue ($ model , [$ parentItemId => $ parentItemMock ]);
228
+ $ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
229
+
230
+ // Assert already registered
231
+ $ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
232
+ }
233
+
186
234
public function testGetList ()
187
235
{
188
236
$ productType = 'configurable ' ;
@@ -192,7 +240,7 @@ public function testGetList()
192
240
->getMock ();
193
241
$ this ->getProductOptionExtensionMock ();
194
242
$ productOption = $ this ->getProductOptionMock ();
195
- $ orderItemMock = $ this ->getOrderMock ($ productType , $ productOption );
243
+ $ orderItemMock = $ this ->getOrderItemMock ($ productType , $ productOption );
196
244
197
245
$ searchResultMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Item \Collection::class)
198
246
->disableOriginalConstructor ()
@@ -214,35 +262,12 @@ public function testDeleteById()
214
262
$ orderItemId = 1 ;
215
263
$ productType = 'configurable ' ;
216
264
217
- $ requestMock = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
218
- ->disableOriginalConstructor ()
219
- ->getMock ();
220
-
221
265
$ orderItemMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
222
266
->disableOriginalConstructor ()
223
267
->getMock ();
224
-
225
- $ orderMock = $ this ->createMock (\Magento \Sales \Model \Order::class);
226
- $ orderMock ->expects ($ this ->once ())
227
- ->method ('getAllItems ' )
228
- ->willReturn ([$ orderItemMock ]);
229
-
230
268
$ orderItemMock ->expects ($ this ->once ())
231
- ->method ('load ' )
232
- ->with ($ orderItemId )
233
- ->willReturn ($ orderItemMock );
234
- $ orderItemMock ->expects ($ this ->exactly (2 ))
235
- ->method ('getItemId ' )
269
+ ->method ('getEntityId ' )
236
270
->willReturn ($ orderItemId );
237
- $ orderItemMock ->expects ($ this ->once ())
238
- ->method ('getProductType ' )
239
- ->willReturn ($ productType );
240
- $ orderItemMock ->expects ($ this ->once ())
241
- ->method ('getBuyRequest ' )
242
- ->willReturn ($ requestMock );
243
- $ orderItemMock ->expects ($ this ->once ())
244
- ->method ('getOrder ' )
245
- ->willReturn ($ orderMock );
246
271
247
272
$ orderItemResourceMock = $ this ->getMockBuilder (\Magento \Framework \Model \ResourceModel \Db \AbstractDb::class)
248
273
->disableOriginalConstructor ()
@@ -252,15 +277,16 @@ public function testDeleteById()
252
277
->with ($ orderItemMock )
253
278
->willReturnSelf ();
254
279
255
- $ this ->metadata ->expects ($ this ->once ())
256
- ->method ('getNewInstance ' )
257
- ->willReturn ($ orderItemMock );
258
280
$ this ->metadata ->expects ($ this ->exactly (1 ))
259
281
->method ('getMapper ' )
260
282
->willReturn ($ orderItemResourceMock );
261
283
262
284
$ model = $ this ->getModel ($ orderItemMock , $ productType );
285
+ $ reflectedRegistryProperty = new \ReflectionProperty ($ model , 'registry ' );
286
+ $ reflectedRegistryProperty ->setAccessible (true );
287
+ $ reflectedRegistryProperty ->setValue ($ model , [$ orderItemId => $ orderItemMock ]);
263
288
$ this ->assertTrue ($ model ->deleteById ($ orderItemId ));
289
+ $ this ->assertEmpty ($ reflectedRegistryProperty ->getValue ($ model ));
264
290
}
265
291
266
292
/**
@@ -318,7 +344,7 @@ protected function getModel(
318
344
* @param \PHPUnit_Framework_MockObject_MockObject $productOption
319
345
* @return \PHPUnit_Framework_MockObject_MockObject
320
346
*/
321
- protected function getOrderMock ($ productType , $ productOption )
347
+ protected function getOrderItemMock ($ productType , $ productOption )
322
348
{
323
349
$ requestMock = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
324
350
->disableOriginalConstructor ()
0 commit comments