Skip to content

Commit

Permalink
Merge pull request #576 from PHPCSStandards/feature/configdouble-stab…
Browse files Browse the repository at this point in the history
…ilize

Tests: stability tweaks for the non-sniff tests
  • Loading branch information
jrfnl authored Jul 27, 2024
2 parents 329ad21 + 6b7cc9d commit a3d11a9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/ConfigDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ public function __construct(array $cliArgs=[], $skipSettingStandard=false, $skip
}//end __construct()


/**
* Ensures the static properties in the Config class are reset to their default values
* when the ConfigDouble is no longer used.
*
* @return void
*/
public function __destruct()
{
$this->setStaticConfigProperty('overriddenDefaults', []);
$this->setStaticConfigProperty('executablePaths', []);
$this->setStaticConfigProperty('configData', null);
$this->setStaticConfigProperty('configDataFile', null);

}//end __destruct()


/**
* Sets the command line values and optionally prevents a file system search for a custom ruleset.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ public static function initializeFile()
}//end initializeFile()


/**
* Clean up after finished test by resetting all static properties on the class to their default values.
*
* Note: This is a PHPUnit cross-version compatible {@see \PHPUnit\Framework\TestCase::tearDownAfterClass()}
* method.
*
* @afterClass
*
* @return void
*/
public static function reset()
{
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
// The explicit method call prevents potential stray test-local references to the $config object
// preventing the destructor from running the clean up (which without stray references would be
// automagically triggered when `self::$phpcsFile` is reset, but we can't definitively rely on that).
if (isset(self::$phpcsFile) === true) {
self::$phpcsFile->config->__destruct();
}

self::$fileExtension = 'inc';
self::$tabWidth = 4;
self::$phpcsFile = null;

}//end reset()


/**
* Get the token pointer for a target token based on a specific comment found on the line before.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Core/Filters/AbstractFilterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public static function initializeConfigAndRuleset()
}//end initializeConfigAndRuleset()


/**
* Clean up after finished test by resetting all static properties on the Config class to their default values.
*
* Note: This is a PHPUnit cross-version compatible {@see \PHPUnit\Framework\TestCase::tearDownAfterClass()}
* method.
*
* @afterClass
*
* @return void
*/
public static function reset()
{
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
// The explicit method call prevents potential stray test-local references to the $config object
// preventing the destructor from running the clean up (which without stray references would be
// automagically triggered when `self::$phpcsFile` is reset, but we can't definitively rely on that).
if (isset(self::$config) === true) {
self::$config->__destruct();
}

}//end reset()


/**
* Helper method to retrieve a mock object for a Filter class.
*
Expand Down

0 comments on commit a3d11a9

Please sign in to comment.