12
12
use Magento \Backend \Model \View \Result \RedirectFactory ;
13
13
use Magento \Framework \App \Request \Http ;
14
14
use Magento \Framework \AuthorizationInterface ;
15
+ use Magento \Framework \Controller \Result \Json ;
16
+ use Magento \Framework \Controller \Result \JsonFactory ;
15
17
use Magento \Framework \ObjectManagerInterface ;
16
18
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
19
use Magento \Sales \Api \OrderRepositoryInterface ;
@@ -77,6 +79,12 @@ class AddCommentTest extends TestCase
77
79
*/
78
80
private $ objectManagerMock ;
79
81
82
+ /** @var JsonFactory|MockObject */
83
+ private $ jsonFactory ;
84
+
85
+ /** @var Json|MockObject */
86
+ private $ resultJson ;
87
+
80
88
/**
81
89
* Test setup
82
90
*/
@@ -94,14 +102,22 @@ protected function setUp(): void
94
102
95
103
$ this ->contextMock ->expects ($ this ->once ())->method ('getRequest ' )->willReturn ($ this ->requestMock );
96
104
105
+ $ this ->resultJson = $ this ->getMockBuilder (Json::class)
106
+ ->disableOriginalConstructor ()
107
+ ->getMock ();
108
+ $ this ->jsonFactory = $ this ->getMockBuilder (JsonFactory::class)
109
+ ->disableOriginalConstructor ()
110
+ ->getMock ();
111
+
97
112
$ objectManagerHelper = new ObjectManager ($ this );
98
113
$ this ->addCommentController = $ objectManagerHelper ->getObject (
99
114
AddComment::class,
100
115
[
101
116
'context ' => $ this ->contextMock ,
102
117
'orderRepository ' => $ this ->orderRepositoryMock ,
103
118
'_authorization ' => $ this ->authorizationMock ,
104
- '_objectManager ' => $ this ->objectManagerMock
119
+ '_objectManager ' => $ this ->objectManagerMock ,
120
+ 'resultJsonFactory ' => $ this ->jsonFactory
105
121
]
106
122
);
107
123
}
@@ -205,4 +221,44 @@ public function executeWillNotifyCustomerDataProvider()
205
221
],
206
222
];
207
223
}
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
+ }
208
264
}
0 commit comments