Skip to content

Commit

Permalink
Fix reading/getting env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Sep 9, 2024
1 parent 73dbab9 commit 55d68b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/PHPMailerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use PHPMailer\PHPMailer\DSNConfigurator;
use PHPMailer\PHPMailer\PHPMailer;

use function add_action;
use function constant;
use function defined;
use function explode;
use function getenv;

/**
* Configures PHPMailer via environment variables.
Expand Down Expand Up @@ -99,7 +97,8 @@ private function configureDKIM(PHPMailer $mailer): void

private function getConfig(string $key, $default = null)
{
if ($value = getenv($key)) {
$value = $_ENV[$key] ?? null;
if ($value !== null) {
return $value;
}

Expand Down
35 changes: 18 additions & 17 deletions tests/PHPMailerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use PopArtDesign\WordPressMailerDSN\PHPMailerConfigurator;


/**
* @covers PHPMailerConfigurator
* @runTestsInSeparateProcesses
Expand All @@ -20,7 +21,7 @@ public function testConfiguresUsingMailerDsnEnv()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DSN=smtps://pop:[email protected]:587?SMTPDebug=3&Timeout=1000');
$_ENV['MAILER_DSN'] = 'smtps://pop:[email protected]:587?SMTPDebug=3&Timeout=1000';

$configurator->configure($mailer);

Expand Down Expand Up @@ -51,7 +52,7 @@ public function testGivesPreferenceToEnvsOverConstants()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DSN=smtps://pop:[email protected]:587?SMTPDebug=3&Timeout=1000');
$_ENV['MAILER_DSN'] = 'smtps://pop:[email protected]:587?SMTPDebug=3&Timeout=1000';
define('MAILER_DSN', 'sendmail://localhost?Sendmail=/usr/sbin/sendmail%20-oi%20-t');

$configurator->configure($mailer);
Expand All @@ -64,8 +65,8 @@ public function testConfiguresDebugUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DEBUG=3');
putenv('MAILER_DEBUG_OUTPUT=error_log');
$_ENV['MAILER_DEBUG'] = 3;
$_ENV['MAILER_DEBUG_OUTPUT'] = 'error_log';

$configurator->configure($mailer);

Expand All @@ -78,9 +79,9 @@ public function testConfiguresCommonUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_FROM=[email protected]');
putenv('MAILER_FROM_NAME=Oleg Voronkovich');
putenv('MAILER_SENDER=[email protected]');
$_ENV['MAILER_FROM'] = '[email protected]';
$_ENV['MAILER_FROM_NAME'] = 'Oleg Voronkovich';
$_ENV['MAILER_SENDER'] = '[email protected]';

$configurator->configure($mailer);

Expand All @@ -94,12 +95,12 @@ public function testConfiguresDkimUsingEnvs()
$configurator = new PHPMailerConfigurator();
$mailer = new PHPMailer();

putenv('MAILER_DKIM_PRIVATE=/tmp/private.key');
putenv('MAILER_DKIM_PRIVATE_STRING=private');
putenv('MAILER_DKIM_PASSPHRASE=secret');
putenv('MAILER_DKIM_SELECTOR=mailer');
putenv('MAILER_DKIM_DOMAIN=popartdesign.ru');
putenv('MAILER_DKIM_IDENTITY=[email protected]');
$_ENV['MAILER_DKIM_PRIVATE'] = '/tmp/private.key';
$_ENV['MAILER_DKIM_PRIVATE_STRING'] = 'private';
$_ENV['MAILER_DKIM_PASSPHRASE'] = 'secret';
$_ENV['MAILER_DKIM_SELECTOR'] = 'mailer';
$_ENV['MAILER_DKIM_DOMAIN'] = 'popartdesign.ru';
$_ENV['MAILER_DKIM_IDENTITY'] = '[email protected]';

$configurator->configure($mailer);

Expand All @@ -118,10 +119,10 @@ public function testConfiguresDkimDomainAndIdentityWithDefaultValues()

$mailer->From = '[email protected]';

putenv('MAILER_DKIM_PRIVATE=/tmp/private.key');
putenv('MAILER_DKIM_PRIVATE_STRING=private');
putenv('MAILER_DKIM_PASSPHRASE=secret');
putenv('MAILER_DKIM_SELECTOR=mailer');
$_ENV['MAILER_DKIM_PRIVATE'] = '/tmp/private.key';
$_ENV['MAILER_DKIM_PRIVATE_STRING'] = 'private';
$_ENV['MAILER_DKIM_PASSPHRASE'] = 'secret';
$_ENV['MAILER_DKIM_SELECTOR'] = 'mailer';

$configurator->configure($mailer);

Expand Down

0 comments on commit 55d68b0

Please sign in to comment.