TestCase::checkChanges(): make dynamic
The `TestCase::checkChanges()` method is a way of exposing what properties in the `PHPMailer` class have a changed value compared to their default value in a particular test situation. The method is used for debugging tests.
As things were, the `TestCase::checkChanges()` method would check against a limited set of hard-coded values to determine whether the default value of a property has been updated.
This is unstable as:
1. Default values may change in the `PHPMailer` class and the values within this method would need to be updated to match, which is easily forgotten.
2. New properties may be introduced in the `PHPMailer` class and be relevant to this debug changelog.
Again, it would require manually adding these new properties to this method to start tracking them.
3. Property values may be changed in the `set_up()` method, which would be a "known change" for a certain test.
In part such "expected" changes were taken into account in this method based on the previously hard-coded setting changes in `set_up()`.
With the logic for the property setting from the `set_up()` method now being more flexible, the pre-setting of properties having been reduced to the bare minimum, but also allowing individual test clases to set their own additional changes, keeping track of what is a "known" change by checking against hard-coded values is no longer stable.
With this in mind, I propose to make the `TestCase::checkChanges()` method dynamic.
To that end, this commit:
* Retrieves the default values of all properties of the `PHPMailer` class via the PHP native `get_class_vars()` function.
* Will automatically check for changes in *all* properties, with only a limited set of _exclusions_, effectively changing the changelog from an "inclusion list" to an "exclusion list".
A select list of properties is excluded from being listed in the changelog via the `TestCase::$changelogExclude` property.
See the inline documentation in the property for the reasoning behind excluding certain properties from the changelog.
* The value of static properties will always be compared to their default value as registered in the `TestCase::$PHPMailerStaticProps` method and will be listed when different.
_Note: as documented, this list has to be hard-coded due to Reflection (as well as `get_class_vars()`) not handling default values for static properties correctly._
* The value of non-static properties will be compared to both the known changes made in the `set_up()` method and if the property was not changed in `set_up()`, to their default value. The property will be listed in the changelog when the value is different from the "expected" value, i.e. not a known change from `set_up()` and not the default value.
In addition to this, the representation of the properties will now be created via `var_export()`, which allows for listing `null` and boolean values as well.
This commit is contained in:
parent
218fd13c88
commit
55c54e46b0
|
|
@ -74,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.}
|
||||
|
|
@ -273,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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue