@@ -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,7 +154,55 @@ 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 ]);
167
+
168
+ $ orderMock = $ this ->createMock (\Magento \Sales \Model \Order::class);
169
+ $ orderMock ->expects ($ this ->once ())
170
+ ->method ('getItemsCollection ' )
171
+ ->willReturn ($ orderItemCollectionMock );
172
+
173
+ $ orderItemMock ->expects ($ this ->once ())
174
+ ->method ('load ' )
175
+ ->with ($ orderItemId )
176
+ ->willReturn ($ orderItemMock );
177
+ $ orderItemMock ->expects ($ this ->exactly (3 ))
178
+ ->method ('getItemId ' )
179
+ ->willReturn ($ orderItemId );
180
+ $ orderItemMock ->expects ($ this ->once ())
181
+ ->method ('getOrder ' )
182
+ ->willReturn ($ orderMock );
183
+
184
+ $ this ->metadata ->expects ($ this ->once ())
185
+ ->method ('getNewInstance ' )
186
+ ->willReturn ($ orderItemMock );
187
+
188
+ $ model = $ this ->getModel ($ orderItemMock , $ productType );
189
+ $ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
190
+
191
+ // Assert already registered
192
+ $ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
193
+ }
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 );
158
206
159
207
$ orderItemMock ->expects ($ this ->once ())
160
208
->method ('load ' )
@@ -163,12 +211,20 @@ public function testGet()
163
211
$ orderItemMock ->expects ($ this ->once ())
164
212
->method ('getItemId ' )
165
213
->willReturn ($ orderItemId );
214
+ $ orderItemMock ->expects ($ this ->exactly (3 ))
215
+ ->method ('getParentItemId ' )
216
+ ->willReturn ($ parentItemId );
166
217
167
218
$ this ->metadata ->expects ($ this ->once ())
168
219
->method ('getNewInstance ' )
169
220
->willReturn ($ orderItemMock );
170
221
222
+ $ parentItemMock = $ this ->createMock (\Magento \Sales \Model \Order \Item::class);
223
+
171
224
$ model = $ this ->getModel ($ orderItemMock , $ productType );
225
+ $ reflectedRegistryProperty = new \ReflectionProperty ($ model , 'registry ' );
226
+ $ reflectedRegistryProperty ->setAccessible (true );
227
+ $ reflectedRegistryProperty ->setValue ($ model , [$ parentItemId => $ parentItemMock ]);
172
228
$ this ->assertSame ($ orderItemMock , $ model ->get ($ orderItemId ));
173
229
174
230
// Assert already registered
@@ -184,7 +240,7 @@ public function testGetList()
184
240
->getMock ();
185
241
$ this ->getProductOptionExtensionMock ();
186
242
$ productOption = $ this ->getProductOptionMock ();
187
- $ orderItemMock = $ this ->getOrderMock ($ productType , $ productOption );
243
+ $ orderItemMock = $ this ->getOrderItemMock ($ productType , $ productOption );
188
244
189
245
$ searchResultMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Item \Collection::class)
190
246
->disableOriginalConstructor ()
@@ -206,26 +262,12 @@ public function testDeleteById()
206
262
$ orderItemId = 1 ;
207
263
$ productType = 'configurable ' ;
208
264
209
- $ requestMock = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
210
- ->disableOriginalConstructor ()
211
- ->getMock ();
212
-
213
265
$ orderItemMock = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Item::class)
214
266
->disableOriginalConstructor ()
215
267
->getMock ();
216
268
$ orderItemMock ->expects ($ this ->once ())
217
- ->method ('load ' )
218
- ->with ($ orderItemId )
219
- ->willReturn ($ orderItemMock );
220
- $ orderItemMock ->expects ($ this ->once ())
221
- ->method ('getItemId ' )
269
+ ->method ('getEntityId ' )
222
270
->willReturn ($ orderItemId );
223
- $ orderItemMock ->expects ($ this ->once ())
224
- ->method ('getProductType ' )
225
- ->willReturn ($ productType );
226
- $ orderItemMock ->expects ($ this ->once ())
227
- ->method ('getBuyRequest ' )
228
- ->willReturn ($ requestMock );
229
271
230
272
$ orderItemResourceMock = $ this ->getMockBuilder (\Magento \Framework \Model \ResourceModel \Db \AbstractDb::class)
231
273
->disableOriginalConstructor ()
@@ -235,15 +277,16 @@ public function testDeleteById()
235
277
->with ($ orderItemMock )
236
278
->willReturnSelf ();
237
279
238
- $ this ->metadata ->expects ($ this ->once ())
239
- ->method ('getNewInstance ' )
240
- ->willReturn ($ orderItemMock );
241
280
$ this ->metadata ->expects ($ this ->exactly (1 ))
242
281
->method ('getMapper ' )
243
282
->willReturn ($ orderItemResourceMock );
244
283
245
284
$ model = $ this ->getModel ($ orderItemMock , $ productType );
285
+ $ reflectedRegistryProperty = new \ReflectionProperty ($ model , 'registry ' );
286
+ $ reflectedRegistryProperty ->setAccessible (true );
287
+ $ reflectedRegistryProperty ->setValue ($ model , [$ orderItemId => $ orderItemMock ]);
246
288
$ this ->assertTrue ($ model ->deleteById ($ orderItemId ));
289
+ $ this ->assertEmpty ($ reflectedRegistryProperty ->getValue ($ model ));
247
290
}
248
291
249
292
/**
@@ -301,7 +344,7 @@ protected function getModel(
301
344
* @param \PHPUnit_Framework_MockObject_MockObject $productOption
302
345
* @return \PHPUnit_Framework_MockObject_MockObject
303
346
*/
304
- protected function getOrderMock ($ productType , $ productOption )
347
+ protected function getOrderItemMock ($ productType , $ productOption )
305
348
{
306
349
$ requestMock = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
307
350
->disableOriginalConstructor ()
0 commit comments