diff --git a/tests/Core/AbstractMethodUnitTest.php b/tests/Core/AbstractMethodUnitTest.php index 4d4f546995..c1725b0cd2 100644 --- a/tests/Core/AbstractMethodUnitTest.php +++ b/tests/Core/AbstractMethodUnitTest.php @@ -13,6 +13,7 @@ use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Files\DummyFile; use PHPUnit\Framework\TestCase; +use ReflectionProperty; abstract class AbstractMethodUnitTest extends TestCase { @@ -45,9 +46,22 @@ abstract class AbstractMethodUnitTest extends TestCase */ public static function setUpBeforeClass() { - $config = new Config(); - $config->standards = ['PSR1']; + /* + * Set the static properties in the Config class to specific values for performance + * and to clear out values from other tests. + */ + self::setStaticConfigProperty('executablePaths', []); + + // Set to a usable value to circumvent Config trying to find a phpcs.xml config file. + self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]); + + // Set to values which prevent the test-runner user's `CodeSniffer.conf` file + // from being read and influencing the tests. Also prevent an `exec()` call to stty. + self::setStaticConfigProperty('configData', ['report_width' => 80]); + self::setStaticConfigProperty('configDataFile', ''); + + $config = new Config(); $ruleset = new Ruleset($config); // Default to a file with the same name as the test class. Extension is property based. @@ -74,9 +88,33 @@ public static function tearDownAfterClass() { self::$phpcsFile = null; + // Reset the static properties in the Config class to their defaults to prevent tests influencing each other. + self::setStaticConfigProperty('overriddenDefaults', []); + self::setStaticConfigProperty('executablePaths', []); + self::setStaticConfigProperty('configData', null); + self::setStaticConfigProperty('configDataFile', null); + }//end tearDownAfterClass() + /** + * Helper function to set the value of a private static property on the Config class. + * + * @param string $name The name of the property to set. + * @param mixed $value The value to set the property to. + * + * @return void + */ + public static function setStaticConfigProperty($name, $value) + { + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); + $property->setAccessible(true); + $property->setValue($value); + $property->setAccessible(false); + + }//end setStaticConfigProperty() + + /** * Get the token pointer for a target token based on a specific comment found on the line before. *