0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 01:52:15 +02:00

TestCase: add support for initializing PHPMailer with exceptions

The `PHPMailer::__construct()` method has an optional `$exceptions` property to throw exceptions on errors.

Up to now, the `PHPMailer` instance created by the `set_up()` would not pass any parameters, effectively instantiating `PHPMailer` without turning on exceptions.

In a test situation it may be useful for tests to test the behaviour of a method _with_ and _without_ the exceptions option and to test that certain exceptions are thrown and throw the correct message.

With that in mind, I'm introducing a `USE_EXCEPTIONS` class constant to the `TestCase` which can be overloaded in individual test classes and will be used by the `set_up()` method to determine whether it will be instantiated with exceptions or not.

Includes introducing an overload of the class constant in one of the test class for which it would seem appropriate at this time.
Note: this does mean that the `testDKIMSigningMail()` test will show as errored instead of failed if no SMTP connection could be made, but that IMO is the more correct status anyhow.
This commit is contained in:
jrfnl 2021-06-29 14:33:29 +02:00
parent af68fb202a
commit b97983fcf6
2 changed files with 27 additions and 3 deletions

View File

@ -25,6 +25,13 @@ use PHPMailer\Test\TestCase;
final class DKIMTest extends TestCase
{
/**
* Whether or not to initialize the PHPMailer object to throw exceptions.
*
* @var bool|null
*/
const USE_EXCEPTIONS = true;
const PRIVATE_KEY_FILE = 'dkim_private.pem';
/**
@ -250,7 +257,6 @@ final class DKIMTest extends TestCase
$this->expectException(Exception::class);
$this->expectExceptionMessage('Extension missing: openssl');
$mail = new PHPMailer(true);
$mail->DKIM_Sign('foo');
$this->Mail->DKIM_Sign('foo');
}
}

View File

@ -22,6 +22,17 @@ use Yoast\PHPUnitPolyfills\TestCases\TestCase as PolyfillTestCase;
*/
abstract class TestCase extends PolyfillTestCase
{
/**
* Whether or not to initialize the PHPMailer object to throw exceptions.
*
* Overload this constant in a concrete test class and set the value to `true`
* to initialize PHPMailer with Exceptions turned on.
*
* @var bool|null
*/
const USE_EXCEPTIONS = null;
/**
* Holds the PHPMailer instance.
*
@ -81,7 +92,14 @@ abstract class TestCase extends PolyfillTestCase
if (file_exists(\PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php')) {
include \PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php'; //Overrides go in here
}
$this->Mail = new PHPMailer();
// Initialize the PHPMailer class.
if (is_bool(static::USE_EXCEPTIONS)) {
$this->Mail = new PHPMailer(static::USE_EXCEPTIONS);
} else {
$this->Mail = new PHPMailer();
}
$this->Mail->SMTPDebug = SMTP::DEBUG_CONNECTION; //Full debug output
$this->Mail->Debugoutput = ['PHPMailer\Test\DebugLogTestListener', 'debugLog'];
$this->Mail->Priority = 3;