12
12
use Magento \Sales \Api \Data \OrderInterface ;
13
13
use Magento \Sales \Model \Order ;
14
14
use Magento \Sales \Model \ResourceModel \Order \Status \History \CollectionFactory as HistoryCollectionFactory ;
15
+ use Magento \Sales \Api \OrderItemRepositoryInterface ;
16
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
17
+ use Magento \Framework \Api \SearchCriteria ;
18
+ use Magento \Sales \Api \Data \OrderItemSearchResultInterface ;
15
19
16
20
/**
17
21
* Test class for \Magento\Sales\Model\Order
@@ -87,6 +91,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
87
91
*/
88
92
private $ timezone ;
89
93
94
+ /**
95
+ * @var OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
96
+ */
97
+ private $ itemRepository ;
98
+
99
+ /**
100
+ * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
101
+ */
102
+ private $ searchCriteriaBuilder ;
103
+
90
104
protected function setUp ()
91
105
{
92
106
$ helper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -144,6 +158,15 @@ protected function setUp()
144
158
$ this ->eventManager = $ this ->createMock (\Magento \Framework \Event \Manager::class);
145
159
$ context = $ this ->createPartialMock (\Magento \Framework \Model \Context::class, ['getEventDispatcher ' ]);
146
160
$ context ->expects ($ this ->any ())->method ('getEventDispatcher ' )->willReturn ($ this ->eventManager );
161
+
162
+ $ this ->itemRepository = $ this ->getMockBuilder (OrderItemRepositoryInterface::class)
163
+ ->setMethods (['getList ' ])
164
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
165
+
166
+ $ this ->searchCriteriaBuilder = $ this ->getMockBuilder (SearchCriteriaBuilder::class)
167
+ ->setMethods (['addFilter ' , 'create ' ])
168
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
169
+
147
170
$ this ->order = $ helper ->getObject (
148
171
\Magento \Sales \Model \Order::class,
149
172
[
@@ -157,37 +180,80 @@ protected function setUp()
157
180
'productListFactory ' => $ this ->productCollectionFactoryMock ,
158
181
'localeResolver ' => $ this ->localeResolver ,
159
182
'timezone ' => $ this ->timezone ,
183
+ 'itemRepository ' => $ this ->itemRepository ,
184
+ 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilder
160
185
]
161
186
);
162
187
}
163
188
164
- public function testGetItemById ()
189
+ /**
190
+ * Test testGetItems method.
191
+ */
192
+ public function testGetItems ()
165
193
{
166
- $ realOrderItemId = 1 ;
167
- $ fakeOrderItemId = 2 ;
194
+ $ orderItems = [$ this ->item ];
195
+
196
+ $ this ->searchCriteriaBuilder ->expects ($ this ->once ())->method ('addFilter ' )->willReturnSelf ();
197
+
198
+ $ searchCriteria = $ this ->getMockBuilder (SearchCriteria::class)
199
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
200
+ $ this ->searchCriteriaBuilder ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchCriteria );
168
201
169
- $ orderItem = $ this ->createMock (\Magento \Sales \Model \Order \Item::class);
202
+ $ itemsCollection = $ this ->getMockBuilder (OrderItemSearchResultInterface::class)
203
+ ->setMethods (['getItems ' ])
204
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
205
+ $ itemsCollection ->expects ($ this ->once ())->method ('getItems ' )->willReturn ($ orderItems );
206
+ $ this ->itemRepository ->expects ($ this ->once ())->method ('getList ' )->willReturn ($ itemsCollection );
170
207
208
+ $ this ->assertEquals ($ orderItems , $ this ->order ->getItems ());
209
+ }
210
+
211
+ /**
212
+ * Prepare order item mock.
213
+ *
214
+ * @param int $orderId
215
+ * @return void
216
+ */
217
+ private function prepareOrderItem (int $ orderId = 0 )
218
+ {
171
219
$ this ->order ->setData (
172
220
\Magento \Sales \Api \Data \OrderInterface::ITEMS ,
173
221
[
174
- $ realOrderItemId => $ orderItem
222
+ $ orderId => $ this -> item
175
223
]
176
224
);
225
+ }
177
226
178
- $ this ->assertEquals ($ orderItem , $ this ->order ->getItemById ($ realOrderItemId ));
227
+ /**
228
+ * Test GetItemById method.
229
+ *
230
+ * @return void
231
+ */
232
+ public function testGetItemById ()
233
+ {
234
+ $ realOrderItemId = 1 ;
235
+ $ fakeOrderItemId = 2 ;
236
+
237
+ $ this ->prepareOrderItem ($ realOrderItemId );
238
+
239
+ $ this ->assertEquals ($ this ->item , $ this ->order ->getItemById ($ realOrderItemId ));
179
240
$ this ->assertEquals (null , $ this ->order ->getItemById ($ fakeOrderItemId ));
180
241
}
181
242
182
243
/**
244
+ * Test GetItemByQuoteItemId method.
245
+ *
183
246
* @param int|null $gettingQuoteItemId
184
247
* @param int|null $quoteItemId
185
248
* @param string|null $result
186
249
*
187
250
* @dataProvider dataProviderGetItemByQuoteItemId
251
+ * @return void
188
252
*/
189
253
public function testGetItemByQuoteItemId ($ gettingQuoteItemId , $ quoteItemId , $ result )
190
254
{
255
+ $ this ->prepareOrderItem ();
256
+
191
257
$ this ->item ->expects ($ this ->any ())
192
258
->method ('getQuoteItemId ' )
193
259
->willReturn ($ gettingQuoteItemId );
@@ -212,14 +278,19 @@ public function dataProviderGetItemByQuoteItemId()
212
278
}
213
279
214
280
/**
281
+ * Test getAllVisibleItems method.
282
+ *
215
283
* @param bool $isDeleted
216
284
* @param int|null $parentItemId
217
285
* @param array $result
218
286
*
219
287
* @dataProvider dataProviderGetAllVisibleItems
288
+ * @return void
220
289
*/
221
290
public function testGetAllVisibleItems ($ isDeleted , $ parentItemId , array $ result )
222
291
{
292
+ $ this ->prepareOrderItem ();
293
+
223
294
$ this ->item ->expects ($ this ->once ())
224
295
->method ('isDeleted ' )
225
296
->willReturn ($ isDeleted );
@@ -263,8 +334,15 @@ public function testCanCancelIsPaymentReview()
263
334
$ this ->assertFalse ($ this ->order ->canCancel ());
264
335
}
265
336
337
+ /**
338
+ * Test CanInvoice method.
339
+ *
340
+ * @return void
341
+ */
266
342
public function testCanInvoice ()
267
343
{
344
+ $ this ->prepareOrderItem ();
345
+
268
346
$ this ->item ->expects ($ this ->any ())
269
347
->method ('getQtyToInvoice ' )
270
348
->willReturn (42 );
@@ -304,8 +382,15 @@ public function testCanNotInvoiceWhenActionInvoiceFlagIsFalse()
304
382
$ this ->assertFalse ($ this ->order ->canInvoice ());
305
383
}
306
384
385
+ /**
386
+ * Test CanNotInvoice method when invoice is locked.
387
+ *
388
+ * @return void
389
+ */
307
390
public function testCanNotInvoiceWhenLockedInvoice ()
308
391
{
392
+ $ this ->prepareOrderItem ();
393
+
309
394
$ this ->item ->expects ($ this ->any ())
310
395
->method ('getQtyToInvoice ' )
311
396
->willReturn (42 );
@@ -315,8 +400,15 @@ public function testCanNotInvoiceWhenLockedInvoice()
315
400
$ this ->assertFalse ($ this ->order ->canInvoice ());
316
401
}
317
402
403
+ /**
404
+ * Test CanNotInvoice method when didn't have qty to invoice.
405
+ *
406
+ * @return void
407
+ */
318
408
public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice ()
319
409
{
410
+ $ this ->prepareOrderItem ();
411
+
320
412
$ this ->item ->expects ($ this ->any ())
321
413
->method ('getQtyToInvoice ' )
322
414
->willReturn (0 );
@@ -601,8 +693,15 @@ public function testCanCancelCanReviewPayment()
601
693
$ this ->assertFalse ($ this ->order ->canCancel ());
602
694
}
603
695
696
+ /**
697
+ * Test CanCancelAllInvoiced method.
698
+ *
699
+ * @return void
700
+ */
604
701
public function testCanCancelAllInvoiced ()
605
702
{
703
+ $ this ->prepareOrderItem ();
704
+
606
705
$ paymentMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Payment::class)
607
706
->disableOriginalConstructor ()
608
707
->setMethods (['isDeleted ' , 'canReviewPayment ' , 'canFetchTransactionInfo ' , '__wakeUp ' ])
@@ -662,11 +761,16 @@ public function testCanCancelState()
662
761
}
663
762
664
763
/**
764
+ * Test CanCancelActionFlag method.
765
+ *
665
766
* @param bool $cancelActionFlag
666
767
* @dataProvider dataProviderActionFlag
768
+ * @return void
667
769
*/
668
770
public function testCanCancelActionFlag ($ cancelActionFlag )
669
771
{
772
+ $ this ->prepareOrderItem ();
773
+
670
774
$ paymentMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Payment::class)
671
775
->disableOriginalConstructor ()
672
776
->setMethods (['isDeleted ' , 'canReviewPayment ' , 'canFetchTransactionInfo ' , '__wakeUp ' ])
0 commit comments