|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\Mailer\Tests\Transport; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\Mailer\Exception\UnsupportedSchemeException; |
| 7 | +use Symfony\Component\Mailer\Transport\Dsn; |
| 8 | +use Symfony\Component\Mailer\Transport\NativeTransportFactory; |
| 9 | +use Symfony\Component\Mailer\Transport\SendmailTransport; |
| 10 | +use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; |
| 11 | +use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; |
| 12 | +use Symfony\Component\Mailer\Transport\TransportInterface; |
| 13 | + |
| 14 | +final class NativeTransportFactoryTest extends TestCase |
| 15 | +{ |
| 16 | + public static $fakeConfiguration = []; |
| 17 | + |
| 18 | + public static function setUpBeforeClass(): void |
| 19 | + { |
| 20 | + parent::setUpBeforeClass(); |
| 21 | + |
| 22 | + $namespace = str_replace('\\Tests\\', '\\', __NAMESPACE__); |
| 23 | + |
| 24 | + $current = static::class; |
| 25 | + |
| 26 | + $eval = <<<EOT |
| 27 | +namespace $namespace; |
| 28 | +
|
| 29 | +function ini_get(\$key) |
| 30 | +{ |
| 31 | + \$vals = \\$current::\$fakeConfiguration; |
| 32 | + return \$vals[\$key] ?? ''; |
| 33 | +} |
| 34 | +EOT; |
| 35 | + eval($eval); |
| 36 | + } |
| 37 | + |
| 38 | + public function provideCreateWithNotSupportedScheme(): \Generator |
| 39 | + { |
| 40 | + yield ['sendmail://default']; |
| 41 | + |
| 42 | + if (PHP_OS_FAMILY != 'Windows') { |
| 43 | + yield ['native+smtp://default']; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @dataProvider provideCreateWithNotSupportedScheme |
| 49 | + */ |
| 50 | + public function testCreateWithNotSupportedScheme(string $dsn) |
| 51 | + { |
| 52 | + $this->expectException(UnsupportedSchemeException::class); |
| 53 | + $this->expectExceptionMessageRegExp('#The ".*" scheme is not supported#'); |
| 54 | + |
| 55 | + $sut = new NativeTransportFactory(); |
| 56 | + $sut->create(Dsn::fromString($dsn)); |
| 57 | + } |
| 58 | + |
| 59 | + public function provideCreateSendmailWithNoSendmailPath(): \Generator |
| 60 | + { |
| 61 | + if ('Linux' === PHP_OS_FAMILY) { |
| 62 | + yield ['native://default']; |
| 63 | + } |
| 64 | + |
| 65 | + yield ['native+sendmail://default']; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider provideCreateSendmailWithNoSendmailPath |
| 70 | + */ |
| 71 | + public function testCreateSendmailWithNoSendmailPath(string $dsn) |
| 72 | + { |
| 73 | + $this->expectException(\Exception::class); |
| 74 | + $this->expectExceptionMessage('sendmail_path is not configured'); |
| 75 | + |
| 76 | + $sut = new NativeTransportFactory(); |
| 77 | + $sut->create(Dsn::fromString($dsn)); |
| 78 | + } |
| 79 | + |
| 80 | + public function provideCreateSendmailWithNoHostOrNoPort(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + ['native://default', '', ''], |
| 84 | + ['native+smtp://default', '/usr/sbin/sendmail -t -i', ''], |
| 85 | + ['native://default', '', 'localhost'], |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @requires OSFAMILY Windows |
| 91 | + * |
| 92 | + * @dataProvider provideCreateSendmailWithNoHostOrNoPort |
| 93 | + */ |
| 94 | + public function testCreateSendmailWithNoHostOrNoPort(string $dsn, string $sendmaiPath, string $smtp) |
| 95 | + { |
| 96 | + $this->expectException(\Exception::class); |
| 97 | + $this->expectExceptionMessage('smtp or smtp_port is not configured'); |
| 98 | + |
| 99 | + self::$fakeConfiguration = [ |
| 100 | + 'sendmail_path' => $sendmaiPath, |
| 101 | + 'smtp' => $smtp, |
| 102 | + ]; |
| 103 | + |
| 104 | + $sut = new NativeTransportFactory(); |
| 105 | + $sut->create(Dsn::fromString($dsn)); |
| 106 | + } |
| 107 | + |
| 108 | + public function provideCreate(): \Generator |
| 109 | + { |
| 110 | + yield ['native://default', '/usr/sbin/sendmail -t -i', '', '', new SendmailTransport('/usr/sbin/sendmail -t -i')]; |
| 111 | + yield ['native+sendmail://default', '/usr/sbin/sendmail -t -i', '', '', new SendmailTransport('/usr/sbin/sendmail -t -i')]; |
| 112 | + |
| 113 | + if ('Windows' === PHP_OS_FAMILY) { |
| 114 | + $socketStream = new SocketStream(); |
| 115 | + $socketStream->setHost('myhost.tld'); |
| 116 | + $socketStream->setPort(25); |
| 117 | + $socketStream->disableTls(); |
| 118 | + yield ['native://default', '', 'myhost.tld', '25', new SmtpTransport($socketStream)]; |
| 119 | + yield ['native+smtp://default', '/usr/sbin/sendmail -t -i', 'myhost.tld', '25', new SmtpTransport($socketStream)]; |
| 120 | + |
| 121 | + $socketStreamTls = new SocketStream(); |
| 122 | + $socketStreamTls->setHost('myhost.tld'); |
| 123 | + $socketStreamTls->setPort(465); |
| 124 | + yield ['native://default', '', 'myhost.tld', '465', new SmtpTransport($socketStreamTls)]; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @dataProvider provideCreate |
| 130 | + */ |
| 131 | + public function testCreate(string $dsn, string $sendmailPath, string $smtp, string $smtpPort, TransportInterface $expectedTransport) |
| 132 | + { |
| 133 | + self::$fakeConfiguration = [ |
| 134 | + 'sendmail_path' => $sendmailPath, |
| 135 | + 'smtp' => $smtp, |
| 136 | + 'smtp_port' => $smtpPort, |
| 137 | + ]; |
| 138 | + |
| 139 | + $sut = new NativeTransportFactory(); |
| 140 | + $transport = $sut->create(Dsn::fromString($dsn)); |
| 141 | + |
| 142 | + $this->assertEquals($expectedTransport, $transport); |
| 143 | + } |
| 144 | +} |
0 commit comments