|
10 | 10 |
|
11 | 11 | namespace PHPCSUtils\Tests\TestUtils\ConfigDouble;
|
12 | 12 |
|
| 13 | +use PHPCSUtils\BackCompat\Helper; |
13 | 14 | use PHPCSUtils\TestUtils\ConfigDouble;
|
| 15 | +use ReflectionProperty; |
14 | 16 | use Yoast\PHPUnitPolyfills\TestCases\TestCase;
|
15 | 17 |
|
16 | 18 | /**
|
|
25 | 27 | final class ConfigDoubleTest extends TestCase
|
26 | 28 | {
|
27 | 29 |
|
| 30 | + /** |
| 31 | + * Reset the static properties in the Config class to their true defaults to prevent this class |
| 32 | + * from influencing other tests. |
| 33 | + * |
| 34 | + * @afterClass |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public static function resetConfigToDefaults() |
| 39 | + { |
| 40 | + self::setStaticConfigProperty('overriddenDefaults', []); |
| 41 | + self::setStaticConfigProperty('executablePaths', []); |
| 42 | + self::setStaticConfigProperty('configData', null); |
| 43 | + self::setStaticConfigProperty('configDataFile', null); |
| 44 | + $_SERVER['argv'] = []; |
| 45 | + } |
| 46 | + |
28 | 47 | /**
|
29 | 48 | * Verify that the static properties in the Config class get cleared between instances.
|
30 | 49 | *
|
@@ -200,4 +219,119 @@ public function testReportWidthOverrideIsSkippedOnRequest()
|
200 | 219 | $this->assertNotSame(80, $config->reportWidth, 'Report width has still been set to 80');
|
201 | 220 | }
|
202 | 221 | }
|
| 222 | + |
| 223 | + /** |
| 224 | + * Verify that the static properties in the Config class are reset when the object is destroyed. |
| 225 | + * |
| 226 | + * @covers ::__destruct |
| 227 | + * |
| 228 | + * @return void |
| 229 | + */ |
| 230 | + public function testDestruct() |
| 231 | + { |
| 232 | + $standard = 'Squiz'; |
| 233 | + $reportWidth = 1250; |
| 234 | + $fakeConfFile = 'path/to/file.conf'; |
| 235 | + $toolName = 'a_tool'; |
| 236 | + |
| 237 | + // Create the ConfigDouble object and change the value of a few static properties to allow for testing the reset. |
| 238 | + $cliArgs = [ |
| 239 | + '--standard=' . $standard, |
| 240 | + '--report-width=' . $reportWidth, |
| 241 | + '--runtime-set', |
| 242 | + 'arbitraryKey', |
| 243 | + 'arbitraryValue', |
| 244 | + ]; |
| 245 | + $config = new ConfigDouble($cliArgs); |
| 246 | + |
| 247 | + $config->setStaticConfigProperty('configDataFile', $fakeConfFile); |
| 248 | + $config::getExecutablePath($toolName); |
| 249 | + |
| 250 | + // Verify the static properties in the Config are set to something other than their default value. |
| 251 | + $this->assertSame([$standard], $config->standards, 'Precondition check: Standards was not set to Squiz'); |
| 252 | + $this->assertSame($reportWidth, $config->reportWidth, 'Precondition check: Report width was not set to 1250'); |
| 253 | + $this->assertSame( |
| 254 | + 'arbitraryValue', |
| 255 | + Helper::getConfigData('arbitraryKey'), |
| 256 | + 'Precondition check: ArbitraryKey property was not set on the Config class' |
| 257 | + ); |
| 258 | + |
| 259 | + $overriddenDefaults = $this->getStaticConfigProperty('overriddenDefaults', $config); |
| 260 | + $this->assertIsArray($overriddenDefaults, 'Precondition check: overriddenDefaults property is not an array'); |
| 261 | + $this->assertNotEmpty($overriddenDefaults, 'Precondition check: overriddenDefaults property is an empty array'); |
| 262 | + |
| 263 | + $this->assertSame( |
| 264 | + [$toolName => null], |
| 265 | + $this->getStaticConfigProperty('executablePaths', $config), |
| 266 | + 'Precondition check: executablePaths is still an empty array' |
| 267 | + ); |
| 268 | + |
| 269 | + $configData = $this->getStaticConfigProperty('configData', $config); |
| 270 | + $this->assertIsArray($configData, 'Precondition check: configData property is not an array'); |
| 271 | + $this->assertNotEmpty($configData, 'Precondition check: configData property is an empty array'); |
| 272 | + |
| 273 | + $this->assertSame( |
| 274 | + $fakeConfFile, |
| 275 | + $this->getStaticConfigProperty('configDataFile', $config), |
| 276 | + 'Precondition check: configDataFile property has not been set' |
| 277 | + ); |
| 278 | + |
| 279 | + // Destroy the object. |
| 280 | + unset($config); |
| 281 | + |
| 282 | + // Verify the static properties in the Config are reset to their default values. |
| 283 | + $this->assertSame([], $this->getStaticConfigProperty('overriddenDefaults'), 'overriddenDefaults reset failed'); |
| 284 | + $this->assertSame([], $this->getStaticConfigProperty('executablePaths'), 'executablePaths reset failed'); |
| 285 | + $this->assertNull($this->getStaticConfigProperty('configData'), 'configData reset failed'); |
| 286 | + $this->assertNull($this->getStaticConfigProperty('configDataFile'), 'configDataFile reset failed'); |
| 287 | + |
| 288 | + // This assertion must be last as it re-initializes the $configData and $configDataFile properties. |
| 289 | + $this->assertNull(Helper::getConfigData('arbitraryKey'), 'arbitraryKey property is still set'); |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Helper function to retrieve the value of a private static property on the Config class. |
| 294 | + * |
| 295 | + * @param string $name The name of the property to retrieve. |
| 296 | + * @param \PHP_CodeSniffer\Config $config Optional. The config object. |
| 297 | + * |
| 298 | + * @return mixed |
| 299 | + */ |
| 300 | + private function getStaticConfigProperty($name, $config = null) |
| 301 | + { |
| 302 | + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); |
| 303 | + $property->setAccessible(true); |
| 304 | + |
| 305 | + if ($name === 'overriddenDefaults' && \version_compare(Helper::getVersion(), '3.99.99', '>')) { |
| 306 | + // The `overriddenDefaults` property is no longer static on PHPCS 4.0+. |
| 307 | + if (isset($config)) { |
| 308 | + return $property->getValue($config); |
| 309 | + } else { |
| 310 | + return []; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + return $property->getValue(); |
| 315 | + } |
| 316 | + |
| 317 | + /** |
| 318 | + * Helper function to set the value of a private static property on the Config class. |
| 319 | + * |
| 320 | + * @param string $name The name of the property to set. |
| 321 | + * @param mixed $value The value to set the property to. |
| 322 | + * |
| 323 | + * @return void |
| 324 | + */ |
| 325 | + public static function setStaticConfigProperty($name, $value) |
| 326 | + { |
| 327 | + $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); |
| 328 | + $property->setAccessible(true); |
| 329 | + |
| 330 | + // The `overriddenDefaults` property is no longer static on PHPCS 4.0+, so ignore it. |
| 331 | + if ($name !== 'overriddenDefaults' && \version_compare(Helper::getVersion(), '3.99.99', '<=')) { |
| 332 | + $property->setValue(null, $value); |
| 333 | + } |
| 334 | + |
| 335 | + $property->setAccessible(false); |
| 336 | + } |
203 | 337 | }
|
0 commit comments