13
13
14
14
use Symfony \Component \HttpClient \MockHttpClient ;
15
15
use Symfony \Component \Notifier \Bridge \Slack \SlackOptions ;
16
+ use Symfony \Component \Notifier \Bridge \Slack \SlackSentMessage ;
16
17
use Symfony \Component \Notifier \Bridge \Slack \SlackTransport ;
17
18
use Symfony \Component \Notifier \Exception \InvalidArgumentException ;
18
19
use Symfony \Component \Notifier \Exception \LogicException ;
@@ -115,7 +116,7 @@ public function testSendWithOptions()
115
116
116
117
$ response ->expects ($ this ->once ())
117
118
->method ('getContent ' )
118
- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' ]));
119
+ ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , ' channel ' => ' C123456 ' ]));
119
120
120
121
$ expectedBody = json_encode (['channel ' => $ channel , 'text ' => $ message ]);
121
122
@@ -130,6 +131,8 @@ public function testSendWithOptions()
130
131
$ sentMessage = $ transport ->send (new ChatMessage ('testMessage ' ));
131
132
132
133
$ this ->assertSame ('1503435956.000247 ' , $ sentMessage ->getMessageId ());
134
+ $ this ->assertInstanceOf (SlackSentMessage::class, $ sentMessage );
135
+ $ this ->assertSame ('C123456 ' , $ sentMessage ->getChannelId ());
133
136
}
134
137
135
138
public function testSendWithNotification ()
@@ -145,7 +148,7 @@ public function testSendWithNotification()
145
148
146
149
$ response ->expects ($ this ->once ())
147
150
->method ('getContent ' )
148
- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' ]));
151
+ ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , ' channel ' => ' C123456 ' ]));
149
152
150
153
$ notification = new Notification ($ message );
151
154
$ chatMessage = ChatMessage::fromNotification ($ notification );
@@ -223,7 +226,7 @@ public function testSendIncludesContentTypeWithCharset()
223
226
224
227
$ response ->expects ($ this ->once ())
225
228
->method ('getContent ' )
226
- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' ]));
229
+ ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , ' channel ' => ' C123456 ' ]));
227
230
228
231
$ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response ): ResponseInterface {
229
232
$ this ->assertContains ('Content-Type: application/json; charset=utf-8 ' , $ options ['headers ' ]);
@@ -263,4 +266,39 @@ public function testSendWithErrorsIncluded()
263
266
264
267
$ transport ->send (new ChatMessage ('testMessage ' ));
265
268
}
269
+
270
+ public function testUpdateMessage ()
271
+ {
272
+ $ response = $ this ->createMock (ResponseInterface::class);
273
+
274
+ $ response ->expects ($ this ->exactly (2 ))
275
+ ->method ('getStatusCode ' )
276
+ ->willReturn (200 );
277
+
278
+ $ response ->expects ($ this ->once ())
279
+ ->method ('getContent ' )
280
+ ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
281
+
282
+ $ sentMessage = new SlackSentMessage (new ChatMessage ('Hello ' ), 'slack ' , 'C123456 ' , '1503435956.000247 ' );
283
+ $ chatMessage = $ sentMessage ->getUpdateMessage ('Hello World ' );
284
+
285
+ $ expectedBody = json_encode ([
286
+ 'channel ' => 'C123456 ' ,
287
+ 'ts ' => '1503435956.000247 ' ,
288
+ 'text ' => 'Hello World ' ,
289
+ ]);
290
+
291
+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
292
+ $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
293
+ $ this ->assertStringEndsWith ('chat.update ' , $ url );
294
+
295
+ return $ response ;
296
+ });
297
+
298
+ $ transport = $ this ->createTransport ($ client , 'another-channel ' );
299
+
300
+ $ sentMessage = $ transport ->send ($ chatMessage );
301
+
302
+ $ this ->assertSame ('1503435956.000247 ' , $ sentMessage ->getMessageId ());
303
+ }
266
304
}