|
18 | 18 | use PHPCSUtils\Exceptions\TestTargetNotFound;
|
19 | 19 | use PHPUnit\Framework\TestCase;
|
20 | 20 | use ReflectionClass;
|
| 21 | +use ReflectionProperty; |
21 | 22 |
|
22 | 23 | /**
|
23 | 24 | * Base class for use when testing utility methods for PHP_CodeSniffer.
|
@@ -208,9 +209,22 @@ public static function setUpTestFile()
|
208 | 209 |
|
209 | 210 | $contents = \file_get_contents($caseFile);
|
210 | 211 |
|
| 212 | + /* |
| 213 | + * Set the static properties in the Config class to specific values for performance |
| 214 | + * and to clear out values from other tests. |
| 215 | + */ |
| 216 | + self::setStaticConfigProperty('executablePaths', []); |
| 217 | + |
| 218 | + // Set to values which prevent the test-runner user's `CodeSniffer.conf` file |
| 219 | + // from being read and influencing the tests. Also prevent an `exec()` call to stty. |
| 220 | + self::setStaticConfigProperty('configData', ['report_width' => 80]); |
| 221 | + self::setStaticConfigProperty('configDataFile', ''); |
| 222 | + |
211 | 223 | $config = new \PHP_CodeSniffer\Config();
|
212 | 224 |
|
213 | 225 | /*
|
| 226 | + * Set to a usable value to circumvent Config trying to find a phpcs.xml config file. |
| 227 | + * |
214 | 228 | * We just need to provide a standard so PHPCS will tokenize the file.
|
215 | 229 | * The standard itself doesn't actually matter for testing utility methods,
|
216 | 230 | * so use the smallest one to get the fastest results.
|
@@ -298,6 +312,27 @@ public static function resetTestFile()
|
298 | 312 | self::$tabWidth = 4;
|
299 | 313 | self::$phpcsFile = null;
|
300 | 314 | self::$selectedSniff = ['Dummy.Dummy.Dummy'];
|
| 315 | + |
| 316 | + // Reset the static properties in the Config class to their defaults to prevent tests influencing each other. |
| 317 | + self::setStaticConfigProperty('executablePaths', []); |
| 318 | + self::setStaticConfigProperty('configData', null); |
| 319 | + self::setStaticConfigProperty('configDataFile', null); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Helper function to set the value of a private static property on the PHPCS Config class. |
| 324 | + * |
| 325 | + * @param string $name The name of the property to set. |
| 326 | + * @param mixed $value The value to set the property to. |
| 327 | + * |
| 328 | + * @return void |
| 329 | + */ |
| 330 | + public static function setStaticConfigProperty($name, $value) |
| 331 | + { |
| 332 | + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); |
| 333 | + $property->setAccessible(true); |
| 334 | + $property->setValue(null, $value); |
| 335 | + $property->setAccessible(false); |
301 | 336 | }
|
302 | 337 |
|
303 | 338 | /**
|
|
0 commit comments