SendTestCase: implement use of the `$propertyChanges` property

The `SendTestCase` gets the values of the properties to be set from the `testbootstrap.php` file.

This introduces a `private` property to map the field names used in `$_REQUEST` to the properties in the `PHPMailer` class and adds logic to the overloaded `set_up()` method to fill the `$propertyChanges` TestCase property. The actual setting of the properties in the `PHPMailer` instance is deferred to the underlying `TestCase` parent class.

Includes adding support for presetting the `bcc` value for feature completeness.

Overloading and/or adding to the `$propertyChanges` array from concrete test cases is, of course, supported, so if individual tests need additional presetting of properties, the same logic as mentioned in the previous commit can be used.
This commit is contained in:
jrfnl 2021-07-03 05:14:40 +02:00
parent f9ce138ff7
commit 218fd13c88
1 changed files with 56 additions and 28 deletions

View File

@ -21,6 +21,23 @@ use PHPMailer\Test\PreSendTestCase;
abstract class SendTestCase extends PreSendTestCase
{
/**
* Translation map for supported $REQUEST keys to the property name in the PHPMailer class.
*
* @var array
*/
private $requestKeys = [
'mail_from' => 'From',
'mail_host' => 'Host',
'mail_port' => 'Port',
'mail_useauth' => 'SMTPAuth',
'mail_username' => 'Username',
'mail_userpass' => 'Password',
'mail_to' => 'to',
'mail_cc' => 'cc',
'mail_bcc' => 'bcc',
];
/**
* Run before each test is started.
*/
@ -32,36 +49,45 @@ abstract class SendTestCase extends PreSendTestCase
include \PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php'; // Overrides go in here.
}
if (array_key_exists('mail_from', $_REQUEST)) {
$this->Mail->From = $_REQUEST['mail_from'];
}
if (array_key_exists('mail_host', $_REQUEST)) {
$this->Mail->Host = $_REQUEST['mail_host'];
}
if (array_key_exists('mail_port', $_REQUEST)) {
$this->Mail->Port = $_REQUEST['mail_port'];
}
if (array_key_exists('mail_useauth', $_REQUEST)) {
$this->Mail->SMTPAuth = $_REQUEST['mail_useauth'];
}
if (array_key_exists('mail_username', $_REQUEST)) {
$this->Mail->Username = $_REQUEST['mail_username'];
}
if (array_key_exists('mail_userpass', $_REQUEST)) {
$this->Mail->Password = $_REQUEST['mail_userpass'];
}
if (array_key_exists('mail_to', $_REQUEST)) {
$this->setAddress($_REQUEST['mail_to'], 'Test User', 'to');
}
if (array_key_exists('mail_cc', $_REQUEST) && $_REQUEST['mail_cc'] !== '') {
$this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
/*
* Process the $REQUEST values and add them to the list of properties
* to change at class initialization.
*/
foreach ($this->requestKeys as $requestKey => $phpmailerKey) {
if (array_key_exists($requestKey, $_REQUEST) === false) {
continue;
}
switch ($requestKey) {
case 'mail_to':
$this->propertyChanges[$phpmailerKey] = [
'address' => $_REQUEST[$requestKey],
'name' => 'Test User',
];
break;
case 'mail_cc':
$this->propertyChanges[$phpmailerKey] = [
'address' => $_REQUEST[$requestKey],
'name' => 'Carbon User',
];
break;
case 'mail_bcc':
$this->propertyChanges[$phpmailerKey] = [
'address' => $_REQUEST[$requestKey],
'name' => 'Blind Carbon User',
];
break;
default:
$this->propertyChanges[$phpmailerKey] = $_REQUEST[$requestKey];
break;
}
}
if ($this->Mail->Host != '') {
$this->Mail->isSMTP();
} else {
$this->Mail->isMail();
}
// Initialize the PHPMailer class.
parent::set_up();
}
}
/*
@ -81,6 +107,8 @@ abstract class SendTestCase extends PreSendTestCase
* <br/>
* Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
* <br/>
* Bcc Address: <input type="text" size="50" name="mail_bcc" value="<?php echo get("mail_bcc"); ?>"/>
* <br/>
* SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
* <p/>
* <input type="submit" value="Run Test"/>