1212use Magento \Backend \Model \View \Result \RedirectFactory ;
1313use Magento \Framework \App \Request \Http ;
1414use Magento \Framework \AuthorizationInterface ;
15+ use Magento \Framework \Controller \Result \Json ;
16+ use Magento \Framework \Controller \Result \JsonFactory ;
1517use Magento \Framework \ObjectManagerInterface ;
1618use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1719use Magento \Sales \Api \OrderRepositoryInterface ;
@@ -77,6 +79,12 @@ class AddCommentTest extends TestCase
7779 */
7880 private $ objectManagerMock ;
7981
82+ /** @var JsonFactory|MockObject */
83+ private $ jsonFactory ;
84+
85+ /** @var Json|MockObject */
86+ private $ resultJson ;
87+
8088 /**
8189 * Test setup
8290 */
@@ -94,14 +102,22 @@ protected function setUp(): void
94102
95103 $ this ->contextMock ->expects ($ this ->once ())->method ('getRequest ' )->willReturn ($ this ->requestMock );
96104
105+ $ this ->resultJson = $ this ->getMockBuilder (Json::class)
106+ ->disableOriginalConstructor ()
107+ ->getMock ();
108+ $ this ->jsonFactory = $ this ->getMockBuilder (JsonFactory::class)
109+ ->disableOriginalConstructor ()
110+ ->getMock ();
111+
97112 $ objectManagerHelper = new ObjectManager ($ this );
98113 $ this ->addCommentController = $ objectManagerHelper ->getObject (
99114 AddComment::class,
100115 [
101116 'context ' => $ this ->contextMock ,
102117 'orderRepository ' => $ this ->orderRepositoryMock ,
103118 '_authorization ' => $ this ->authorizationMock ,
104- '_objectManager ' => $ this ->objectManagerMock
119+ '_objectManager ' => $ this ->objectManagerMock ,
120+ 'resultJsonFactory ' => $ this ->jsonFactory
105121 ]
106122 );
107123 }
@@ -205,4 +221,44 @@ public function executeWillNotifyCustomerDataProvider()
205221 ],
206222 ];
207223 }
224+
225+ /**
226+ * Assert error message for empty comment value
227+ *
228+ * @return void
229+ */
230+ public function testExecuteForEmptyCommentMessage (): void
231+ {
232+ $ orderId = 30 ;
233+ $ orderStatus = 'processing ' ;
234+ $ historyData = [
235+ 'comment ' => '' ,
236+ 'is_customer_notified ' => false ,
237+ 'status ' => 'processing '
238+ ];
239+
240+ $ this ->requestMock ->expects ($ this ->once ())->method ('getParam ' )->with ('order_id ' )->willReturn ($ orderId );
241+ $ this ->orderMock ->expects ($ this ->atLeastOnce ())->method ('getDataByKey ' )
242+ ->with ('status ' )->willReturn ($ orderStatus );
243+ $ this ->orderRepositoryMock ->expects ($ this ->once ())
244+ ->method ('get ' )
245+ ->willReturn ($ this ->orderMock );
246+ $ this ->requestMock ->expects ($ this ->once ())->method ('getPost ' )->with ('history ' )->willReturn ($ historyData );
247+
248+ $ this ->resultJson ->expects ($ this ->once ())
249+ ->method ('setData ' )
250+ ->with (
251+ [
252+ 'error ' => true ,
253+ 'message ' => 'Please provide a comment text or ' .
254+ 'update the order status to be able to submit a comment for this order. '
255+ ]
256+ )
257+ ->willReturnSelf ();
258+ $ this ->jsonFactory ->expects ($ this ->once ())
259+ ->method ('create ' )
260+ ->willReturn ($ this ->resultJson );
261+
262+ $ this ->assertSame ($ this ->resultJson , $ this ->addCommentController ->execute ());
263+ }
208264}
0 commit comments