|
13 | 13 | use PHP_CodeSniffer\Ruleset;
|
14 | 14 | use PHP_CodeSniffer\Files\DummyFile;
|
15 | 15 | use PHPUnit\Framework\TestCase;
|
| 16 | +use ReflectionProperty; |
16 | 17 |
|
17 | 18 | abstract class AbstractMethodUnitTest extends TestCase
|
18 | 19 | {
|
@@ -47,9 +48,22 @@ abstract class AbstractMethodUnitTest extends TestCase
|
47 | 48 | */
|
48 | 49 | public static function initializeFile()
|
49 | 50 | {
|
50 |
| - $config = new Config(); |
51 |
| - $config->standards = ['PSR1']; |
| 51 | + /* |
| 52 | + * Set the static properties in the Config class to specific values for performance |
| 53 | + * and to clear out values from other tests. |
| 54 | + */ |
52 | 55 |
|
| 56 | + self::setStaticConfigProperty('executablePaths', []); |
| 57 | + |
| 58 | + // Set to a usable value to circumvent Config trying to find a phpcs.xml config file. |
| 59 | + self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]); |
| 60 | + |
| 61 | + // Set to values which prevent the test-runner user's `CodeSniffer.conf` file |
| 62 | + // from being read and influencing the tests. Also prevent an `exec()` call to stty. |
| 63 | + self::setStaticConfigProperty('configData', ['report_width' => 80]); |
| 64 | + self::setStaticConfigProperty('configDataFile', ''); |
| 65 | + |
| 66 | + $config = new Config(); |
53 | 67 | $ruleset = new Ruleset($config);
|
54 | 68 |
|
55 | 69 | // Default to a file with the same name as the test class. Extension is property based.
|
@@ -78,9 +92,33 @@ public static function resetFile()
|
78 | 92 | {
|
79 | 93 | self::$phpcsFile = null;
|
80 | 94 |
|
| 95 | + // Reset the static properties in the Config class to their defaults to prevent tests influencing each other. |
| 96 | + self::setStaticConfigProperty('overriddenDefaults', []); |
| 97 | + self::setStaticConfigProperty('executablePaths', []); |
| 98 | + self::setStaticConfigProperty('configData', null); |
| 99 | + self::setStaticConfigProperty('configDataFile', null); |
| 100 | + |
81 | 101 | }//end resetFile()
|
82 | 102 |
|
83 | 103 |
|
| 104 | + /** |
| 105 | + * Helper function to set the value of a private static property on the Config class. |
| 106 | + * |
| 107 | + * @param string $name The name of the property to set. |
| 108 | + * @param mixed $value The value to set the property to. |
| 109 | + * |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public static function setStaticConfigProperty($name, $value) |
| 113 | + { |
| 114 | + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); |
| 115 | + $property->setAccessible(true); |
| 116 | + $property->setValue(null, $value); |
| 117 | + $property->setAccessible(false); |
| 118 | + |
| 119 | + }//end setStaticConfigProperty() |
| 120 | + |
| 121 | + |
84 | 122 | /**
|
85 | 123 | * Get the token pointer for a target token based on a specific comment found on the line before.
|
86 | 124 | *
|
|
0 commit comments