Skip to content

Commit 9e0ef95

Browse files
committed
UtilityMethodTestCase: performance improvement
PR squizlabs/PHP_CodeSniffer 3831 / PHPCSStandards/PHP_CodeSniffer 61 in PHPCS itself makes a change to improve the performance of the `Config` class. This commit lets the `UtilityMethodTestCase` take advantage of that performance improvement by explicitly passing the `report_width` to prevent a call to `shell_exec('stty ...')` from being made and slowing down the tests. While not benchmarked properly, with the test runs for PHPCSUtils on my local machine on Windows, the difference this change makes is significant and noticable: * Before: **2.5 minutes**. * After: **1 second**. This advantage can only be seen with PHPCS 3.8.0 or higher, but the change does not negatively impact test runs against PHPCS < 3.8.0, so there is no need to raise the minimum PHPCS version.
1 parent f76bab4 commit 9e0ef95

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)