Skip to content

Commit

Permalink
AbstractMethodUnitTest: take advantage of the change in reportWidth h…
Browse files Browse the repository at this point in the history
…andling

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**.
  • Loading branch information
jrfnl committed May 20, 2023
1 parent b9192ad commit 1f11631
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

abstract class AbstractMethodUnitTest extends TestCase
{
Expand Down Expand Up @@ -45,9 +46,22 @@ abstract class AbstractMethodUnitTest extends TestCase
*/
public static function setUpBeforeClass()
{
$config = new Config();
$config->standards = ['PSR1'];
/*
* Set the static properties in the Config class to specific values for performance
* and to clear out values from other tests.
*/

self::setStaticConfigProperty('executablePaths', []);

// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]);

// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
self::setStaticConfigProperty('configData', ['report_width' => 80]);
self::setStaticConfigProperty('configDataFile', '');

$config = new Config();
$ruleset = new Ruleset($config);

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

// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
self::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);
self::setStaticConfigProperty('configData', null);
self::setStaticConfigProperty('configDataFile', null);

}//end tearDownAfterClass()


/**
* Helper function to set the value of a private static property on the Config class.
*
* @param string $name The name of the property to set.
* @param mixed $value The value to set the property to.
*
* @return void
*/
public static function setStaticConfigProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
$property->setValue($value);
$property->setAccessible(false);

}//end setStaticConfigProperty()


/**
* Get the token pointer for a target token based on a specific comment found on the line before.
*
Expand Down

0 comments on commit 1f11631

Please sign in to comment.