Skip to content

Commit 66ae5ca

Browse files
committed
deprecate the TransportFactoryTestCase
1 parent c455af3 commit 66ae5ca

8 files changed

+149
-80
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ CHANGELOG
44
7.2
55
---
66

7+
* Deprecate `TransportFactoryTestCase`, extend `AbstractTransportFactoryTestCase` instead
8+
9+
The `testIncompleteDsnException()` test is no longer provided by default. If you make use of it by implementing the `incompleteDsnProvider()` data providers,
10+
you now need to use the `IncompleteDsnTestTrait`.
11+
712
* Make `TransportFactoryTestCase` compatible with PHPUnit 10+
813

914
7.1
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Mailer\Test;
13+
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
17+
use Symfony\Component\Mailer\Transport\Dsn;
18+
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
19+
use Symfony\Component\Mailer\Transport\TransportInterface;
20+
21+
abstract class AbstractTransportFactoryTestCase extends TestCase
22+
{
23+
protected const USER = 'u$er';
24+
protected const PASSWORD = 'pa$s';
25+
26+
abstract public function getFactory(): TransportFactoryInterface;
27+
28+
/**
29+
* @psalm-return iterable<array{0: Dsn, 1: bool}>
30+
*/
31+
abstract public static function supportsProvider(): iterable;
32+
33+
/**
34+
* @psalm-return iterable<array{0: Dsn, 1: TransportInterface}>
35+
*/
36+
abstract public static function createProvider(): iterable;
37+
38+
/**
39+
* @psalm-return iterable<array{0: Dsn, 1?: string|null}>
40+
*/
41+
abstract public static function unsupportedSchemeProvider(): iterable;
42+
43+
/**
44+
* @dataProvider supportsProvider
45+
*/
46+
#[DataProvider('supportsProvider')]
47+
public function testSupports(Dsn $dsn, bool $supports)
48+
{
49+
$factory = $this->getFactory();
50+
51+
$this->assertSame($supports, $factory->supports($dsn));
52+
}
53+
54+
/**
55+
* @dataProvider createProvider
56+
*/
57+
#[DataProvider('createProvider')]
58+
public function testCreate(Dsn $dsn, TransportInterface $transport)
59+
{
60+
$factory = $this->getFactory();
61+
62+
$this->assertEquals($transport, $factory->create($dsn));
63+
if (str_contains('smtp', $dsn->getScheme())) {
64+
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport);
65+
}
66+
}
67+
68+
/**
69+
* @dataProvider unsupportedSchemeProvider
70+
*/
71+
#[DataProvider('unsupportedSchemeProvider')]
72+
public function testUnsupportedSchemeException(Dsn $dsn, ?string $message = null)
73+
{
74+
$factory = $this->getFactory();
75+
76+
$this->expectException(UnsupportedSchemeException::class);
77+
if (null !== $message) {
78+
$this->expectExceptionMessage($message);
79+
}
80+
81+
$factory->create($dsn);
82+
}
83+
}

Test/IncompleteDsnTestTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Mailer\Test;
13+
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use Symfony\Component\Mailer\Exception\IncompleteDsnException;
16+
use Symfony\Component\Mailer\Transport\Dsn;
17+
18+
trait IncompleteDsnTestTrait
19+
{
20+
/**
21+
* @psalm-return iterable<array{0: Dsn}>
22+
*/
23+
abstract public static function incompleteDsnProvider(): iterable;
24+
25+
/**
26+
* @dataProvider incompleteDsnProvider
27+
*/
28+
#[DataProvider('incompleteDsnProvider')]
29+
public function testIncompleteDsnException(Dsn $dsn)
30+
{
31+
$factory = $this->getFactory();
32+
33+
$this->expectException(IncompleteDsnException::class);
34+
$factory->create($dsn);
35+
}
36+
}

Test/TransportFactoryTestCase.php

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,26 @@
1111

1212
namespace Symfony\Component\Mailer\Test;
1313

14-
use PHPUnit\Framework\Attributes\DataProvider;
15-
use PHPUnit\Framework\TestCase;
1614
use Psr\Log\LoggerInterface;
17-
use Symfony\Component\Mailer\Exception\IncompleteDsnException;
18-
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
1915
use Symfony\Component\Mailer\Transport\Dsn;
20-
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
21-
use Symfony\Component\Mailer\Transport\TransportInterface;
2216
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2317
use Symfony\Contracts\HttpClient\HttpClientInterface;
2418

2519
/**
2620
* A test case to ease testing Transport Factory.
2721
*
2822
* @author Konstantin Myakshin <[email protected]>
23+
*
24+
* @deprecated since Symfony 7.2, use AbstractTransportFactoryTestCase instead
2925
*/
30-
abstract class TransportFactoryTestCase extends TestCase
26+
abstract class TransportFactoryTestCase extends AbstractTransportFactoryTestCase
3127
{
32-
protected const USER = 'u$er';
33-
protected const PASSWORD = 'pa$s';
28+
use IncompleteDsnTestTrait;
3429

3530
protected EventDispatcherInterface $dispatcher;
3631
protected HttpClientInterface $client;
3732
protected LoggerInterface $logger;
3833

39-
abstract public function getFactory(): TransportFactoryInterface;
40-
41-
/**
42-
* @psalm-return iterable<array{0: Dsn, 1: bool}>
43-
*/
44-
abstract public static function supportsProvider(): iterable;
45-
46-
/**
47-
* @psalm-return iterable<array{0: Dsn, 1: TransportInterface}>
48-
*/
49-
abstract public static function createProvider(): iterable;
50-
5134
/**
5235
* @psalm-return iterable<array{0: Dsn, 1?: string|null}>
5336
*/
@@ -64,59 +47,6 @@ public static function incompleteDsnProvider(): iterable
6447
return [];
6548
}
6649

