2020use Magento \Sales \Api \OrderStatusHistoryRepositoryInterface ;
2121use Magento \Sales \Api \PaymentFailuresInterface ;
2222use Magento \Sales \Model \Order ;
23+ use Magento \Sales \Model \Order \Config ;
2324use Magento \Sales \Model \Order \Email \Sender \OrderCommentSender ;
2425use Magento \Sales \Model \Order \Status \History ;
2526use Magento \Sales \Model \OrderMutex ;
2829use PHPUnit \Framework \MockObject \MockObject ;
2930use PHPUnit \Framework \TestCase ;
3031use Psr \Log \LoggerInterface ;
31- use Magento \Framework \Phrase ;
3232use Magento \Framework \Exception \LocalizedException ;
3333
3434/**
3535 *
3636 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37+ * @SuppressWarnings(PHPMD.TooManyFields)
3738 */
3839class OrderServiceTest extends TestCase
3940{
@@ -112,6 +113,11 @@ class OrderServiceTest extends TestCase
112113 */
113114 private $ resourceConnectionMock ;
114115
116+ /**
117+ * @var MockObject|Config
118+ */
119+ private $ orderConfigMock ;
120+
115121 protected function setUp (): void
116122 {
117123 $ this ->orderRepositoryMock = $ this ->getMockBuilder (
@@ -189,6 +195,10 @@ protected function setUp(): void
189195 ->disableOriginalConstructor ()
190196 ->getMock ();
191197
198+ $ this ->orderConfigMock = $ this ->getMockBuilder (Config::class)
199+ ->disableOriginalConstructor ()
200+ ->getMock ();
201+
192202 $ this ->orderService = new OrderService (
193203 $ this ->orderRepositoryMock ,
194204 $ this ->orderStatusHistoryRepositoryMock ,
@@ -199,7 +209,8 @@ protected function setUp(): void
199209 $ this ->orderCommentSender ,
200210 $ paymentFailures ,
201211 $ logger ,
202- new OrderMutex ($ this ->resourceConnectionMock )
212+ new OrderMutex ($ this ->resourceConnectionMock ),
213+ $ this ->orderConfigMock
203214 );
204215 }
205216
@@ -276,11 +287,15 @@ public function testGetCommentsList()
276287
277288 public function testAddComment ()
278289 {
290+ $ orderId = 123 ;
279291 $ 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 );
284299 $ this ->orderMock ->expects ($ this ->once ())
285300 ->method ('addStatusHistory ' )
286301 ->with ($ this ->orderStatusHistoryMock )
@@ -294,23 +309,52 @@ public function testAddComment()
294309 $ this ->orderCommentSender ->expects ($ this ->once ())
295310 ->method ('send ' )
296311 ->with ($ this ->orderMock , false , $ clearComment );
297- $ this ->assertTrue ($ this ->orderService ->addComment (123 , $ this ->orderStatusHistoryMock ));
312+ $ this ->assertTrue ($ this ->orderService ->addComment ($ orderId , $ this ->orderStatusHistoryMock ));
298313 }
299314
300315 /**
301316 * test for add comment with order status change case
302317 */
303318 public function testAddCommentWithStatus ()
304319 {
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
309324 );
310- $ this ->orderStatusHistoryMock ->method ('getStatus ' )
311- ->willThrowException ($ inputException );
325+ $ this ->mockCommentStatuses ($ orderId , Order::STATE_NEW );
312326 $ 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 );
314358 }
315359
316360 public function testNotify ()
0 commit comments