11
11
12
12
namespace Symfony \Component \Notifier \Bridge \Slack \Tests ;
13
13
14
- use PHPUnit \Framework \TestCase ;
15
14
use Symfony \Component \HttpClient \MockHttpClient ;
16
15
use Symfony \Component \Notifier \Bridge \Slack \SlackOptions ;
17
16
use Symfony \Component \Notifier \Bridge \Slack \SlackTransport ;
18
17
use Symfony \Component \Notifier \Exception \InvalidArgumentException ;
19
18
use Symfony \Component \Notifier \Exception \LogicException ;
20
19
use Symfony \Component \Notifier \Exception \TransportException ;
21
- use Symfony \Component \Notifier \Exception \UnsupportedMessageTypeException ;
22
20
use Symfony \Component \Notifier \Message \ChatMessage ;
23
21
use Symfony \Component \Notifier \Message \MessageInterface ;
24
22
use Symfony \Component \Notifier \Message \MessageOptionsInterface ;
23
+ use Symfony \Component \Notifier \Message \SmsMessage ;
25
24
use Symfony \Component \Notifier \Notification \Notification ;
25
+ use Symfony \Component \Notifier \Tests \TransportTestCase ;
26
+ use Symfony \Component \Notifier \Transport \TransportInterface ;
26
27
use Symfony \Contracts \HttpClient \HttpClientInterface ;
27
28
use Symfony \Contracts \HttpClient \ResponseInterface ;
28
29
29
- final class SlackTransportTest extends TestCase
30
+ final class SlackTransportTest extends TransportTestCase
30
31
{
31
- public function testToStringContainsProperties ()
32
+ /**
33
+ * @return SlackTransport
34
+ */
35
+ public function createTransport (?HttpClientInterface $ client = null , string $ channel = null ): TransportInterface
32
36
{
33
- $ channel = 'test Channel ' ; // invalid channel name to test url encoding of the channel
34
-
35
- $ transport = new SlackTransport ('xoxb-TestToken ' , $ channel , $ this ->createMock (HttpClientInterface::class));
36
- $ transport ->setHost ('host.test ' );
37
-
38
- $ this ->assertSame ('slack://host.test?channel=test+Channel ' , (string ) $ transport );
37
+ return new SlackTransport ('xoxb-TestToken ' , $ channel , $ client ?: $ this ->createMock (HttpClientInterface::class));
39
38
}
40
39
41
- public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentException ()
40
+ public function toStringProvider (): iterable
42
41
{
43
- $ this ->expectException (InvalidArgumentException::class);
44
- $ 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. ' );
45
-
46
- new SlackTransport ('token ' , 'testChannel ' , $ this ->createMock (HttpClientInterface::class));
42
+ yield ['slack://slack.com ' , $ this ->createTransport ()];
43
+ yield ['slack://slack.com?channel=test+Channel ' , $ this ->createTransport (null , 'test Channel ' )];
47
44
}
48
45
49
- public function testSupportsChatMessage ()
46
+ public function supportedMessagesProvider (): iterable
50
47
{
51
- $ transport = new SlackTransport ('xoxb-TestToken ' , 'testChannel ' , $ this ->createMock (HttpClientInterface::class));
52
-
53
- $ this ->assertTrue ($ transport ->supports (new ChatMessage ('testChatMessage ' )));
54
- $ this ->assertFalse ($ transport ->supports ($ this ->createMock (MessageInterface::class)));
48
+ yield [new ChatMessage ('Hello! ' )];
55
49
}
56
50
57
- public function testSendNonChatMessageThrowsLogicException ()
51
+ public function unsupportedMessagesProvider (): iterable
58
52
{
59
- $ transport = new SlackTransport ('xoxb-TestToken ' , 'testChannel ' , $ this ->createMock (HttpClientInterface::class));
53
+ yield [new SmsMessage ('0611223344 ' , 'Hello! ' )];
54
+ yield [$ this ->createMock (MessageInterface::class)];
55
+ }
60
56
61
- $ this ->expectException (UnsupportedMessageTypeException::class);
57
+ public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentException ()
58
+ {
59
+ $ this ->expectException (InvalidArgumentException::class);
60
+ $ 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. ' );
62
61
63
- $ transport -> send ( $ this ->createMock (MessageInterface ::class));
62
+ new SlackTransport ( ' token ' , ' testChannel ' , $ this ->createMock (HttpClientInterface ::class));
64
63
}
65
64
66
- public function testSendWithEmptyArrayResponseThrows ()
65
+ public function testSendWithEmptyArrayResponseThrowsTransportException ()
67
66
{
68
67
$ this ->expectException (TransportException::class);
69
68
@@ -79,12 +78,12 @@ public function testSendWithEmptyArrayResponseThrows()
79
78
return $ response ;
80
79
});
81
80
82
- $ transport = new SlackTransport ( ' xoxb-TestToken ' , 'testChannel ' , $ client );
81
+ $ transport = $ this -> createTransport ( $ client , 'testChannel ' );
83
82
84
83
$ transport ->send (new ChatMessage ('testMessage ' ));
85
84
}
86
85
87
- public function testSendWithErrorResponseThrows ()
86
+ public function testSendWithErrorResponseThrowsTransportException ()
88
87
{
89
88
$ this ->expectException (TransportException::class);
90
89
$ this ->expectExceptionMessageMatches ('/testErrorCode/ ' );
@@ -102,14 +101,13 @@ public function testSendWithErrorResponseThrows()
102
101
return $ response ;
103
102
});
104
103
105
- $ transport = new SlackTransport ( ' xoxb-TestToken ' , 'testChannel ' , $ client );
104
+ $ transport = $ this -> createTransport ( $ client , 'testChannel ' );
106
105
107
106
$ transport ->send (new ChatMessage ('testMessage ' ));
108
107
}
109
108
110
109
public function testSendWithOptions ()
111
110
{
112
- $ token = 'xoxb-TestToken ' ;
113
111
$ channel = 'testChannel ' ;
114
112
$ message = 'testMessage ' ;
115
113
@@ -131,14 +129,13 @@ public function testSendWithOptions()
131
129
return $ response ;
132
130
});
133
131
134
- $ transport = new SlackTransport ( $ token , $ channel, $ client );
132
+ $ transport = $ this -> createTransport ( $ client , $ channel );
135
133
136
134
$ transport ->send (new ChatMessage ('testMessage ' ));
137
135
}
138
136
139
137
public function testSendWithNotification ()
140
138
{
141
- $ token = 'xoxb-TestToken ' ;
142
139
$ channel = 'testChannel ' ;
143
140
$ message = 'testMessage ' ;
144
141
@@ -168,7 +165,7 @@ public function testSendWithNotification()
168
165
return $ response ;
169
166
});
170
167
171
- $ transport = new SlackTransport ( $ token , $ channel, $ client );
168
+ $ transport = $ this -> createTransport ( $ client , $ channel );
172
169
173
170
$ transport ->send ($ chatMessage );
174
171
}
@@ -181,14 +178,13 @@ public function testSendWithInvalidOptions()
181
178
return $ this ->createMock (ResponseInterface::class);
182
179
});
183
180
184
- $ transport = new SlackTransport ( ' xoxb-TestToken ' , 'testChannel ' , $ client );
181
+ $ transport = $ this -> createTransport ( $ client , 'testChannel ' );
185
182
186
183
$ transport ->send (new ChatMessage ('testMessage ' , $ this ->createMock (MessageOptionsInterface::class)));
187
184
}
188
185
189
186
public function testSendWith200ResponseButNotOk ()
190
187
{
191
- $ token = 'xoxb-TestToken ' ;
192
188
$ channel = 'testChannel ' ;
193
189
$ message = 'testMessage ' ;
194
190
@@ -212,7 +208,7 @@ public function testSendWith200ResponseButNotOk()
212
208
return $ response ;
213
209
});
214
210
215
- $ transport = new SlackTransport ( $ token , $ channel, $ client );
211
+ $ transport = $ this -> createTransport ( $ client , $ channel );
216
212
217
213
$ transport ->send (new ChatMessage ('testMessage ' ));
218
214
}
0 commit comments