Skip to content

Commit f641553

Browse files
OskarStarkderrabus
authored andcommitted
[Notifier] Use abstract test cases in 5.x
1 parent 83dc034 commit f641553

File tree

2 files changed

+61
-77
lines changed

2 files changed

+61
-77
lines changed

Tests/SlackTransportFactoryTest.php

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,38 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Slack\Tests;
1313

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
16-
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
1715
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
18-
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
1917
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
2019

21-
final class SlackTransportFactoryTest extends TestCase
20+
final class SlackTransportFactoryTest extends TransportFactoryTestCase
2221
{
23-
public function testCreateWithDsn()
22+
/**
23+
* @return SlackTransportFactory
24+
*/
25+
public function createFactory(): TransportFactoryInterface
2426
{
25-
$factory = $this->createFactory();
26-
27-
$transport = $factory->create(Dsn::fromString('slack://xoxb-TestUser@host.test/?channel=testChannel'));
28-
29-
$this->assertSame('slack://host.test?channel=testChannel', (string) $transport);
27+
return new SlackTransportFactory();
3028
}
3129

32-
public function testCreateWithDsnWithoutPath()
30+
public function createProvider(): iterable
3331
{
34-
$factory = $this->createFactory();
35-
36-
$transport = $factory->create(Dsn::fromString('slack://xoxb-TestUser@host.test?channel=testChannel'));
37-
38-
$this->assertSame('slack://host.test?channel=testChannel', (string) $transport);
32+
yield [
33+
'slack://host.test',
34+
'slack://xoxb-TestToken@host.test',
35+
];
36+
37+
yield 'with path' => [
38+
'slack://host.test?channel=testChannel',
39+
'slack://xoxb-TestToken@host.test/?channel=testChannel',
40+
];
41+
42+
yield 'without path' => [
43+
'slack://host.test?channel=testChannel',
44+
'slack://xoxb-TestToken@host.test?channel=testChannel',
45+
];
3946
}
4047

4148
public function testCreateWithDeprecatedDsn()
@@ -48,38 +55,19 @@ public function testCreateWithDeprecatedDsn()
4855
$factory->create(Dsn::fromString('slack://default/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'));
4956
}
5057

51-
public function testCreateWithNoTokenThrowsInclompleteDsnException()
52-
{
53-
$factory = $this->createFactory();
54-
55-
$this->expectException(IncompleteDsnException::class);
56-
$factory->create(Dsn::fromString('slack://host.test?channel=testChannel'));
57-
}
58-
59-
public function testSupportsReturnsTrueWithSupportedScheme()
58+
public function supportsProvider(): iterable
6059
{
61-
$factory = $this->createFactory();
62-
63-
$this->assertTrue($factory->supports(Dsn::fromString('slack://host?channel=testChannel')));
60+
yield [true, 'slack://xoxb-TestToken@host?channel=testChannel'];
61+
yield [false, 'somethingElse://xoxb-TestToken@host?channel=testChannel'];
6462
}
6563

66-
public function testSupportsReturnsFalseWithUnsupportedScheme()
64+
public function incompleteDsnProvider(): iterable
6765
{
68-
$factory = $this->createFactory();
69-
70-
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host?channel=testChannel')));
66+
yield 'missing token' => ['slack://host.test?channel=testChannel'];
7167
}
7268

73-
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
69+
public function unsupportedSchemeProvider(): iterable
7470
{
75-
$factory = $this->createFactory();
76-
77-
$this->expectException(UnsupportedSchemeException::class);
78-
$factory->create(Dsn::fromString('somethingElse://host?channel=testChannel'));
79-
}
80-
81-
private function createFactory(): SlackTransportFactory
82-
{
83-
return new SlackTransportFactory();
71+
yield ['somethingElse://xoxb-TestToken@host?channel=testChannel'];
8472
}
8573
}

Tests/SlackTransportTest.php

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,58 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Slack\Tests;
1313

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\HttpClient\MockHttpClient;
1615
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
1716
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
1817
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1918
use Symfony\Component\Notifier\Exception\LogicException;
2019
use Symfony\Component\Notifier\Exception\TransportException;
21-
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
2220
use Symfony\Component\Notifier\Message\ChatMessage;
2321
use Symfony\Component\Notifier\Message\MessageInterface;
2422
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
23+
use Symfony\Component\Notifier\Message\SmsMessage;
2524
use Symfony\Component\Notifier\Notification\Notification;
25+
use Symfony\Component\Notifier\Tests\TransportTestCase;
26+
use Symfony\Component\Notifier\Transport\TransportInterface;
2627
use Symfony\Contracts\HttpClient\HttpClientInterface;
2728
use Symfony\Contracts\HttpClient\ResponseInterface;
2829