67-
/**
68-
* @dataProvider supportsProvider
69-
*/
70-
#[DataProvider('supportsProvider')]
71-
public function testSupports(Dsn $dsn, bool $supports)
72-
{
73-
$factory = $this->getFactory();
74-
75-
$this->assertSame($supports, $factory->supports($dsn));
76-
}
77-
78-
/**
79-
* @dataProvider createProvider
80-
*/
81-
#[DataProvider('createProvider')]
82-
public function testCreate(Dsn $dsn, TransportInterface $transport)
83-
{
84-
$factory = $this->getFactory();
85-
86-
$this->assertEquals($transport, $factory->create($dsn));
87-
if (str_contains('smtp', $dsn->getScheme())) {
88-
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport);
89-
}
90-
}
91-
92-
/**
93-
* @dataProvider unsupportedSchemeProvider
94-
*/
95-
#[DataProvider('unsupportedSchemeProvider')]
96-
public function testUnsupportedSchemeException(Dsn $dsn, ?string $message = null)
97-
{
98-
$factory = $this->getFactory();
99-
100-
$this->expectException(UnsupportedSchemeException::class);
101-
if (null !== $message) {
102-
$this->expectExceptionMessage($message);
103-
}
104-
105-
$factory->create($dsn);
106-
}
107-
108-
/**
109-
* @dataProvider incompleteDsnProvider
110-
*/
111-
#[DataProvider('incompleteDsnProvider')]
112-
public function testIncompleteDsnException(Dsn $dsn)
113-
{
114-
$factory = $this->getFactory();
115-
116-
$this->expectException(IncompleteDsnException::class);
117-
$factory->create($dsn);
118-
}
119-
12050
protected function getDispatcher(): EventDispatcherInterface
12151
{
12252
return $this->dispatcher ??= $this->createMock(EventDispatcherInterface::class);

Tests/Transport/NullTransportFactoryTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
use Psr\Log\NullLogger;
1515
use Symfony\Component\HttpClient\MockHttpClient;
16-
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase;
1717
use Symfony\Component\Mailer\Transport\Dsn;
1818
use Symfony\Component\Mailer\Transport\NullTransport;
1919
use Symfony\Component\Mailer\Transport\NullTransportFactory;
2020
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
2121

22-
class NullTransportFactoryTest extends TransportFactoryTestCase
22+
class NullTransportFactoryTest extends AbstractTransportFactoryTestCase
2323
{
2424
public function getFactory(): TransportFactoryInterface
2525
{
@@ -41,4 +41,9 @@ public static function createProvider(): iterable
4141
new NullTransport(null, new NullLogger()),
4242
];
4343
}
44+
45+
public static function unsupportedSchemeProvider(): iterable
46+
{
47+
yield [new Dsn('smtp', 'localhost')];
48+
}
4449
}

Tests/Transport/SendmailTransportFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
use Psr\Log\NullLogger;
1515
use Symfony\Component\HttpClient\MockHttpClient;
16-
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase;
1717
use Symfony\Component\Mailer\Transport\Dsn;
1818
use Symfony\Component\Mailer\Transport\SendmailTransport;
1919
use Symfony\Component\Mailer\Transport\SendmailTransportFactory;
2020
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
2121

22-
class SendmailTransportFactoryTest extends TransportFactoryTestCase
22+
class SendmailTransportFactoryTest extends AbstractTransportFactoryTestCase
2323
{
2424
public function getFactory(): TransportFactoryInterface
2525
{

Tests/Transport/Smtp/EsmtpTransportFactoryTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
use Psr\Log\NullLogger;
1515
use Symfony\Component\HttpClient\MockHttpClient;
16-
use Symfony\Component\Mailer\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase;
1717
use Symfony\Component\Mailer\Transport\Dsn;
1818
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
1919
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
2020
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
2121
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
2222

23-
class EsmtpTransportFactoryTest extends TransportFactoryTestCase
23+
class EsmtpTransportFactoryTest extends AbstractTransportFactoryTestCase
2424
{
2525
public function getFactory(): TransportFactoryInterface
2626
{
@@ -181,4 +181,9 @@ public static function createProvider(): iterable
181181
$transport,
182182
];
183183
}
184+
185+
public static function unsupportedSchemeProvider(): iterable
186+
{
187+
yield [new Dsn('null', '')];
188+
}
184189
}

Transport/Smtp/EsmtpTransportFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Mailer\Transport\Smtp;
1313

14+
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
1415
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
1516
use Symfony\Component\Mailer\Transport\Dsn;
1617
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
@@ -23,6 +24,10 @@ final class EsmtpTransportFactory extends AbstractTransportFactory
2324
{
2425
public function create(Dsn $dsn): TransportInterface
2526
{
27+
if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) {
28+
throw new UnsupportedSchemeException($dsn, 'smtp', $this->getSupportedSchemes());
29+
}
30+
2631
$autoTls = '' === $dsn->getOption('auto_tls') || filter_var($dsn->getOption('auto_tls', true), \FILTER_VALIDATE_BOOL);
2732
$tls = 'smtps' === $dsn->getScheme() ? true : ($autoTls ? null : false);
2833
$port = $dsn->getPort(0);

0 commit comments

Comments
 (0)