<commit_before><?php

/**
 * PHPMailer - Base test class.
 * PHP version 5.5.
 *
 * @author    <NAME> <<EMAIL>>
 * @author    <NAME>
 * @copyright 2012 - 2020 <NAME>
 * @copyright 2004 - 2009 <NAME>
 * @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
{

    /**
     * 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.
     *
     * @var PHPMailer
     */
    protected $Mail;

    /**
     * Holds the change log.
     *
     * @var string[]
     */
    private $ChangeLog = [];

    /**
     * Holds the note log.
     *
     * @var string[]
     */
    private $NoteLog = [];

    /**
     * List of *public static* properties in the PHPMailer class with their default values.
     *
     * This list is used by the `set_up()` method.
     *
     * @var array Key is the property name, value the default as per the PHPMailer class.
     */
    private $PHPMailerStaticProps = [
        'validator' => 'php',
    ];

    /**
     * Run before each test class.
     */
    public static function set_up_before_class()
    {
        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()
    {
        // Make sure that public static variables contain their default values at the start of each test.
        foreach ($this->PHPMailerStaticProps as $key => $value) {
            PHPMailer::${$key} = $value;
        }

        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);
        } 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;
        $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 = '<EMAIL>';
        }
        $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('<EMAIL>', 'Reply Guy', 'ReplyTo');
        $this->Mail->Sender = '<EMAIL>';
        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 test class native properties between tests.
        $this->Mail = null;
        $this->ChangeLog = [];
        $this->NoteLog = [];
    }
<commit_msg>TestCase: iterate on resetting of static properties<commit_after><?php

/**
 * PHPMailer - Base test class.
 * PHP version 5.5.
 *
 * @author    <NAME> <<EMAIL>>
 * @author    <NAME>
 * @copyright 2012 - 2020 <NAME>
 * @copyright 2004 - 2009 <NAME>
 * @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 ReflectionClass;
use ReflectionProperty;
use Yoast\PHPUnitPolyfills\TestCases\TestCase as PolyfillTestCase;

/**
 * PHPMailer - Base test class.
 */
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.
     *
     * @var PHPMailer
     */
    protected $Mail;

    /**
     * Holds the change log.
     *
     * @var string[]
     */
    private $ChangeLog = [];

    /**
     * Holds the note log.
     *
     * @var string[]
     */
    private $NoteLog = [];

    /**
     * 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.
     *
     * {@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.}
     *
     * @var array Key is the property name, value the default as per the PHPMailer class.
     */
    private $PHPMailerStaticProps = [
        'LE'        => PHPMailer::CRLF,
        'validator' => 'php',
    ];

    /**
     * Run before each test class.
     */
    public static function set_up_before_class()
    {
        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.
        }

        // 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;
        $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 = '<EMAIL>';
        }
        $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('<EMAIL>', 'Reply Guy', 'ReplyTo');
        $this->Mail->Sender = '<EMAIL>';
        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()
    {
        // Make sure that any changes to static variables are undone after each test.
        $this->resetStaticProperties();

        // Clean test class native properties between tests.
        $this->Mail = null;
        $this->ChangeLog = [];
        $this->NoteLog = [];
    }

    /**
     * Reset the static properties in the PHPMailer class to their default values.
     */
    protected function resetStaticProperties()
    {
        $reflClass        = new ReflectionClass(PHPMailer::class);
        $staticPropValues = $reflClass->getStaticProperties();

        foreach ($this->PHPMailerStaticProps as $name => $default) {
            if (isset($staticPropValues[$name]) && $staticPropValues[$name] === $default) {
                continue;
            }

            self::updateStaticProperty(PHPMailer::class, $name, $default);
        }
    }

    /**
     * Update the value of a - potentially inaccessible - static property in a class.
     *
     * @param string $className    The target class.
     * @param string $propertyName The name of the static property.
     * @param mixed  $value        The new value for the property.
     */
    public static function updateStaticProperty($className, $propertyName, $value)
    {
        $reflProp = new ReflectionProperty($className, $propertyName);
        $isPublic = $reflProp->isPublic();
        if ($isPublic !== true) {
            $reflProp->setAccessible(true);
        }

        $reflProp->setValue($value);

        if ($isPublic !== true) {
            $reflProp->setAccessible(false);
        }
    }

{'fraction_non_alphanumeric': 0.14305142371440713, 'fraction_numerical': 0.0047598810029749255, 'mean_word_length': 2.1201272871917265, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 162, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012595', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3491, 'n_words': 829}