1313
1414use PHPUnit \Framework \Attributes \TestWith ;
1515use Symfony \Component \HttpClient \MockHttpClient ;
16+ use Symfony \Component \HttpClient \Response \MockResponse ;
1617use Symfony \Component \Notifier \Bridge \Slack \SlackOptions ;
1718use Symfony \Component \Notifier \Bridge \Slack \SlackSentMessage ;
1819use Symfony \Component \Notifier \Bridge \Slack \SlackTransport ;
@@ -55,22 +56,14 @@ public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentExce
5556 $ this ->expectException (InvalidArgumentException::class);
5657 $ this ->expectExceptionMessage ('A valid Slack token needs to start with "xoxb-", "xoxp-" or "xoxa-2". See https://api.slack.com/authentication/token-types for further information. ' );
5758
58- new SlackTransport ('token ' , 'testChannel ' , $ this -> createMock (HttpClientInterface::class ));
59+ new SlackTransport ('token ' , 'testChannel ' , new MockHttpClient ( ));
5960 }
6061
6162 public function testSendWithEmptyArrayResponseThrowsTransportException ()
6263 {
6364 $ this ->expectException (TransportException::class);
6465
65- $ response = $ this ->createMock (ResponseInterface::class);
66- $ response ->expects ($ this ->exactly (2 ))
67- ->method ('getStatusCode ' )
68- ->willReturn (500 );
69- $ response ->expects ($ this ->once ())
70- ->method ('getContent ' )
71- ->willReturn ('[] ' );
72-
73- $ client = new MockHttpClient (static fn (): ResponseInterface => $ response );
66+ $ client = new MockHttpClient (new MockResponse ('[] ' , ['http_code ' => 500 ]));
7467
7568 $ transport = self ::createTransport ($ client , 'testChannel ' );
7669
@@ -82,16 +75,7 @@ public function testSendWithErrorResponseThrowsTransportException()
8275 $ this ->expectException (TransportException::class);
8376 $ this ->expectExceptionMessageMatches ('/testErrorCode/ ' );
8477
85- $ response = $ this ->createMock (ResponseInterface::class);
86- $ response ->expects ($ this ->exactly (2 ))
87- ->method ('getStatusCode ' )
88- ->willReturn (400 );
89-
90- $ response ->expects ($ this ->once ())
91- ->method ('getContent ' )
92- ->willReturn (json_encode (['error ' => 'testErrorCode ' ]));
93-
94- $ client = new MockHttpClient (static fn (): ResponseInterface => $ response );
78+ $ client = new MockHttpClient (new MockResponse (json_encode (['error ' => 'testErrorCode ' ]), ['http_code ' => 400 ]));
9579
9680 $ transport = self ::createTransport ($ client , 'testChannel ' );
9781
@@ -103,22 +87,12 @@ public function testSendWithOptions()
10387 $ channel = 'testChannel ' ;
10488 $ message = 'testMessage ' ;
10589
106- $ response = $ this ->createMock (ResponseInterface::class);
107-
108- $ response ->expects ($ this ->exactly (2 ))
109- ->method ('getStatusCode ' )
110- ->willReturn (200 );
111-
112- $ response ->expects ($ this ->once ())
113- ->method ('getContent ' )
114- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
115-
11690 $ expectedBody = json_encode (['channel ' => $ channel , 'text ' => $ message ]);
11791
118- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
92+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
11993 $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
12094
121- return $ response ;
95+ return new MockResponse ( json_encode ([ ' ok ' => true , ' ts ' => ' 1503435956.000247 ' , ' channel ' => ' C123456 ' ])) ;
12296 });
12397
12498 $ transport = self ::createTransport ($ client , $ channel );
@@ -135,16 +109,6 @@ public function testSendWithNotification()
135109 $ channel = 'testChannel ' ;
136110 $ message = 'testMessage ' ;
137111
138- $ response = $ this ->createMock (ResponseInterface::class);
139-
140- $ response ->expects ($ this ->exactly (2 ))
141- ->method ('getStatusCode ' )
142- ->willReturn (200 );
143-
144- $ response ->expects ($ this ->once ())
145- ->method ('getContent ' )
146- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
147-
148112 $ notification = new Notification ($ message );
149113 $ chatMessage = ChatMessage::fromNotification ($ notification );
150114 $ options = SlackOptions::fromNotification ($ notification );
@@ -155,10 +119,10 @@ public function testSendWithNotification()
155119 'text ' => $ message ,
156120 ]);
157121
158- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
122+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
159123 $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
160124
161- return $ response ;
125+ return new MockResponse ( json_encode ([ ' ok ' => true , ' ts ' => ' 1503435956.000247 ' , ' channel ' => ' C123456 ' ])) ;
162126 });
163127
164128 $ transport = self ::createTransport ($ client , $ channel );
@@ -175,16 +139,6 @@ public function testSendWithBooleanOptionValue(bool $value)
175139 $ channel = 'testChannel ' ;
176140 $ message = 'testMessage ' ;
177141
178- $ response = $ this ->createMock (ResponseInterface::class);
179-
180- $ response ->expects ($ this ->exactly (2 ))
181- ->method ('getStatusCode ' )
182- ->willReturn (200 );
183-
184- $ response ->expects ($ this ->once ())
185- ->method ('getContent ' )
186- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
187-
188142 $ options = new SlackOptions ();
189143 $ options ->asUser ($ value );
190144 $ options ->linkNames ($ value );
@@ -205,10 +159,10 @@ public function testSendWithBooleanOptionValue(bool $value)
205159 'unfurl_media ' => $ value ,
206160 ]);
207161
208- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
162+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
209163 $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
210164
211- return $ response ;
165+ return new MockResponse ( json_encode ([ ' ok ' => true , ' ts ' => ' 1503435956.000247 ' , ' channel ' => ' C123456 ' ])) ;
212166 });
213167
214168 $ transport = self ::createTransport ($ client , $ channel );
@@ -223,22 +177,12 @@ public function testSendWith200ResponseButNotOk()
223177
224178 $ this ->expectException (TransportException::class);
225179
226- $ response = $ this ->createMock (ResponseInterface::class);
227-
228- $ response ->expects ($ this ->exactly (2 ))
229- ->method ('getStatusCode ' )
230- ->willReturn (200 );
231-
232- $ response ->expects ($ this ->once ())
233- ->method ('getContent ' )
234- ->willReturn (json_encode (['ok ' => false , 'error ' => 'testErrorCode ' ]));
235-
236180 $ expectedBody = json_encode (['channel ' => $ channel , 'text ' => $ message ]);
237181
238- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
182+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
239183 $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
240184
241- return $ response ;
185+ return new MockResponse ( json_encode ([ ' ok ' => false , ' error ' => ' testErrorCode ' ])) ;
242186 });
243187
244188 $ transport = self ::createTransport ($ client , $ channel );
@@ -248,20 +192,10 @@ public function testSendWith200ResponseButNotOk()
248192
249193 public function testSendIncludesContentTypeWithCharset ()
250194 {
251- $ response = $ this ->createMock (ResponseInterface::class);
252-
253- $ response ->expects ($ this ->exactly (2 ))
254- ->method ('getStatusCode ' )
255- ->willReturn (200 );
256-
257- $ response ->expects ($ this ->once ())
258- ->method ('getContent ' )
259- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
260-
261- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response ): ResponseInterface {
195+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []): ResponseInterface {
262196 $ this ->assertContains ('Content-Type: application/json; charset=utf-8 ' , $ options ['headers ' ]);
263197
264- return $ response ;
198+ return new MockResponse ( json_encode ([ ' ok ' => true , ' ts ' => ' 1503435956.000247 ' , ' channel ' => ' C123456 ' ])) ;
265199 });
266200
267201 $ transport = self ::createTransport ($ client );
@@ -271,21 +205,11 @@ public function testSendIncludesContentTypeWithCharset()
271205
272206 public function testSendWithErrorsIncluded ()
273207 {
274- $ response = $ this ->createMock (ResponseInterface::class);
275-
276- $ response ->expects ($ this ->exactly (2 ))
277- ->method ('getStatusCode ' )
278- ->willReturn (200 );
279-
280- $ response ->expects ($ this ->once ())
281- ->method ('getContent ' )
282- ->willReturn (json_encode ([
283- 'ok ' => false ,
284- 'error ' => 'invalid_blocks ' ,
285- 'errors ' => ['no more than 50 items allowed [json-pointer:/blocks] ' ],
286- ]));
287-
288- $ client = new MockHttpClient (fn (): ResponseInterface => $ response );
208+ $ client = new MockHttpClient (new MockResponse (json_encode ([
209+ 'ok ' => false ,
210+ 'error ' => 'invalid_blocks ' ,
211+ 'errors ' => ['no more than 50 items allowed [json-pointer:/blocks] ' ],
212+ ])));
289213
290214 $ transport = self ::createTransport ($ client , 'testChannel ' );
291215
@@ -297,16 +221,6 @@ public function testSendWithErrorsIncluded()
297221
298222 public function testUpdateMessage ()
299223 {
300- $ response = $ this ->createMock (ResponseInterface::class);
301-
302- $ response ->expects ($ this ->exactly (2 ))
303- ->method ('getStatusCode ' )
304- ->willReturn (200 );
305-
306- $ response ->expects ($ this ->once ())
307- ->method ('getContent ' )
308- ->willReturn (json_encode (['ok ' => true , 'ts ' => '1503435956.000247 ' , 'channel ' => 'C123456 ' ]));
309-
310224 $ sentMessage = new SlackSentMessage (new ChatMessage ('Hello ' ), 'slack ' , 'C123456 ' , '1503435956.000247 ' );
311225 $ chatMessage = $ sentMessage ->getUpdateMessage ('Hello World ' );
312226
@@ -316,11 +230,11 @@ public function testUpdateMessage()
316230 'text ' => 'Hello World ' ,
317231 ]);
318232
319- $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ response , $ expectedBody ): ResponseInterface {
233+ $ client = new MockHttpClient (function (string $ method , string $ url , array $ options = []) use ($ expectedBody ): ResponseInterface {
320234 $ this ->assertJsonStringEqualsJsonString ($ expectedBody , $ options ['body ' ]);
321235 $ this ->assertStringEndsWith ('chat.update ' , $ url );
322236
323- return $ response ;
237+ return new MockResponse ( json_encode ([ ' ok ' => true , ' ts ' => ' 1503435956.000247 ' , ' channel ' => ' C123456 ' ])) ;
324238 });
325239
326240 $ transport = $ this ->createTransport ($ client , 'another-channel ' );
0 commit comments