Implement base configuration

This commit is contained in:
Oleg Voronkovich 2023-02-20 17:47:19 +03:00
parent 2fc807cf0c
commit 53442cc3f0
2 changed files with 68 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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');
}
}