Skip to content

Commit 0b6c9e5

Browse files
OskarStarkderrabus
authored andcommitted
[Notifier] [DX] Abstract test cases
1 parent 9051158 commit 0b6c9e5

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

Tests/TransportFactoryTestCase.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
19+
20+
/**
21+
* A test case to ease testing a notifier transport factory.
22+
*
23+
* @author Oskar Stark <[email protected]>
24+
*/
25+
abstract class TransportFactoryTestCase extends TestCase
26+
{
27+
abstract public function createFactory(): TransportFactoryInterface;
28+
29+
/**
30+
* @return iterable<array{0: bool, 1: string}>
31+
*/
32+
abstract public function supportsProvider(): iterable;
33+
34+
/**
35+
* @return iterable<array{0: string, 1: string, 2: TransportInterface}>
36+
*/
37+
abstract public function createProvider(): iterable;
38+
39+
/**
40+
* @return iterable<array{0: string, 1: string|null}>
41+
*/
42+
public function unsupportedSchemeProvider(): iterable
43+
{
44+
return [];
45+
}
46+
47+
/**
48+
* @return iterable<array{0: string, 1: string|null}>
49+
*/
50+
public function incompleteDsnProvider(): iterable
51+
{
52+
return [];
53+
}
54+
55+
/**
56+
* @dataProvider supportsProvider
57+
*/
58+
public function testSupports(bool $expected, string $dsn): void
59+
{
60+
$factory = $this->createFactory();
61+
62+
$this->assertSame($expected, $factory->supports(Dsn::fromString($dsn)));
63+
}
64+
65+
/**
66+
* @dataProvider createProvider
67+
*/
68+
public function testCreate(string $expected, string $dsn): void
69+
{
70+
$factory = $this->createFactory();
71+
$transport = $factory->create(Dsn::fromString($dsn));
72+
73+
$this->assertSame($expected, (string) $transport);
74+
}
75+
76+
/**
77+
* @dataProvider unsupportedSchemeProvider
78+
*/
79+
public function testUnsupportedSchemeException(string $dsn, string $message = null): void
80+
{
81+
$factory = $this->createFactory();
82+
83+
$dsn = Dsn::fromString($dsn);
84+
85+
$this->expectException(UnsupportedSchemeException::class);
86+
if (null !== $message) {
87+
$this->expectExceptionMessage($message);
88+
}
89+
90+
$factory->create($dsn);
91+
}
92+
93+
/**
94+
* @dataProvider incompleteDsnProvider
95+
*/
96+
public function testIncompleteDsnException(string $dsn, string $message = null): void
97+
{
98+
$factory = $this->createFactory();
99+
100+
$dsn = Dsn::fromString($dsn);
101+
102+
$this->expectException(IncompleteDsnException::class);
103+
if (null !== $message) {
104+
$this->expectExceptionMessage($message);
105+
}
106+
107+
$factory->create($dsn);
108+
}
109+
}

Tests/TransportTestCase.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Exception\LogicException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
use Symfony\Contracts\HttpClient\HttpClientInterface;
19+
20+
/**
21+
* A test case to ease testing a Notifier transport.
22+
*
23+
* @author Oskar Stark <[email protected]>
24+
*/
25+
abstract class TransportTestCase extends TestCase
26+
{
27+
protected const CUSTOM_HOST = 'host.test';
28+
protected const CUSTOM_PORT = 42;
29+
30+
abstract public function createTransport(?HttpClientInterface $client = null): TransportInterface;
31+
32+
/**
33+
* @return iterable<array{0: string, 1: TransportInterface}>
34+
*/
35+
abstract public function toStringProvider(): iterable;
36+
37+
/**
38+
* @return iterable<array{0: MessageInterface, 1: TransportInterface}>
39+
*/
40+
abstract public function supportedMessagesProvider(): iterable;
41+
42+
/**
43+
* @return iterable<array{0: MessageInterface, 1: TransportInterface}>
44+
*/
45+
abstract public function unsupportedMessagesProvider(): iterable;
46+
47+
/**
48+
* @dataProvider toStringProvider
49+
*/
50+
public function testToString(string $expected, TransportInterface $transport): void
51+
{
52+
$this->assertSame($expected, (string) $transport);
53+
}
54+
55+
/**
56+
* @dataProvider supportedMessagesProvider
57+
*/
58+
public function testSupportedMessages(MessageInterface $message, ?TransportInterface $transport = null): void
59+
{
60+
if (null === $transport) {
61+
$transport = $this->createTransport();
62+
}
63+
64+
$this->assertTrue($transport->supports($message));
65+
}
66+
67+
/**
68+
* @dataProvider unsupportedMessagesProvider
69+
*/
70+
public function testUnsupportedMessages(MessageInterface $message, ?TransportInterface $transport = null): void
71+
{
72+
if (null === $transport) {
73+
$transport = $this->createTransport();
74+
}
75+
76+
$this->assertFalse($transport->supports($message));
77+
}
78+
79+
/**
80+
* @dataProvider unsupportedMessagesProvider
81+
*/
82+
public function testUnsupportedMessagesTrowLogicExceptionWhenSend(MessageInterface $message, ?TransportInterface $transport = null): void
83+
{
84+
if (null === $transport) {
85+
$transport = $this->createTransport();
86+
}
87+
88+
$this->expectException(LogicException::class);
89+
90+
$transport->send($message);
91+
}
92+
93+
public function testCanSetCustomHost(): void
94+
{
95+
$transport = $this->createTransport();
96+
97+
$transport->setHost($customHost = self::CUSTOM_HOST);
98+
99+
$this->assertStringContainsString(sprintf('://%s', $customHost), (string) $transport);
100+
}
101+
102+
public function testCanSetCustomPort(): void
103+
{
104+
$transport = $this->createTransport();
105+
106+
$transport->setPort($customPort = self::CUSTOM_PORT);
107+
108+
/*
109+
* @see https://regex101.com/r/0xQKuY/2
110+
*/
111+
$this->assertMatchesRegularExpression(sprintf('/^.*\/\/.*\:%s.*$/', $customPort), (string) $transport);
112+
}
113+
114+
public function testCanSetCustomHostAndPort(): void
115+
{
116+
$transport = $this->createTransport();
117+
118+
$transport->setHost($customHost = self::CUSTOM_HOST);
119+
$transport->setPort($customPort = self::CUSTOM_PORT);
120+
121+
$this->assertStringContainsString(sprintf('://%s:%s', $customHost, $customPort), (string) $transport);
122+
}
123+
}

0 commit comments

Comments
 (0)