29-
final class SlackTransportTest extends TestCase
30+
final class SlackTransportTest extends TransportTestCase
3031
{
31-
public function testToStringContainsProperties()
32+
/**
33+
* @return SlackTransport
34+
*/
35+
public function createTransport(?HttpClientInterface $client = null, string $channel = null): TransportInterface
3236
{
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));
3938
}
4039

41-
public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentException()
40+
public function toStringProvider(): iterable
4241
{
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')];
4744
}
4845

49-
public function testSupportsChatMessage()
46+
public function supportedMessagesProvider(): iterable
5047
{
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!')];
5549
}
5650

57-
public function testSendNonChatMessageThrowsLogicException()
51+
public function unsupportedMessagesProvider(): iterable
5852
{
59-
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $this->createMock(HttpClientInterface::class));
53+
yield [new SmsMessage('0611223344', 'Hello!')];
54+
yield [$this->createMock(MessageInterface::class)];
55+
}
6056

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.');
6261

63-
$transport->send($this->createMock(MessageInterface::class));
62+
new SlackTransport('token', 'testChannel', $this->createMock(HttpClientInterface::class));
6463
}
6564

66-
public function testSendWithEmptyArrayResponseThrows()
65+
public function testSendWithEmptyArrayResponseThrowsTransportException()
6766
{
6867
$this->expectException(TransportException::class);
6968

@@ -79,12 +78,12 @@ public function testSendWithEmptyArrayResponseThrows()
7978
return $response;
8079
});
8180

82-
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);
81+
$transport = $this->createTransport($client, 'testChannel');
8382

8483
$transport->send(new ChatMessage('testMessage'));
8584
}
8685

87-
public function testSendWithErrorResponseThrows()
86+
public function testSendWithErrorResponseThrowsTransportException()
8887
{
8988
$this->expectException(TransportException::class);
9089
$this->expectExceptionMessageMatches('/testErrorCode/');
@@ -102,14 +101,13 @@ public function testSendWithErrorResponseThrows()
102101
return $response;
103102
});
104103

105-
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);
104+
$transport = $this->createTransport($client, 'testChannel');
106105

107106
$transport->send(new ChatMessage('testMessage'));
108107
}
109108

110109
public function testSendWithOptions()
111110
{
112-
$token = 'xoxb-TestToken';
113111
$channel = 'testChannel';
114112
$message = 'testMessage';
115113

@@ -131,14 +129,13 @@ public function testSendWithOptions()
131129
return $response;
132130
});
133131

134-
$transport = new SlackTransport($token, $channel, $client);
132+
$transport = $this->createTransport($client, $channel);
135133

136134
$transport->send(new ChatMessage('testMessage'));
137135
}
138136

139137
public function testSendWithNotification()
140138
{
141-
$token = 'xoxb-TestToken';
142139
$channel = 'testChannel';
143140
$message = 'testMessage';
144141

@@ -168,7 +165,7 @@ public function testSendWithNotification()
168165
return $response;
169166
});
170167

171-
$transport = new SlackTransport($token, $channel, $client);
168+
$transport = $this->createTransport($client, $channel);
172169

173170
$transport->send($chatMessage);
174171
}
@@ -181,14 +178,13 @@ public function testSendWithInvalidOptions()
181178
return $this->createMock(ResponseInterface::class);
182179
});
183180

184-
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);
181+
$transport = $this->createTransport($client, 'testChannel');
185182

186183
$transport->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class)));
187184
}
188185

189186
public function testSendWith200ResponseButNotOk()
190187
{
191-
$token = 'xoxb-TestToken';
192188
$channel = 'testChannel';
193189
$message = 'testMessage';
194190

@@ -212,7 +208,7 @@ public function testSendWith200ResponseButNotOk()
212208
return $response;
213209
});
214210

215-
$transport = new SlackTransport($token, $channel, $client);
211+
$transport = $this->createTransport($client, $channel);
216212

217213
$transport->send(new ChatMessage('testMessage'));
218214
}

0 commit comments

Comments
 (0)