20
20
use Magento \Sales \Api \OrderStatusHistoryRepositoryInterface ;
21
21
use Magento \Sales \Api \PaymentFailuresInterface ;
22
22
use Magento \Sales \Model \Order ;
23
+ use Magento \Sales \Model \Order \Config ;
23
24
use Magento \Sales \Model \Order \Email \Sender \OrderCommentSender ;
24
25
use Magento \Sales \Model \Order \Status \History ;
25
26
use Magento \Sales \Model \OrderMutex ;
28
29
use PHPUnit \Framework \MockObject \MockObject ;
29
30
use PHPUnit \Framework \TestCase ;
30
31
use Psr \Log \LoggerInterface ;
31
- use Magento \Framework \Phrase ;
32
32
use Magento \Framework \Exception \LocalizedException ;
33
33
34
34
/**
35
35
*
36
36
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
+ * @SuppressWarnings(PHPMD.TooManyFields)
37
38
*/
38
39
class OrderServiceTest extends TestCase
39
40
{
@@ -112,6 +113,11 @@ class OrderServiceTest extends TestCase
112
113
*/
113
114
private $ resourceConnectionMock ;
114
115
116
+ /**
117
+ * @var MockObject|Config
118
+ */
119
+ private $ orderConfigMock ;
120
+
115
121
protected function setUp (): void
116
122
{
117
123
$ this ->orderRepositoryMock = $ this ->getMockBuilder (
@@ -189,6 +195,10 @@ protected function setUp(): void
189
195
->disableOriginalConstructor ()
190
196
->getMock ();
191
197
198
+ $ this ->orderConfigMock = $ this ->getMockBuilder (Config::class)
199
+ ->disableOriginalConstructor ()
200
+ ->getMock ();
201
+
192
202
$ this ->orderService = new OrderService (
193
203
$ this ->orderRepositoryMock ,
194
204
$ this ->orderStatusHistoryRepositoryMock ,
@@ -199,7 +209,8 @@ protected function setUp(): void
199
209
$ this ->orderCommentSender ,
200
210
$ paymentFailures ,
201
211
$ logger ,
202
- new OrderMutex ($ this ->resourceConnectionMock )
212
+ new OrderMutex ($ this ->resourceConnectionMock ),
213
+ $ this ->orderConfigMock
203
214
);
204
215
}
205
216
@@ -276,11 +287,15 @@ public function testGetCommentsList()
276
287
277
288
public function testAddComment ()
278
289
{
290
+ $ orderId = 123 ;
279
291
$ clearComment = "Comment text here... " ;
280
- $ this ->orderRepositoryMock ->expects ($ this ->once ())
281
- ->method ('get ' )
282
- ->with (123 )
283
- ->willReturn ($ this ->orderMock );
292
+ $ this ->mockCommentStatuses ($ orderId , Order::STATUS_FRAUD );
293
+ $ this ->orderMock ->expects ($ this ->once ())
294
+ ->method ('setStatus ' )
295
+ ->willReturn (Order::STATUS_FRAUD );
296
+ $ this ->orderStatusHistoryMock ->expects ($ this ->once ())
297
+ ->method ('setStatus ' )
298
+ ->willReturn (Order::STATUS_FRAUD );
284
299
$ this ->orderMock ->expects ($ this ->once ())
285
300
->method ('addStatusHistory ' )
286
301
->with ($ this ->orderStatusHistoryMock )
@@ -294,23 +309,52 @@ public function testAddComment()
294
309
$ this ->orderCommentSender ->expects ($ this ->once ())
295
310
->method ('send ' )
296
311
->with ($ this ->orderMock , false , $ clearComment );
297
- $ this ->assertTrue ($ this ->orderService ->addComment (123 , $ this ->orderStatusHistoryMock ));
312
+ $ this ->assertTrue ($ this ->orderService ->addComment ($ orderId , $ this ->orderStatusHistoryMock ));
298
313
}
299
314
300
315
/**
301
316
* test for add comment with order status change case
302
317
*/
303
318
public function testAddCommentWithStatus ()
304
319
{
305
- $ params = [ ' status ' => ' holded ' ] ;
306
- $ inputException = new LocalizedException (
307
- new Phrase ( 'Unable to add comment: The status "%1" is not part of the order
308
- status history. ' , $ params )
320
+ $ orderId = 123 ;
321
+ $ inputException = __ (
322
+ 'Unable to add comment: The status "%1" is not part of the order status history. ' ,
323
+ Order:: STATE_NEW
309
324
);
310
- $ this ->orderStatusHistoryMock ->method ('getStatus ' )
311
- ->willThrowException ($ inputException );
325
+ $ this ->mockCommentStatuses ($ orderId , Order::STATE_NEW );
312
326
$ this ->expectException (LocalizedException::class);
313
- $ this ->orderService ->addComment (123 , $ this ->orderStatusHistoryMock );
327
+ $ this ->expectExceptionMessage ((string )$ inputException );
328
+ $ this ->orderService ->addComment ($ orderId , $ this ->orderStatusHistoryMock );
329
+ }
330
+
331
+ /**
332
+ * @param $orderId
333
+ * @param $orderStatusHistory
334
+ */
335
+ private function mockCommentStatuses ($ orderId , $ orderStatusHistory ): void
336
+ {
337
+ $ this ->orderRepositoryMock ->expects ($ this ->once ())
338
+ ->method ('get ' )
339
+ ->with ($ orderId )
340
+ ->willReturn ($ this ->orderMock );
341
+ $ this ->orderMock ->expects ($ this ->once ())
342
+ ->method ('getState ' )
343
+ ->willReturn (Order::STATE_PROCESSING );
344
+ $ this ->orderConfigMock ->expects ($ this ->once ())
345
+ ->method ('getStateStatuses ' )
346
+ ->with (Order::STATE_PROCESSING )
347
+ ->willReturn ([
348
+ Order::STATE_PROCESSING => 'Processing ' ,
349
+ Order::STATUS_FRAUD => 'Suspected Fraud ' ,
350
+ 'test ' => 'Tests '
351
+ ]);
352
+ $ this ->orderMock ->expects ($ this ->once ())
353
+ ->method ('getStatus ' )
354
+ ->willReturn (Order::STATE_PROCESSING );
355
+ $ this ->orderStatusHistoryMock ->expects ($ this ->once ())
356
+ ->method ('getStatus ' )
357
+ ->willReturn ($ orderStatusHistory );
314
358
}
315
359
316
360
public function testNotify ()
0 commit comments