|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Base class to use for tests invoking the Runner class. |
| 4 | + * |
| 5 | + * As those tests will use the _real_ Config class instead of the ConfigDouble, we need to ensure |
| 6 | + * this doesn't negatively impact other tests, what with the Config using static properties. |
| 7 | + * |
| 8 | + * @copyright 2025 PHPCSStandards and contributors |
| 9 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PHP_CodeSniffer\Tests\Core\Runner; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use ReflectionProperty; |
| 16 | + |
| 17 | +abstract class AbstractRunnerTestCase extends TestCase |
| 18 | +{ |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * Set static properties in the Config class to prevent tests influencing each other. |
| 23 | + * |
| 24 | + * @before |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function setConfigStatics() |
| 29 | + { |
| 30 | + // Set to the property's default value to clear out potentially set values from other tests. |
| 31 | + self::setStaticConfigProperty('overriddenDefaults', []); |
| 32 | + self::setStaticConfigProperty('executablePaths', []); |
| 33 | + |
| 34 | + // Set to values which prevent the test-runner user's `CodeSniffer.conf` file |
| 35 | + // from being read and influencing the tests. |
| 36 | + self::setStaticConfigProperty('configData', []); |
| 37 | + self::setStaticConfigProperty('configDataFile', ''); |
| 38 | + |
| 39 | + }//end setConfigStatics() |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Clean up after each finished test. |
| 44 | + * |
| 45 | + * @after |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function clearArgv() |
| 50 | + { |
| 51 | + $_SERVER['argv'] = []; |
| 52 | + |
| 53 | + }//end clearArgv() |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Reset the static properties in the Config class to their true defaults to prevent this class |
| 58 | + * from influencing other tests. |
| 59 | + * |
| 60 | + * @afterClass |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public static function reset() |
| 65 | + { |
| 66 | + self::setStaticConfigProperty('overriddenDefaults', []); |
| 67 | + self::setStaticConfigProperty('executablePaths', []); |
| 68 | + self::setStaticConfigProperty('configData', null); |
| 69 | + self::setStaticConfigProperty('configDataFile', null); |
| 70 | + $_SERVER['argv'] = []; |
| 71 | + |
| 72 | + }//end reset() |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Helper function to set a static property on the Config class. |
| 77 | + * |
| 78 | + * @param string $name The name of the property to set. |
| 79 | + * @param mixed $value The value to set the property to. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public static function setStaticConfigProperty($name, $value) |
| 84 | + { |
| 85 | + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); |
| 86 | + $property->setAccessible(true); |
| 87 | + $property->setValue(null, $value); |
| 88 | + $property->setAccessible(false); |
| 89 | + |
| 90 | + }//end setStaticConfigProperty() |
| 91 | + |
| 92 | + |
| 93 | +}//end class |
0 commit comments