Skip to content

Commit 6b7cc9d

Browse files
committed
Tests/ConfigDouble: bug fix - always reset Config statics after use
The `Config` class uses a number of static properties, which may be updated during tests. These were previously - prior to 275 - reset to their default values in the `AbstractMethodUnitTest::resetTestFile()` method, but this reset was inadvertently removed with the reasoning that, if all tests use the `ConfigDouble`, the reset would no longer be needed as the `ConfigDouble` resets on being initialized. The flaw in this logic is that not all tests are guaranteed to use the `ConfigDouble`, which means that without the reset, the `Config` class may be left "dirty" after tests using the `ConfigDouble`, which could break tests. This commit fixes this issue by: * Adding a `__destruct()` method to the `ConfigDouble` class which will reset the static properties on the PHPCS native `Config` class whenever an object created from this class is destroyed. * Explicitly calling the `__destruct()` method from the `AbstractMethodUnitTest::reset()` method to ensure it is always run after a test has finished ("after class"), even if there would still be a lingering reference to the object. This is only a stability tweak, there are no existing tests affected by this issue at this time.
1 parent c4c389c commit 6b7cc9d

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

tests/ConfigDouble.php

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public function __construct(array $cliArgs=[], $skipSettingStandard=false, $skip
7272
}//end __construct()
7373

7474

75+
/**
76+
* Ensures the static properties in the Config class are reset to their default values
77+
* when the ConfigDouble is no longer used.
78+
*
79+
* @return void
80+
*/
81+
public function __destruct()
82+
{
83+
$this->setStaticConfigProperty('overriddenDefaults', []);
84+
$this->setStaticConfigProperty('executablePaths', []);
85+
$this->setStaticConfigProperty('configData', null);
86+
$this->setStaticConfigProperty('configDataFile', null);
87+
88+
}//end __destruct()
89+
90+
7591
/**
7692
* Sets the command line values and optionally prevents a file system search for a custom ruleset.
7793
*

tests/Core/AbstractMethodUnitTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public static function initializeFile()
9292
*/
9393
public static function reset()
9494
{
95+
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
96+
// The explicit method call prevents potential stray test-local references to the $config object
97+
// preventing the destructor from running the clean up (which without stray references would be
98+
// automagically triggered when `self::$phpcsFile` is reset, but we can't definitively rely on that).
99+
if (isset(self::$phpcsFile) === true) {
100+
self::$phpcsFile->config->__destruct();
101+
}
102+
95103
self::$fileExtension = 'inc';
96104
self::$tabWidth = 4;
97105
self::$phpcsFile = null;

tests/Core/Filters/AbstractFilterTestCase.php

+23
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ public static function initializeConfigAndRuleset()
5151
}//end initializeConfigAndRuleset()
5252

5353

54+
/**
55+
* Clean up after finished test by resetting all static properties on the Config class to their default values.
56+
*
57+
* Note: This is a PHPUnit cross-version compatible {@see \PHPUnit\Framework\TestCase::tearDownAfterClass()}
58+
* method.
59+
*
60+
* @afterClass
61+
*
62+
* @return void
63+
*/
64+
public static function reset()
65+
{
66+
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
67+
// The explicit method call prevents potential stray test-local references to the $config object
68+
// preventing the destructor from running the clean up (which without stray references would be
69+
// automagically triggered when `self::$phpcsFile` is reset, but we can't definitively rely on that).
70+
if (isset(self::$config) === true) {
71+
self::$config->__destruct();
72+
}
73+
74+
}//end reset()
75+
76+
5477
/**
5578
* Helper method to retrieve a mock object for a Filter class.
5679
*

0 commit comments

Comments
 (0)