Skip to content

Commit

Permalink
Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich committed Feb 20, 2023
1 parent ecc11e3 commit 067ff6e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
14 changes: 8 additions & 6 deletions src/DSNConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
namespace PHPMailer\PHPMailer;

/**
* Configure PHPMailer via DSN.
* Configure PHPMailer with DSN string.
*
* @author Oleg Voronkovich (voronkovich) <oleg-voronkovich@yandex.ru>
* @see https://en.wikipedia.org/wiki/Data_source_name
*
* @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
*/
class DSNConfigurator
{
/**
* Configure PHPMailer via DSN.
* Configure PHPMailer instance with DSN string.
*
* @param PHPMailer $mailer PHPMailer instance
* @param string $dsn DSN
Expand All @@ -46,13 +48,13 @@ public function configure(PHPMailer $mailer, $dsn)
}

/**
* Parse DSN.
* Parse DSN string.
*
* @param string $dsn DSN
*
* @throws Exception If DSN is mailformed
*
* @return array configruration
* @return array Configruration
*/
private function parseDSN($dsn)
{
Expand All @@ -72,7 +74,7 @@ private function parseDSN($dsn)
}

/**
* Apply config to mailer.
* Apply configuration to mailer.
*
* @param PHPMailer $mailer PHPMailer instance
* @param array $config Configuration
Expand Down
29 changes: 14 additions & 15 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,20 @@ public function __construct($exceptions = null)
$this->Debugoutput = (strpos(PHP_SAPI, 'cli') !== false ? 'echo' : 'html');
}

/**
* Create new instance configured by DSN.
*
* @param string $dsn DSN
* @param bool $exceptions Should we throw external exceptions?
*
* @return PHPMailer
*/
public static function fromDSN($dsn, $exceptions = null)
{
static $configurator = new DSNConfigurator();

return $configurator->configure(new PHPMailer($exceptions), $dsn);
}
/**
* Destructor.
*/
Expand Down Expand Up @@ -5123,19 +5137,4 @@ public function setOAuth(OAuthTokenProvider $oauth)
{
$this->oauth = $oauth;
}

/**
* Create new instance configured by DSN.
*
* @param string $dsn DSN
* @param bool $exceptions Should we throw external exceptions?
*
* @return PHPMailer
*/
public static function fromDSN($dsn, $exceptions = null)
{
static $configurator = new DSNConfigurator();

return $configurator->configure(new PHPMailer($exceptions), $dsn);
}
}
74 changes: 37 additions & 37 deletions test/PHPMailer/DSNConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testConfigureMail()

$configurator->configure($this->Mail, 'mail://localhost');

$this->assertEquals($this->Mail->Mailer, 'mail');
self::assertEquals($this->Mail->Mailer, 'mail');
}

