AbstractTransportFactoryTestCase.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer\Test;
  11. use PHPUnit\Framework\Attributes\DataProvider;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
  14. use Symfony\Component\Mailer\Transport\Dsn;
  15. use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
  16. use Symfony\Component\Mailer\Transport\TransportInterface;
  17. abstract class AbstractTransportFactoryTestCase extends TestCase
  18. {
  19. protected const USER = 'u$er';
  20. protected const PASSWORD = 'pa$s';
  21. abstract public function getFactory(): TransportFactoryInterface;
  22. /**
  23. * @psalm-return iterable<array{0: Dsn, 1: bool}>
  24. */
  25. abstract public static function supportsProvider(): iterable;
  26. /**
  27. * @psalm-return iterable<array{0: Dsn, 1: TransportInterface}>
  28. */
  29. abstract public static function createProvider(): iterable;
  30. /**
  31. * @psalm-return iterable<array{0: Dsn, 1?: string|null}>
  32. */
  33. abstract public static function unsupportedSchemeProvider(): iterable;
  34. /**
  35. * @dataProvider supportsProvider
  36. */
  37. #[DataProvider('supportsProvider')]
  38. public function testSupports(Dsn $dsn, bool $supports)
  39. {
  40. $factory = $this->getFactory();
  41. $this->assertSame($supports, $factory->supports($dsn));
  42. }
  43. /**
  44. * @dataProvider createProvider
  45. */
  46. #[DataProvider('createProvider')]
  47. public function testCreate(Dsn $dsn, TransportInterface $transport)
  48. {
  49. $factory = $this->getFactory();
  50. $this->assertEquals($transport, $factory->create($dsn));
  51. if (str_contains('smtp', $dsn->getScheme())) {
  52. $this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport);
  53. }
  54. }
  55. /**
  56. * @dataProvider unsupportedSchemeProvider
  57. */
  58. #[DataProvider('unsupportedSchemeProvider')]
  59. public function testUnsupportedSchemeException(Dsn $dsn, ?string $message = null)
  60. {
  61. $factory = $this->getFactory();
  62. $this->expectException(UnsupportedSchemeException::class);
  63. if (null !== $message) {
  64. $this->expectExceptionMessage($message);
  65. }
  66. $factory->create($dsn);
  67. }
  68. }