From 66ae5ca3d980eba65f4a352e9629c69b7928cb69 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 18 Sep 2024 13:58:19 +0200 Subject: [PATCH] deprecate the TransportFactoryTestCase --- CHANGELOG.md | 5 ++ Test/AbstractTransportFactoryTestCase.php | 83 +++++++++++++++++++ Test/IncompleteDsnTestTrait.php | 36 ++++++++ Test/TransportFactoryTestCase.php | 78 +---------------- Tests/Transport/NullTransportFactoryTest.php | 9 +- .../SendmailTransportFactoryTest.php | 4 +- .../Smtp/EsmtpTransportFactoryTest.php | 9 +- Transport/Smtp/EsmtpTransportFactory.php | 5 ++ 8 files changed, 149 insertions(+), 80 deletions(-) create mode 100644 Test/AbstractTransportFactoryTestCase.php create mode 100644 Test/IncompleteDsnTestTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fb575..e77263d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ CHANGELOG 7.2 --- + * Deprecate `TransportFactoryTestCase`, extend `AbstractTransportFactoryTestCase` instead + + The `testIncompleteDsnException()` test is no longer provided by default. If you make use of it by implementing the `incompleteDsnProvider()` data providers, + you now need to use the `IncompleteDsnTestTrait`. + * Make `TransportFactoryTestCase` compatible with PHPUnit 10+ 7.1 diff --git a/Test/AbstractTransportFactoryTestCase.php b/Test/AbstractTransportFactoryTestCase.php new file mode 100644 index 0000000..c378948 --- /dev/null +++ b/Test/AbstractTransportFactoryTestCase.php @@ -0,0 +1,83 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mailer\Test; + +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; +use Symfony\Component\Mailer\Transport\Dsn; +use Symfony\Component\Mailer\Transport\TransportFactoryInterface; +use Symfony\Component\Mailer\Transport\TransportInterface; + +abstract class AbstractTransportFactoryTestCase extends TestCase +{ + protected const USER = 'u$er'; + protected const PASSWORD = 'pa$s'; + + abstract public function getFactory(): TransportFactoryInterface; + + /** + * @psalm-return iterable + */ + abstract public static function supportsProvider(): iterable; + + /** + * @psalm-return iterable + */ + abstract public static function createProvider(): iterable; + + /** + * @psalm-return iterable + */ + abstract public static function unsupportedSchemeProvider(): iterable; + + /** + * @dataProvider supportsProvider + */ + #[DataProvider('supportsProvider')] + public function testSupports(Dsn $dsn, bool $supports) + { + $factory = $this->getFactory(); + + $this->assertSame($supports, $factory->supports($dsn)); + } + + /** + * @dataProvider createProvider + */ + #[DataProvider('createProvider')] + public function testCreate(Dsn $dsn, TransportInterface $transport) + { + $factory = $this->getFactory(); + + $this->assertEquals($transport, $factory->create($dsn)); + if (str_contains('smtp', $dsn->getScheme())) { + $this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport); + } + } + + /** + * @dataProvider unsupportedSchemeProvider + */ + #[DataProvider('unsupportedSchemeProvider')] + public function testUnsupportedSchemeException(Dsn $dsn, ?string $message = null) + { + $factory = $this->getFactory(); + + $this->expectException(UnsupportedSchemeException::class); + if (null !== $message) { + $this->expectExceptionMessage($message); + } + + $factory->create($dsn); + } +} diff --git a/Test/IncompleteDsnTestTrait.php b/Test/IncompleteDsnTestTrait.php new file mode 100644 index 0000000..fbf322a --- /dev/null +++ b/Test/IncompleteDsnTestTrait.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mailer\Test; + +use PHPUnit\Framework\Attributes\DataProvider; +use Symfony\Component\Mailer\Exception\IncompleteDsnException; +use Symfony\Component\Mailer\Transport\Dsn; + +trait IncompleteDsnTestTrait +{ + /** + * @psalm-return iterable + */ + abstract public static function incompleteDsnProvider(): iterable; + + /** + * @dataProvider incompleteDsnProvider + */ + #[DataProvider('incompleteDsnProvider')] + public function testIncompleteDsnException(Dsn $dsn) + { + $factory = $this->getFactory(); + + $this->expectException(IncompleteDsnException::class); + $factory->create($dsn); + } +} diff --git a/Test/TransportFactoryTestCase.php b/Test/TransportFactoryTestCase.php index d39839f..782bba3 100644 --- a/Test/TransportFactoryTestCase.php +++ b/Test/TransportFactoryTestCase.php @@ -11,14 +11,8 @@ namespace Symfony\Component\Mailer\Test; -use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\Mailer\Exception\IncompleteDsnException; -use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; use Symfony\Component\Mailer\Transport\Dsn; -use Symfony\Component\Mailer\Transport\TransportFactoryInterface; -use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -26,28 +20,17 @@ * A test case to ease testing Transport Factory. * * @author Konstantin Myakshin + * + * @deprecated since Symfony 7.2, use AbstractTransportFactoryTestCase instead */ -abstract class TransportFactoryTestCase extends TestCase +abstract class TransportFactoryTestCase extends AbstractTransportFactoryTestCase { - protected const USER = 'u$er'; - protected const PASSWORD = 'pa$s'; + use IncompleteDsnTestTrait; protected EventDispatcherInterface $dispatcher; protected HttpClientInterface $client; protected LoggerInterface $logger; - abstract public function getFactory(): TransportFactoryInterface; - - /** - * @psalm-return iterable - */ - abstract public static function supportsProvider(): iterable; - - /** - * @psalm-return iterable - */ - abstract public static function createProvider(): iterable; - /** * @psalm-return iterable */ @@ -64,59 +47,6 @@ public static function incompleteDsnProvider(): iterable return []; } - /** - * @dataProvider supportsProvider - */ - #[DataProvider('supportsProvider')] - public function testSupports(Dsn $dsn, bool $supports) - { - $factory = $this->getFactory(); - - $this->assertSame($supports, $factory->supports($dsn)); - } - - /** - * @dataProvider createProvider - */ - #[DataProvider('createProvider')] - public function testCreate(Dsn $dsn, TransportInterface $transport) - { - $factory = $this->getFactory(); - - $this->assertEquals($transport, $factory->create($dsn)); - if (str_contains('smtp', $dsn->getScheme())) { - $this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport); - } - } - - /** - * @dataProvider unsupportedSchemeProvider - */ - #[DataProvider('unsupportedSchemeProvider')] - public function testUnsupportedSchemeException(Dsn $dsn, ?string $message = null) - { - $factory = $this->getFactory(); - - $this->expectException(UnsupportedSchemeException::class); - if (null !== $message) { - $this->expectExceptionMessage($message); - } - - $factory->create($dsn); - } - - /** - * @dataProvider incompleteDsnProvider - */ - #[DataProvider('incompleteDsnProvider')] - public function testIncompleteDsnException(Dsn $dsn) - { - $factory = $this->getFactory(); - - $this->expectException(IncompleteDsnException::class); - $factory->create($dsn); - } - protected function getDispatcher(): EventDispatcherInterface { return $this->dispatcher ??= $this->createMock(EventDispatcherInterface::class); diff --git a/Tests/Transport/NullTransportFactoryTest.php b/Tests/Transport/NullTransportFactoryTest.php index b28935a..e738373 100644 --- a/Tests/Transport/NullTransportFactoryTest.php +++ b/Tests/Transport/NullTransportFactoryTest.php @@ -13,13 +13,13 @@ use Psr\Log\NullLogger; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Mailer\Test\TransportFactoryTestCase; +use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase; use Symfony\Component\Mailer\Transport\Dsn; use Symfony\Component\Mailer\Transport\NullTransport; use Symfony\Component\Mailer\Transport\NullTransportFactory; use Symfony\Component\Mailer\Transport\TransportFactoryInterface; -class NullTransportFactoryTest extends TransportFactoryTestCase +class NullTransportFactoryTest extends AbstractTransportFactoryTestCase { public function getFactory(): TransportFactoryInterface { @@ -41,4 +41,9 @@ public static function createProvider(): iterable new NullTransport(null, new NullLogger()), ]; } + + public static function unsupportedSchemeProvider(): iterable + { + yield [new Dsn('smtp', 'localhost')]; + } } diff --git a/Tests/Transport/SendmailTransportFactoryTest.php b/Tests/Transport/SendmailTransportFactoryTest.php index a3d08f5..3ef9cc9 100644 --- a/Tests/Transport/SendmailTransportFactoryTest.php +++ b/Tests/Transport/SendmailTransportFactoryTest.php @@ -13,13 +13,13 @@ use Psr\Log\NullLogger; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Mailer\Test\TransportFactoryTestCase; +use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase; use Symfony\Component\Mailer\Transport\Dsn; use Symfony\Component\Mailer\Transport\SendmailTransport; use Symfony\Component\Mailer\Transport\SendmailTransportFactory; use Symfony\Component\Mailer\Transport\TransportFactoryInterface; -class SendmailTransportFactoryTest extends TransportFactoryTestCase +class SendmailTransportFactoryTest extends AbstractTransportFactoryTestCase { public function getFactory(): TransportFactoryInterface { diff --git a/Tests/Transport/Smtp/EsmtpTransportFactoryTest.php b/Tests/Transport/Smtp/EsmtpTransportFactoryTest.php index 442583a..b21e4ae 100644 --- a/Tests/Transport/Smtp/EsmtpTransportFactoryTest.php +++ b/Tests/Transport/Smtp/EsmtpTransportFactoryTest.php @@ -13,14 +13,14 @@ use Psr\Log\NullLogger; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Mailer\Test\TransportFactoryTestCase; +use Symfony\Component\Mailer\Test\AbstractTransportFactoryTestCase; use Symfony\Component\Mailer\Transport\Dsn; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory; use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; use Symfony\Component\Mailer\Transport\TransportFactoryInterface; -class EsmtpTransportFactoryTest extends TransportFactoryTestCase +class EsmtpTransportFactoryTest extends AbstractTransportFactoryTestCase { public function getFactory(): TransportFactoryInterface { @@ -181,4 +181,9 @@ public static function createProvider(): iterable $transport, ]; } + + public static function unsupportedSchemeProvider(): iterable + { + yield [new Dsn('null', '')]; + } } diff --git a/Transport/Smtp/EsmtpTransportFactory.php b/Transport/Smtp/EsmtpTransportFactory.php index 9df0b95..492e78a 100644 --- a/Transport/Smtp/EsmtpTransportFactory.php +++ b/Transport/Smtp/EsmtpTransportFactory.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Mailer\Transport\Smtp; +use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; use Symfony\Component\Mailer\Transport\AbstractTransportFactory; use Symfony\Component\Mailer\Transport\Dsn; use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; @@ -23,6 +24,10 @@ final class EsmtpTransportFactory extends AbstractTransportFactory { public function create(Dsn $dsn): TransportInterface { + if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) { + throw new UnsupportedSchemeException($dsn, 'smtp', $this->getSupportedSchemes()); + } + $autoTls = '' === $dsn->getOption('auto_tls') || filter_var($dsn->getOption('auto_tls', true), \FILTER_VALIDATE_BOOL); $tls = 'smtps' === $dsn->getScheme() ? true : ($autoTls ? null : false); $port = $dsn->getPort(0);