@@ -29,17 +29,17 @@ final class SlackTransportTest extends TestCase
2929 public function testToStringContainsProperties (): void
3030 {
3131 $ host = 'testHost ' ;
32- $ path = 'testPath ' ;
32+ $ channel = 'test Channel ' ; // invalid channel name to test url encoding of the channel
3333
34- $ transport = new SlackTransport ($ path , $ this ->createMock (HttpClientInterface::class));
34+ $ transport = new SlackTransport (' testToken ' , $ channel , $ this ->createMock (HttpClientInterface::class));
3535 $ transport ->setHost ('testHost ' );
3636
37- $ this ->assertSame (sprintf ('slack://%s/ %s ' , $ host , $ path ), (string ) $ transport );
37+ $ this ->assertSame (sprintf ('slack://%s?channel= %s ' , $ host , urlencode ( $ channel ) ), (string ) $ transport );
3838 }
3939
4040 public function testSupportsChatMessage (): void
4141 {
42- $ transport = new SlackTransport ('testPath ' , $ this ->createMock (HttpClientInterface::class));
42+ $ transport = new SlackTransport ('testToken ' , ' testChannel ' , $ this ->createMock (HttpClientInterface::class));
4343
4444 $ this ->assertTrue ($ transport ->supports (new ChatMessage ('testChatMessage ' )));
4545 $ this ->assertFalse ($ transport ->supports ($ this ->createMock (MessageInterface::class)));
@@ -49,7 +49,7 @@ public function testSendNonChatMessageThrows(): void
4949 {
5050 $ this ->expectException (LogicException::class);
5151
52- $ transport = new SlackTransport ('testPath ' , $ this ->createMock (HttpClientInterface::class));
52+ $ transport = new SlackTransport ('testToken ' , ' testChannel ' , $ this ->createMock (HttpClientInterface::class));
5353
5454 $ transport ->send ($ this ->createMock (MessageInterface::class));
5555 }
@@ -70,15 +70,15 @@ public function testSendWithEmptyArrayResponseThrows(): void
7070 return $ response ;
7171 });
7272
73- $ transport = new SlackTransport ('testPath ' , $ client );
73+ $ transport = new SlackTransport ('testToken ' , ' testChannel ' , $ client );
7474
7575 $ transport ->send (new ChatMessage ('testMessage ' ));
7676 }
7777
7878 public function testSendWithErrorResponseThrows (): void
7979 {
8080 $ this ->expectException (TransportException::class);
81- $ this ->expectExceptionMessage ( ' testErrorCode ' );
81+ $ this ->expectExceptionMessageRegExp ( ' / testErrorCode/ ' );
8282
8383 $ response = $ this ->createMock (ResponseInterface::class);
8484 $ response ->expects ($ this ->exactly (2 ))
@@ -87,20 +87,21 @@ public function testSendWithErrorResponseThrows(): void
8787
8888 $ response ->expects ($ this ->once ())
8989 ->method ('getContent ' )
90- ->willReturn (' testErrorCode ' );
90+ ->willReturn (json_encode ([ ' error ' => ' testErrorCode ']) );
9191
9292 $ client = new MockHttpClient (static function () use ($ response ): ResponseInterface {
9393 return $ response ;
9494 });
9595
96- $ transport = new SlackTransport ('testPath ' , $ client );
96+ $ transport = new SlackTransport ('testToken ' , ' testChannel ' , $ client );
9797
9898 $ transport ->send (new ChatMessage ('testMessage ' ));
9999 }
100100
101101 public function testSendWithOptions (): void
102102 {
103- $ path = 'testPath ' ;
103+ $ token = 'testToken ' ;
104+ $ channel = 'testChannel ' ;
104105 $ message = 'testMessage ' ;
105106
106107 $ response = $ this ->createMock (ResponseInterface::class);
@@ -111,24 +112,25 @@ public function testSendWithOptions(): void
111112
112113 $ response ->expects ($ this ->once ())
113114 ->method ('getContent ' )
114- ->willReturn ('ok ' );
115+ ->willReturn (json_encode ([ 'ok ' => true ]) );
115116
116- $ expectedBody = json_encode (['text ' => $ message ]);
117+ $ expectedBody = json_encode (['channel ' => $ channel , ' text ' => $ message ]);
117118
118119 $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
119- $ this ->assertSame ($ expectedBody , $ options ['body ' ]);
120+ $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
120121
121122 return $ response ;
122123 });
123124
124- $ transport = new SlackTransport ($ path , $ client );
125+ $ transport = new SlackTransport ($ token , $ channel , $ client );
125126
126127 $ transport ->send (new ChatMessage ('testMessage ' ));
127128 }
128129
129130 public function testSendWithNotification (): void
130131 {
131- $ host = 'testHost ' ;
132+ $ token = 'testToken ' ;
133+ $ channel = 'testChannel ' ;
132134 $ message = 'testMessage ' ;
133135
134136 $ response = $ this ->createMock (ResponseInterface::class);
@@ -139,24 +141,25 @@ public function testSendWithNotification(): void
139141
140142 $ response ->expects ($ this ->once ())
141143 ->method ('getContent ' )
142- ->willReturn ('ok ' );
144+ ->willReturn (json_encode ([ 'ok ' => true ]) );
143145
144146 $ notification = new Notification ($ message );
145147 $ chatMessage = ChatMessage::fromNotification ($ notification );
146148 $ options = SlackOptions::fromNotification ($ notification );
147149
148150 $ expectedBody = json_encode ([
149151 'blocks ' => $ options ->toArray ()['blocks ' ],
152+ 'channel ' => $ channel ,
150153 'text ' => $ message ,
151154 ]);
152155
153156 $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
154- $ this ->assertSame ($ expectedBody , $ options ['body ' ]);
157+ $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
155158
156159 return $ response ;
157160 });
158161
159- $ transport = new SlackTransport ($ host , $ client );
162+ $ transport = new SlackTransport ($ token , $ channel , $ client );
160163
161164 $ transport ->send ($ chatMessage );
162165 }
@@ -169,14 +172,15 @@ public function testSendWithInvalidOptions(): void
169172 return $ this ->createMock (ResponseInterface::class);
170173 });
171174
172- $ transport = new SlackTransport ('testHost ' , $ client );
175+ $ transport = new SlackTransport ('testToken ' , ' testChannel ' , $ client );
173176
174177 $ transport ->send (new ChatMessage ('testMessage ' , $ this ->createMock (MessageOptionsInterface::class)));
175178 }
176179
177180 public function testSendWith200ResponseButNotOk (): void
178181 {
179- $ host = 'testChannel ' ;
182+ $ token = 'testToken ' ;
183+ $ channel = 'testChannel ' ;
180184 $ message = 'testMessage ' ;
181185
182186 $ this ->expectException (TransportException::class);
@@ -189,17 +193,17 @@ public function testSendWith200ResponseButNotOk(): void
189193
190194 $ response ->expects ($ this ->once ())
191195 ->method ('getContent ' )
192- ->willReturn (' testErrorCode ' );
196+ ->willReturn (json_encode ([ ' ok ' => false , ' error ' => ' testErrorCode ']) );
193197
194- $ expectedBody = json_encode (['text ' => $ message ]);
198+ $ expectedBody = json_encode (['channel ' => $ channel , ' text ' => $ message ]);
195199
196200 $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
197- $ this ->assertSame ($ expectedBody , $ options ['body ' ]);
201+ $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
198202
199203 return $ response ;
200204 });
201205
202- $ transport = new SlackTransport ($ host , $ client );
206+ $ transport = new SlackTransport ($ token , $ channel , $ client );
203207
204208 $ transport ->send (new ChatMessage ('testMessage ' ));
205209 }
0 commit comments