Skip to content

Commit dc93c9a

Browse files
authored
Merge pull request #525 from PHPCSStandards/feature/utilitymethodtestcase-improve-performance
UtilityMethodTestCase: performance improvement
2 parents f76bab4 + 9e0ef95 commit dc93c9a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

PHPCSUtils/TestUtils/UtilityMethodTestCase.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPCSUtils\Exceptions\TestTargetNotFound;
1919
use PHPUnit\Framework\TestCase;
2020
use ReflectionClass;
21+
use ReflectionProperty;
2122

2223
/**
2324
* Base class for use when testing utility methods for PHP_CodeSniffer.
@@ -208,9 +209,22 @@ public static function setUpTestFile()
208209

209210
$contents = \file_get_contents($caseFile);
210211

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+
211223
$config = new \PHP_CodeSniffer\Config();
212224

213225
/*
226+
* Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
227+
*
214228
* We just need to provide a standard so PHPCS will tokenize the file.
215229
* The standard itself doesn't actually matter for testing utility methods,
216230
* so use the smallest one to get the fastest results.
@@ -298,6 +312,27 @@ public static function resetTestFile()
298312
self::$tabWidth = 4;
299313
self::$phpcsFile = null;
300314
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);
301336
}
302337

303338
/**

0 commit comments

Comments
 (0)