From 53442cc3f0b6178d7514dfa791a63bb4b66efbff Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Mon, 20 Feb 2023 17:47:19 +0300 Subject: [PATCH] Implement base configuration --- src/DSNConfigurator.php | 40 ++++++++++++++++++++++++++ test/PHPMailer/DSNConfiguratorTest.php | 28 ++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/DSNConfigurator.php b/src/DSNConfigurator.php index 6da7f781..8cea0876 100644 --- a/src/DSNConfigurator.php +++ b/src/DSNConfigurator.php @@ -40,6 +40,8 @@ class DSNConfigurator { $config = $this->parseDSN($dsn); + $this->applyConfig($mailer, $config); + return $mailer; } @@ -68,4 +70,42 @@ class DSNConfigurator return $config; } + + /** + * Apply config to mailer. + * + * @param PHPMailer $mailer PHPMailer instance + * @param array $config Configuration + * + * @throws Exception If scheme is invalid + * + * @return PHPMailer + */ + private function applyConfig(PHPMailer $mailer, $config) + { + switch ($config['scheme']) { + case 'mail': + $mailer->isMail(); + break; + case 'sendmail': + $mailer->isSendmail(); + break; + case 'qmail': + $mailer->isQmail(); + break; + case 'smtp': + case 'smtps': + $mailer->isSMTP(); + break; + default: + throw new Exception( + sprintf( + 'Invalid scheme: "%s". Allowed values: "mail", "sendmail", "qmail", "smtp", "smtps".', + $config['scheme'], + ) + ); + } + + return $mailer; + } } diff --git a/test/PHPMailer/DSNConfiguratorTest.php b/test/PHPMailer/DSNConfiguratorTest.php index 1a68c094..f6ac6a1c 100644 --- a/test/PHPMailer/DSNConfiguratorTest.php +++ b/test/PHPMailer/DSNConfiguratorTest.php @@ -28,4 +28,32 @@ final class DSNConfiguratorTest extends TestCase $configurator->configure($this->Mail, 'localhost'); } + + public function testInvalidScheme() + { + $configurator = new DSNConfigurator(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Invalid scheme: "ftp".'); + + $configurator->configure($this->Mail, 'ftp://localhost'); + } + + public function testConfigureSendmail() + { + $configurator = new DSNConfigurator(); + + $configurator->configure($this->Mail, 'sendmail://localhost'); + + $this->assertEquals($this->Mail->Mailer, 'sendmail'); + } + + public function testConfigureSmtp() + { + $configurator = new DSNConfigurator(); + + $configurator->configure($this->Mail, 'smtp://localhost'); + + $this->assertEquals($this->Mail->Mailer, 'smtp'); + } }