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

TestCase: make the property pre-setting more flexible

This commit makes the property setting in the `TestCase::set_up()` more flexible by combining an overloadable property `$propertyChanges` and a `foreach` loop to set the actual property values.

Concrete test classes can either overload the `$propertyChanges` property with their own version or can add to the default setup using the following pattern:
```php
protected function set_up()
{
    $this->propertyChanges['additional_key'] = 'value';
    // Add more properties...

    parent::set_up();
}
```
This commit is contained in:
jrfnl 2021-07-03 04:58:01 +02:00
parent d7b048cf64
commit 5620c873d9

View File

@ -35,6 +35,24 @@ abstract class TestCase extends PolyfillTestCase
*/
const USE_EXCEPTIONS = null;
/**
* Property names and their values for the test instance of the PHPMailer class.
*
* These (public) properties will be set in the `set_up()` method.
*
* This property can be enhanced/overloaded in concrete test classes to change the presets
* or add additional properties.
*
* It is the responsibility of the individual test classes to ensure that
* property values of the correct type are passed.
*
* @var array Key is the property name, value the desired value for the PHPMailer instance.
*/
protected $propertyChanges = [
'SMTPDebug' => SMTP::DEBUG_CONNECTION, // Full debug output.
'Debugoutput' => ['PHPMailer\Test\DebugLogTestListener', 'debugLog'],
];
/**
* Holds the PHPMailer instance.
*
@ -98,8 +116,20 @@ abstract class TestCase extends PolyfillTestCase
$this->Mail = new PHPMailer();
}
$this->Mail->SMTPDebug = SMTP::DEBUG_CONNECTION; // Full debug output.
$this->Mail->Debugoutput = ['PHPMailer\Test\DebugLogTestListener', 'debugLog'];
// Set initial property values.
foreach ($this->propertyChanges as $key => $value) {
if ($key === 'to' || $key === 'cc' || $key === 'bcc' || $key === 'ReplyTo') {
if (is_array($value) && isset($value['address'], $value['name'])) {
$this->setAddress($value['address'], $value['name'], $key);
} elseif (is_string($value)) {
$this->setAddress($value, '', $key);
}
continue;
}
$this->Mail->{$key} = $value;
}
if ($this->Mail->Host != '') {
$this->Mail->isSMTP();