Skip to content

Commit 086d955

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 1235d21 commit 086d955

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 39 additions & 1 deletion
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,7 +46,21 @@ abstract class AbstractMethodUnitTest extends TestCase
4546
*/
4647
public static function setUpBeforeClass(): void
4748
{
48-
$config = new Config();
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+
*/
53+
54+
self::setStaticConfigProperty('executablePaths', []);
55+
56+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
57+
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
58+
self::setStaticConfigProperty('configData', ['report_width' => 80]);
59+
self::setStaticConfigProperty('configDataFile', '');
60+
61+
$config = new Config();
62+
63+
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
4964
$config->standards = ['PSR1'];
5065

5166
$ruleset = new Ruleset($config);
@@ -74,9 +89,32 @@ public static function tearDownAfterClass(): void
7489
{
7590
self::$phpcsFile = null;
7691

92+
// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
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(Config::class, $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)