0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-19 17:42:14 +02:00

Merge pull request #2412 from jrfnl/feature/tests-split-and-improve-testcase

Tests: split `TestCase` class into three base test case classes and make more flexible
This commit is contained in:
Marcus Bointon 2021-07-06 20:17:43 +02:00 committed by GitHub
commit e372a5e85b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 276 additions and 116 deletions

View File

@ -13,12 +13,12 @@
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\SendTestCase;
/**
* Test CRAM-MD5 authentication functionality.
*/
final class AuthCRAMMD5Test extends TestCase
final class AuthCRAMMD5Test extends SendTestCase
{
/**

View File

@ -15,14 +15,14 @@ namespace PHPMailer\Test\PHPMailer;
use Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\SendTestCase;
/**
* Test DKIM signing functionality.
*
* @group dkim
*/
final class DKIMTest extends TestCase
final class DKIMTest extends SendTestCase
{
/**

View File

@ -13,7 +13,7 @@
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\PreSendTestCase;
/**
* Test setting and retrieving message ID.
@ -21,7 +21,7 @@ use PHPMailer\Test\TestCase;
* @covers \PHPMailer\PHPMailer\PHPMailer::createHeader
* @covers \PHPMailer\PHPMailer\PHPMailer::getLastMessageID
*/
final class GetLastMessageIDTest extends TestCase
final class GetLastMessageIDTest extends PreSendTestCase
{
/**

View File

@ -14,12 +14,12 @@
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\SendTestCase;
/**
* Test line length detection and handling.
*/
final class HasLineLongerThanMaxTest extends TestCase
final class HasLineLongerThanMaxTest extends SendTestCase
{
/**

View File

@ -13,12 +13,12 @@
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\PreSendTestCase;
/**
* Test ICal calendar events handling.
*/
final class ICalTest extends TestCase
final class ICalTest extends PreSendTestCase
{
/**

View File

@ -13,12 +13,12 @@
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\SendTestCase;
/**
* Test sending mail using the various available mail transport options.
*/
final class MailTransportTest extends TestCase
final class MailTransportTest extends SendTestCase
{
/**

View File

@ -16,12 +16,12 @@ namespace PHPMailer\Test\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\Test\TestCase;
use PHPMailer\Test\SendTestCase;
/**
* PHPMailer - PHP email transport unit test class.
*/
final class PHPMailerTest extends TestCase
final class PHPMailerTest extends SendTestCase
{
/**
* Check that we have loaded default test params.

47
test/PreSendTestCase.php Normal file
View File

@ -0,0 +1,47 @@
<?php
/**
* PHPMailer - Base test class.
* PHP version 5.5.
*
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
* @author Andy Prevost
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace PHPMailer\Test;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\Test\TestCase;
/**
* PHPMailer - Test class for tests which need the `PHPMailer::preSend()` method to be called.
*/
abstract class PreSendTestCase extends TestCase
{
/**
* Property names and their values for the test instance of the PHPMailer class.
*
* These 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.
*
* @var array
*/
protected $propertyChanges = [
// Generic changes.
'SMTPDebug' => SMTP::DEBUG_CONNECTION, // Full debug output.
'Debugoutput' => ['PHPMailer\Test\DebugLogTestListener', 'debugLog'],
// Minimal set of properties which are needed for the preSend() command to succeed.
'From' => 'unit_test@phpmailer.example.com',
'to' => [
'address' => 'somebody@example.com',
'name' => 'Test User',
],
];
}

119
test/SendTestCase.php Normal file
View File

@ -0,0 +1,119 @@
<?php
/**
* PHPMailer - Base test class.
* PHP version 5.5.
*
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
* @author Andy Prevost
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace PHPMailer\Test;
use PHPMailer\Test\PreSendTestCase;
/**
* PHPMailer - Test class for tests which need the `PHPMailer::send()` method to be called.
*/
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.
*/
protected function set_up()
{
parent::set_up();
if (file_exists(\PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php')) {
include \PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php'; // Overrides go in here.
}
/*
* 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;
}
}
// Initialize the PHPMailer class.
parent::set_up();
}
}
/*
* This is a sample form for setting appropriate test values through a browser
* These values can also be set using a file called testbootstrap.php (not in repo) in the same folder as this script
* which is probably more useful if you run these tests a lot
* <html>
* <body>
* <h3>PHPMailer Unit Test</h3>
* By entering a SMTP hostname it will automatically perform tests with SMTP.
*
* <form name="phpmailer_unit" action=__FILE__ method="get">
* <input type="hidden" name="submitted" value="1"/>
* From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
* <br/>
* To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
* <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"/>
*
* </form>
* </body>
* </html>
*/

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.
*
@ -56,11 +74,43 @@ abstract class TestCase extends PolyfillTestCase
*/
private $NoteLog = [];
/*
* List of *public* properties which we don't want listed in the changelog
* as they will already be included in the mail/debug information
* created in `buildBody()` anyway.
*
* Note: no need to include protected or private properties as the tests don't
* have access to those anyway.
*
* @var array Key is the property name, value irrelevant.
*/
private $changelogExclude = [
// These are always set in set_up().
'SMTPDebug' => true,
'Debugoutput' => true,
// These are part of the message body anyway.
'Subject' => true,
'Body' => true,
'AltBody' => true,
'Ical' => true,
// These will always change.
'MessageID' => true,
'MessageDate' => true,
// These are always explicitly added via buildBody() anyway.
'ContentType' => true,
'CharSet' => true,
'Host' => true,
];
/**
* List of *static* properties in the PHPMailer class which _may_ be changed from within a test,
* with their default values.
*
* This list is used by the {@see `TestCase::resetStaticProperties()`} method.
* This list is used by the {@see `TestCase::resetStaticProperties()`} method, as well as
* in the {@see `TestCase::checkChanges()`} method.
*
* {@internal The default values have to be (manually) maintained here as the Reflection
* extension does not provide accurate information on the default values of static properties.}
@ -91,10 +141,6 @@ abstract class TestCase extends PolyfillTestCase
*/
protected function set_up()
{
if (file_exists(\PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php')) {
include \PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php'; // Overrides go in here.
}
// Initialize the PHPMailer class.
if (is_bool(static::USE_EXCEPTIONS)) {
$this->Mail = new PHPMailer(static::USE_EXCEPTIONS);
@ -102,58 +148,26 @@ 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'];
$this->Mail->Priority = 3;
$this->Mail->Encoding = '8bit';
$this->Mail->CharSet = PHPMailer::CHARSET_ISO88591;
if (array_key_exists('mail_from', $_REQUEST)) {
$this->Mail->From = $_REQUEST['mail_from'];
} else {
$this->Mail->From = 'unit_test@phpmailer.example.com';
// 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;
}
$this->Mail->FromName = 'Unit Tester';
$this->Mail->Sender = '';
$this->Mail->Subject = 'Unit Test';
$this->Mail->Body = '';
$this->Mail->AltBody = '';
$this->Mail->WordWrap = 0;
if (array_key_exists('mail_host', $_REQUEST)) {
$this->Mail->Host = $_REQUEST['mail_host'];
} else {
$this->Mail->Host = 'mail.example.com';
}
if (array_key_exists('mail_port', $_REQUEST)) {
$this->Mail->Port = $_REQUEST['mail_port'];
} else {
$this->Mail->Port = 25;
}
$this->Mail->Helo = 'localhost.localdomain';
$this->Mail->SMTPAuth = false;
$this->Mail->Username = '';
$this->Mail->Password = '';
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'];
}
$this->setAddress('no_reply@phpmailer.example.com', 'Reply Guy', 'ReplyTo');
$this->Mail->Sender = 'unit_test@phpmailer.example.com';
if ($this->Mail->Host != '') {
$this->Mail->isSMTP();
} else {
$this->Mail->isMail();
}
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');
}
}
/**
@ -291,32 +305,37 @@ abstract class TestCase extends PolyfillTestCase
*/
protected function checkChanges()
{
if (3 != $this->Mail->Priority) {
$this->addChange('Priority', $this->Mail->Priority);
}
if (PHPMailer::ENCODING_8BIT !== $this->Mail->Encoding) {
$this->addChange('Encoding', $this->Mail->Encoding);
}
if (PHPMailer::CHARSET_ISO88591 !== $this->Mail->CharSet) {
$this->addChange('CharSet', $this->Mail->CharSet);
}
if ('' != $this->Mail->Sender) {
$this->addChange('Sender', $this->Mail->Sender);
}
if (0 != $this->Mail->WordWrap) {
$this->addChange('WordWrap', $this->Mail->WordWrap);
}
if ('mail' !== $this->Mail->Mailer) {
$this->addChange('Mailer', $this->Mail->Mailer);
}
if (25 != $this->Mail->Port) {
$this->addChange('Port', $this->Mail->Port);
}
if ('localhost.localdomain' !== $this->Mail->Helo) {
$this->addChange('Helo', $this->Mail->Helo);
}
if ($this->Mail->SMTPAuth) {
$this->addChange('SMTPAuth', 'true');
// Get the default values of all public properties.
$defaults = get_class_vars(PHPMailer::class);
foreach ($defaults as $propertyName => $value) {
if (isset($this->changelogExclude[$propertyName])) {
continue;
}
if (isset($this->PHPMailerStaticProps[$propertyName])) {
// Nested static access is not supported in PHP < 7.0, so we need an interim variable.
$mail = $this->Mail;
if ($mail::${$propertyName} !== $this->PHPMailerStaticProps[$propertyName]) {
$this->addChange($propertyName, var_export($mail::${$propertyName}, true));
}
continue;
}
// Check against the TestCase specific defaults.
if (
isset($this->propertyChanges[$propertyName])
&& $this->Mail->{$propertyName} !== $this->propertyChanges[$propertyName]
) {
$this->addChange($propertyName, var_export($this->Mail->{$propertyName}, true));
continue;
}
// Check against the PHPMailer class defaults.
if ($this->Mail->{$propertyName} !== $value) {
$this->addChange($propertyName, var_export($this->Mail->{$propertyName}, true));
}
}
}
@ -366,28 +385,3 @@ abstract class TestCase extends PolyfillTestCase
return false;
}
}
/*
* This is a sample form for setting appropriate test values through a browser
* These values can also be set using a file called testbootstrap.php (not in repo) in the same folder as this script
* which is probably more useful if you run these tests a lot
* <html>
* <body>
* <h3>PHPMailer Unit Test</h3>
* By entering a SMTP hostname it will automatically perform tests with SMTP.
*
* <form name="phpmailer_unit" action=__FILE__ method="get">
* <input type="hidden" name="submitted" value="1"/>
* From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
* <br/>
* To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
* <br/>
* Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
* <br/>
* SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
* <p/>
* <input type="submit" value="Run Test"/>
*
* </form>
* </body>
* </html>
*/