Skip to content

Commit 10e4a65

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 c6c6a10 commit 10e4a65

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
{
@@ -45,9 +46,22 @@ abstract class AbstractMethodUnitTest extends TestCase
4546
*/
4647
public static function setUpBeforeClass()
4748
{
48-
$config = new Config();
49-
$config->standards = ['PSR1'];
49+
/*
50+
* Set the static properties in the Config class to specific values for performance
51+
* and to clear out values from other tests.
52+
*/
5053

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

5367
// Default to a file with the same name as the test class. Extension is property based.
@@ -74,9 +88,33 @@ public static function tearDownAfterClass()
7488
{
7589
self::$phpcsFile = null;
7690

91+
// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
92+
self::setStaticConfigProperty('overriddenDefaults', []);
93+
self::setStaticConfigProperty('executablePaths', []);
94+
self::setStaticConfigProperty('configData', null);
95+
self::setStaticConfigProperty('configDataFile', null);
96+
7797
}//end tearDownAfterClass()
7898

7999

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

0 commit comments

Comments
 (0)