Skip to content

Commit 8474e71

Browse files
committed
AbstractMethodUnitTest: take advantage of the change in reportWidth handling
For the tests using the `AbstractMethodUnitTest` class, the `reportWidth` and most other config settings are irrelevant. This commit changes some of the set up/tear down for the test to make the use of the `Config` class more efficient. This should make the tests using the `AbstractMethodUnitTest` class as their base significantly faster (at the very least on Windows). While not benchmarked properly, I have done some comparisons with the test runs on my local machine on Windows. * `phpunit --filter Core` (= the tests which use this base class + a few extra tests): Before: **2 minutes**. After: **8 seconds**. * The same effect can be seen when running `phpunit` without a `--filter`: Before: **7 minutes**. After: **5 minutes**. * And when I apply a similar change to the one made here to the base test class in PHPCSUtils (4621 tests): Before: **2.5 minutes**. After: **1 second**.
1 parent 00f2136 commit 8474e71

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHP_CodeSniffer\Files\DummyFile;
1515
use PHPUnit\Framework\TestCase;
16+
use ReflectionProperty;
1617

1718
abstract class AbstractMethodUnitTest extends TestCase
1819
{
@@ -47,9 +48,22 @@ abstract class AbstractMethodUnitTest extends TestCase
4748
*/
4849
public static function initializeFile()
4950
{
50-
$config = new Config();
51-
$config->standards = ['PSR1'];
51+
/*
52+
* Set the static properties in the Config class to specific values for performance
53+
* and to clear out values from other tests.
54+
*/
5255

56+
self::setStaticConfigProperty('executablePaths', []);
57+
58+
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
59+
self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]);
60+
61+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
62+
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
63+
self::setStaticConfigProperty('configData', ['report_width' => 80]);
64+
self::setStaticConfigProperty('configDataFile', '');
65+
66+
$config = new Config();
5367
$ruleset = new Ruleset($config);
5468

5569
// Default to a file with the same name as the test class. Extension is property based.
@@ -78,9 +92,33 @@ public static function resetFile()
7892
{
7993
self::$phpcsFile = null;
8094

95+
// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
96+
self::setStaticConfigProperty('overriddenDefaults', []);
97+
self::setStaticConfigProperty('executablePaths', []);
98+
self::setStaticConfigProperty('configData', null);
99+
self::setStaticConfigProperty('configDataFile', null);
100+
81101
}//end resetFile()
82102

83103

104+
/**
105+
* Helper function to set the value of a private static property on the Config class.
106+
*
107+
* @param string $name The name of the property to set.
108+
* @param mixed $value The value to set the property to.
109+
*
110+
* @return void
111+
*/
112+
public static function setStaticConfigProperty($name, $value)
113+
{
114+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
115+
$property->setAccessible(true);
116+
$property->setValue(null, $value);
117+
$property->setAccessible(false);
118+
119+
}//end setStaticConfigProperty()
120+
121+
84122
/**
85123
* Get the token pointer for a target token based on a specific comment found on the line before.
86124
*

0 commit comments

Comments
 (0)