/**
Expand All @@ -73,7 +73,7 @@ public function testConfigureSendmail()

$configurator->configure($this->Mail, 'sendmail://localhost');

$this->assertEquals($this->Mail->Mailer, 'sendmail');
self::assertEquals($this->Mail->Mailer, 'sendmail');
}

/**
Expand All @@ -85,7 +85,7 @@ public function testConfigureQmail()

$configurator->configure($this->Mail, 'qmail://localhost');

$this->assertEquals($this->Mail->Mailer, 'qmail');
self::assertEquals($this->Mail->Mailer, 'qmail');
}

/**
Expand All @@ -97,9 +97,9 @@ public function testConfigureSmtpWithoutAuthentication()

$configurator->configure($this->Mail, 'smtp://localhost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->Host, 'localhost');
$this->assertFalse($this->Mail->SMTPAuth);
self::assertEquals($this->Mail->Mailer, 'smtp');
self::assertEquals($this->Mail->Host, 'localhost');
self::assertFalse($this->Mail->SMTPAuth);
}

/**
Expand All @@ -111,12 +111,12 @@ public function testConfigureSmtpWithAuthentication()

$configurator->configure($this->Mail, 'smtp://user:pass@remotehost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->Host, 'remotehost');
self::assertEquals($this->Mail->Mailer, 'smtp');
self::assertEquals($this->Mail->Host, 'remotehost');

$this->assertTrue($this->Mail->SMTPAuth);
$this->assertEquals($this->Mail->Username, 'user');
$this->assertEquals($this->Mail->Password, 'pass');
self::assertTrue($this->Mail->SMTPAuth);
self::assertEquals($this->Mail->Username, 'user');
self::assertEquals($this->Mail->Password, 'pass');
}

/**
Expand All @@ -128,9 +128,9 @@ public function testConfigureSmtpWithoutPort()

$configurator->configure($this->Mail, 'smtp://localhost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->Host, 'localhost');
$this->assertEquals($this->Mail->Port, SMTP::DEFAULT_PORT);
self::assertEquals($this->Mail->Mailer, 'smtp');
self::assertEquals($this->Mail->Host, 'localhost');
self::assertEquals($this->Mail->Port, SMTP::DEFAULT_PORT);
}

/**
Expand All @@ -142,9 +142,9 @@ public function testConfigureSmtpWitPort()

$configurator->configure($this->Mail, 'smtp://localhost:2525');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->Host, 'localhost');
$this->assertEquals($this->Mail->Port, 2525);
self::assertEquals($this->Mail->Mailer, 'smtp');
self::assertEquals($this->Mail->Host, 'localhost');
self::assertEquals($this->Mail->Port, 2525);
}

/**
Expand All @@ -156,15 +156,15 @@ public function testConfigureSmtpsWithoutPort()

$configurator->configure($this->Mail, 'smtps://user:pass@remotehost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->SMTPSecure, PHPMailer::ENCRYPTION_STARTTLS);
self::assertEquals($this->Mail->Mailer, 'smtp');
self::assertEquals($this->Mail->SMTPSecure, PHPMailer::ENCRYPTION_STARTTLS);

$this->assertEquals($this->Mail->Host, 'remotehost');
$this->assertEquals($this->Mail->Port, SMTP::DEFAULT_SECURE_PORT);
self::assertEquals($this->Mail->Host, 'remotehost');
self::assertEquals($this->Mail->Port, SMTP::DEFAULT_SECURE_PORT);

$this->assertTrue($this->Mail->SMTPAuth);
$this->assertEquals($this->Mail->Username, 'user');
$this->assertEquals($this->Mail->Password, 'pass');
self::assertTrue($this->Mail->SMTPAuth);
self::assertEquals($this->Mail->Username, 'user');
self::assertEquals($this->Mail->Password, 'pass');
}

/**
Expand All @@ -189,10 +189,10 @@ public function testConfigureWithOptions()

$configurator->configure($this->Mail, 'sendmail://localhost?Sendmail=/usr/local/bin/sendmail&AllowEmpty=1&WordWrap=78');

$this->assertEquals($this->Mail->Mailer, 'sendmail');
$this->assertEquals($this->Mail->Sendmail, '/usr/local/bin/sendmail');
$this->assertEquals($this->Mail->AllowEmpty, true);
$this->assertEquals($this->Mail->WordWrap, 78);
self::assertEquals($this->Mail->Mailer, 'sendmail');
self::assertEquals($this->Mail->Sendmail, '/usr/local/bin/sendmail');
self::assertEquals($this->Mail->AllowEmpty, true);
self::assertEquals($this->Mail->WordWrap, 78);
}

/**
Expand All @@ -204,17 +204,17 @@ public function testShorcut()
{
$mailer = PHPMailer::fromDSN('smtps://user@gmail.com:secret@smtp.gmail.com?SMTPDebug=3&Timeout=1000');

$this->assertEquals($mailer->Mailer, 'smtp');
$this->assertEquals($mailer->SMTPSecure, PHPMailer::ENCRYPTION_STARTTLS);
self::assertEquals($mailer->Mailer, 'smtp');
self::assertEquals($mailer->SMTPSecure, PHPMailer::ENCRYPTION_STARTTLS);

$this->assertEquals($mailer->Host, 'smtp.gmail.com');
$this->assertEquals($mailer->Port, SMTP::DEFAULT_SECURE_PORT);
self::assertEquals($mailer->Host, 'smtp.gmail.com');
self::assertEquals($mailer->Port, SMTP::DEFAULT_SECURE_PORT);

$this->assertTrue($mailer->SMTPAuth);
$this->assertEquals($mailer->Username, 'user@gmail.com');
$this->assertEquals($mailer->Password, 'secret');
self::assertTrue($mailer->SMTPAuth);
self::assertEquals($mailer->Username, 'user@gmail.com');
self::assertEquals($mailer->Password, 'secret');

$this->assertEquals($mailer->SMTPDebug, 3);
$this->assertEquals($mailer->Timeout, 1000);
self::assertEquals($mailer->SMTPDebug, 3);
self::assertEquals($mailer->Timeout, 1000);
}
}

0 comments on commit 067ff6e

Please sign in to comment.