* @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\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Yoast\PHPUnitPolyfills\TestCases\TestCase as PolyfillTestCase;
/**
* PHPMailer - Base test class.
*/
abstract class TestCase extends PolyfillTestCase
{
/**
* Holds the PHPMailer instance.
*
* @var PHPMailer
*/
protected $Mail;
/**
* Holds the SMTP mail host.
*
* @var string
*/
private $Host = '';
/**
* Holds the change log.
*
* @var string[]
*/
private $ChangeLog = [];
/**
* Holds the note log.
*
* @var string[]
*/
private $NoteLog = [];
/**
* PIDs of any processes we need to kill.
*
* @var array
*/
protected $pids = [];
/**
* Run before each test class.
*/
public static function set_up_before_class()
{
require_once __DIR__ . '/validators.php';
if (defined('PHPMAILER_INCLUDE_DIR') === false) {
/*
* Set up default include path.
* Default to the dir above the test dir, i.e. the project home dir.
*/
define('PHPMAILER_INCLUDE_DIR', dirname(__DIR__));
}
}
/**
* Run before each test is started.
*/
protected function set_up()
{
if (file_exists(\PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php')) {
include \PHPMAILER_INCLUDE_DIR . '/test/testbootstrap.php'; //Overrides go in here
}
$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';
}
$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->Mail->addReplyTo('no_reply@phpmailer.example.com', 'Reply Guy');
$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');
}
}
/**
* Run after each test is completed.
*/
protected function tear_down()
{
//Clean global variables
$this->Mail = null;
$this->ChangeLog = [];
$this->NoteLog = [];
foreach ($this->pids as $pid) {
$p = escapeshellarg($pid);
shell_exec("ps $p && kill -TERM $p");
}
}
/**
* Build the body of the message in the appropriate format.
*/
protected function buildBody()
{
$this->checkChanges();
//Determine line endings for message
if ('text/html' === $this->Mail->ContentType || $this->Mail->AltBody !== '') {
$eol = "
\r\n";
$bullet_start = '