Skip to content

Commit 76f02fd

Browse files
committed
minor #39749 [Notifier] Use abstract test cases in 5.x (OskarStark)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] Use abstract test cases in 5.x | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | --- | License | MIT | Doc PR | --- Same as #39736, but for `5.x` cc @derrabus Commits ------- c233636 [Notifier] Use abstract test cases in 5.x
2 parents 9f5d178 + c233636 commit 76f02fd

15 files changed

+341
-579
lines changed

src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,47 @@
1111

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

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
16-
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17-
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
18-
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
19-
use Symfony\Component\Notifier\Transport\Dsn;
15+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
2017

21-
final class DiscordTransportFactoryTest extends TestCase
18+
final class DiscordTransportFactoryTest extends TransportFactoryTestCase
2219
{
23-
public function testCreateWithDsn()
20+
/**
21+
* @return DiscordTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
2424
{
25-
$factory = $this->createFactory();
26-
27-
$transport = $factory->create(Dsn::fromString('discord://token@host.test?webhook_id=testWebhookId'));
28-
29-
$this->assertSame('discord://host.test?webhook_id=testWebhookId', (string) $transport);
30-
}
31-
32-
public function testCreateWithMissingOptionWebhookIdThrowsMissingRequiredOptionException()
33-
{
34-
$factory = $this->createFactory();
35-
36-
$this->expectException(MissingRequiredOptionException::class);
37-
38-
$factory->create(Dsn::fromString('discord://token@host'));
39-
}
40-
41-
public function testCreateWithNoTokenThrowsIncompleteDsnException()
42-
{
43-
$factory = $this->createFactory();
44-
45-
$this->expectException(IncompleteDsnException::class);
46-
$factory->create(Dsn::fromString('discord://host.test?webhook_id=testWebhookId'));
25+
return new DiscordTransportFactory();
4726
}
4827

49-
public function testSupportsReturnsTrueWithSupportedScheme()
28+
public function createProvider(): iterable
5029
{
51-
$factory = $this->createFactory();
52-
53-
$this->assertTrue($factory->supports(Dsn::fromString('discord://host?webhook_id=testWebhookId')));
30+
yield [
31+
'discord://host.test?webhook_id=testWebhookId',
32+
'discord://token@host.test?webhook_id=testWebhookId',
33+
];
5434
}
5535

56-
public function testSupportsReturnsFalseWithUnsupportedScheme()
36+
public function supportsProvider(): iterable
5737
{
58-
$factory = $this->createFactory();
59-
60-
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host?webhook_id=testWebhookId')));
38+
yield [true, 'discord://host?webhook_id=testWebhookId'];
39+
yield [false, 'somethingElse://host?webhook_id=testWebhookId'];
6140
}
6241

63-
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
42+
public function incompleteDsnProvider(): iterable
6443
{
65-
$factory = $this->createFactory();
66-
67-
$this->expectException(UnsupportedSchemeException::class);
68-
$factory->create(Dsn::fromString('somethingElse://token@host?webhook_id=testWebhookId'));
44+
yield 'missing token' => ['discord://host.test?webhook_id=testWebhookId'];
6945
}
7046

71-
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
47+
public function missingRequiredOptionProvider(): iterable
7248
{
73-
$factory = $this->createFactory();
74-
75-
$this->expectException(UnsupportedSchemeException::class);
76-
77-
// unsupported scheme and missing "webhook_id" option
78-
$factory->create(Dsn::fromString('somethingElse://token@host'));
49+
yield 'missing option: webhook_id' => ['discord://token@host'];
7950
}
8051

81-
private function createFactory(): DiscordTransportFactory
52+
public function unsupportedSchemeProvider(): iterable
8253
{
83-
return new DiscordTransportFactory();
54+
yield ['somethingElse://token@host?webhook_id=testWebhookId'];
55+
yield ['somethingElse://token@host']; // missing "webhook_id" option
8456
}
8557
}

src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,42 @@
1111

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

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\HttpClient\MockHttpClient;
1615
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
1716
use Symfony\Component\Notifier\Exception\LengthException;
1817
use Symfony\Component\Notifier\Exception\TransportException;
19-
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
2018
use Symfony\Component\Notifier\Message\ChatMessage;
2119
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SmsMessage;
21+
use Symfony\Component\Notifier\Tests\TransportTestCase;
22+
use Symfony\Component\Notifier\Transport\TransportInterface;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
2324
use Symfony\Contracts\HttpClient\ResponseInterface;
2425

25-
final class DiscordTransportTest extends TestCase
26+
final class DiscordTransportTest extends TransportTestCase
2627
{
27-
public function testToStringContainsProperties()
28+
/**
29+
* @return DiscordTransport
30+
*/
31+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
2832
{
29-
$transport = $this->createTransport();
30-
31-
$this->assertSame('discord://host.test?webhook_id=testWebhookId', (string) $transport);
33+
return (new DiscordTransport('testToken', 'testWebhookId', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
3234
}
3335

34-
public function testSupportsChatMessage()
36+
public function toStringProvider(): iterable
3537
{
36-
$transport = $this->createTransport();
37-
38-
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
39-
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
38+
yield ['discord://host.test?webhook_id=testWebhookId', $this->createTransport()];
4039
}
4140

42-
public function testSendNonChatMessageThrowsLogicException()
41+
public function supportedMessagesProvider(): iterable
4342
{
44-
$transport = $this->createTransport();
45-
46-
$this->expectException(UnsupportedMessageTypeException::class);
43+
yield [new ChatMessage('Hello!')];
44+
}
4745

48-
$transport->send($this->createMock(MessageInterface::class));
46+
public function unsupportedMessagesProvider(): iterable
47+
{
48+
yield [new SmsMessage('0611223344', 'Hello!')];
49+
yield [$this->createMock(MessageInterface::class)];
4950
}
5051

5152
public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
@@ -79,9 +80,4 @@ public function testSendWithErrorResponseThrows()
7980

8081
$transport->send(new ChatMessage('testMessage'));
8182
}
82-
83-
private function createTransport(?HttpClientInterface $client = null): DiscordTransport
84-
{
85-
return (new DiscordTransport('testToken', 'testWebhookId', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('host.test');
86-
}
8783
}

src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportFactoryTest.php

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,94 +11,50 @@
1111

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

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
16-
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17-
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
18-
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
19-
use Symfony\Component\Notifier\Transport\Dsn;
15+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
2017

21-
final class EsendexTransportFactoryTest extends TestCase
18+
final class EsendexTransportFactoryTest extends TransportFactoryTestCase
2219
{
23-
public function testCreateWithDsn()
20+
/**
21+
* @return EsendexTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
2424
{
25-
$factory = $this->createFactory();
26-
27-
$transport = $factory->create(Dsn::fromString('esendex://email:password@host.test?accountreference=testAccountreference&from=testFrom'));
28-
29-
$this->assertSame('esendex://host.test?accountreference=testAccountreference&from=testFrom', (string) $transport);
30-
}
31-
32-
public function testCreateWithMissingEmailThrowsIncompleteDsnException()
33-
{
34-
$factory = $this->createFactory();
35-
36-
$this->expectException(IncompleteDsnException::class);
37-
38-
$factory->create(Dsn::fromString('esendex://:password@host?accountreference=testAccountreference&from=FROM'));
39-
}
40-
41-
public function testCreateWithMissingPasswordThrowsIncompleteDsnException()
42-
{
43-
$factory = $this->createFactory();
44-
45-
$this->expectException(IncompleteDsnException::class);
46-
47-
$factory->create(Dsn::fromString('esendex://email:@host?accountreference=testAccountreference&from=FROM'));
48-
}
49-
50-
public function testCreateWithMissingOptionAccountreferenceThrowsMissingRequiredOptionException()
51-
{
52-
$factory = $this->createFactory();
53-
54-
$this->expectException(MissingRequiredOptionException::class);
55-
56-
$factory->create(Dsn::fromString('esendex://email:password@host?from=FROM'));
57-
}
58-
59-
public function testCreateWithMissingOptionFromThrowsMissingRequiredOptionException()
60-
{
61-
$factory = $this->createFactory();
62-
63-
$this->expectException(MissingRequiredOptionException::class);
64-
65-
$factory->create(Dsn::fromString('esendex://email:password@host?accountreference=ACCOUNTREFERENCE'));
25+
return new EsendexTransportFactory();
6626
}
6727

68-
public function testSupportsReturnsTrueWithSupportedScheme()
28+
public function createProvider(): iterable
6929
{
70-
$factory = $this->createFactory();
71-
72-
$this->assertTrue($factory->supports(Dsn::fromString('esendex://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM')));
30+
yield [
31+
'esendex://host.test?accountreference=ACCOUNTREFERENCE&from=FROM',
32+
'esendex://email:password@host.test?accountreference=ACCOUNTREFERENCE&from=FROM',
33+
];
7334
}
7435

75-
public function testSupportsReturnsFalseWithUnsupportedScheme()
36+
public function supportsProvider(): iterable
7637
{
77-
$factory = $this->createFactory();
78-
79-
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM')));
38+
yield [true, 'esendex://email:password@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
39+
yield [false, 'somethingElse://email:password@default'];
8040
}
8141

82-
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
42+
public function incompleteDsnProvider(): iterable
8343
{
84-
$factory = $this->createFactory();
85-
86-
$this->expectException(UnsupportedSchemeException::class);
87-
$factory->create(Dsn::fromString('somethingElse://email:password@host?accountreference=REFERENCE&from=FROM'));
44+
yield 'missing credentials' => ['esendex://host?accountreference=ACCOUNTREFERENCE&from=FROM'];
45+
yield 'missing email' => ['esendex://:password@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
46+
yield 'missing password' => ['esendex://email:@host?accountreference=ACCOUNTREFERENCE&from=FROM'];
8847
}
8948

90-
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
49+
public function missingRequiredOptionProvider(): iterable
9150
{
92-
$factory = $this->createFactory();
93-
94-
$this->expectException(UnsupportedSchemeException::class);
95-
96-
// unsupported scheme and missing "from" option
97-
$factory->create(Dsn::fromString('somethingElse://email:password@host?accountreference=REFERENCE'));
51+
yield 'missing option: from' => ['esendex://email:password@host?accountreference=ACCOUNTREFERENCE'];
52+
yield 'missing option: accountreference' => ['esendex://email:password@host?from=FROM'];
9853
}
9954

100-
private function createFactory(): EsendexTransportFactory
55+
public function unsupportedSchemeProvider(): iterable
10156
{
102-
return new EsendexTransportFactory();
57+
yield ['somethingElse://email:password@default?accountreference=ACCOUNTREFERENCE&from=FROM'];
58+
yield ['somethingElse://email:password@host?accountreference=ACCOUNTREFERENCE']; // missing "from" option
10359
}
10460
}

src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportTest.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,44 @@
1111

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

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\HttpClient\MockHttpClient;
1615
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransport;
1716
use Symfony\Component\Notifier\Exception\TransportException;
18-
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
1918
use Symfony\Component\Notifier\Message\MessageInterface;
2019
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Tests\TransportTestCase;
21+
use Symfony\Component\Notifier\Transport\TransportInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223
use Symfony\Contracts\HttpClient\ResponseInterface;
2324

24-
final class EsendexTransportTest extends TestCase
25+
final class EsendexTransportTest extends TransportTestCase
2526
{
26-
public function testToString()
27+
/**
28+
* @return EsendexTransport
29+
*/
30+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
2731
{
28-
$transport = $this->createTransport();
29-
30-
$this->assertSame('esendex://host.test?accountreference=testAccountReference&from=testFrom', (string) $transport);
32+
return (new EsendexTransport('email', 'password', 'testAccountReference', 'testFrom', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
3133
}
3234

33-
public function testSupportsSmsMessage()
35+
public function toStringProvider(): iterable
3436
{
35-
$transport = $this->createTransport();
36-
37-
$this->assertTrue($transport->supports(new SmsMessage('phone', 'testSmsMessage')));
38-
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
37+
yield ['esendex://host.test?accountreference=testAccountReference&from=testFrom', $this->createTransport()];
3938
}
4039

41-
public function testSendNonSmsMessageThrowsLogicException()
40+
public function supportedMessagesProvider(): iterable
4241
{
43-
$transport = $this->createTransport();
44-
45-
$this->expectException(UnsupportedMessageTypeException::class);
42+
yield [new SmsMessage('0611223344', 'Hello!')];
43+
}
4644

47-
$transport->send($this->createMock(MessageInterface::class));
45+
public function unsupportedMessagesProvider(): iterable
46+
{
47+
yield [new ChatMessage('Hello!')];
48+
yield [$this->createMock(MessageInterface::class)];
4849
}
4950

50-
public function testSendWithErrorResponseThrows()
51+
public function testSendWithErrorResponseThrowsTransportException()
5152
{
5253
$response = $this->createMock(ResponseInterface::class);
5354
$response->expects($this->exactly(2))
@@ -66,7 +67,7 @@ public function testSendWithErrorResponseThrows()
6667
$transport->send(new SmsMessage('phone', 'testMessage'));
6768
}
6869

69-
public function testSendWithErrorResponseContainingDetailsThrows()
70+
public function testSendWithErrorResponseContainingDetailsThrowsTransportException()
7071
{
7172
$response = $this->createMock(ResponseInterface::class);
7273
$response->expects($this->exactly(2))
@@ -87,9 +88,4 @@ public function testSendWithErrorResponseContainingDetailsThrows()
8788

8889
$transport->send(new SmsMessage('phone', 'testMessage'));
8990
}
90-
91-
private function createTransport(?HttpClientInterface $client = null): EsendexTransport
92-
{
93-
return (new EsendexTransport('testEmail', 'testPassword', 'testAccountReference', 'testFrom', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
94-
}
9591
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy