Skip to content

Commit bfa5acd

Browse files
committed
✨ New PHPCSUtils\TestUtils\ConfigDouble class
The PHP_CodeSniffer native `Config` class contains a number of static properties. As the value of these static properties will be retained between instantiations of the class, config values set in one test can influence the results for another test, which makes tests unstable. This commit introduces a test "double" of the `Config` class which prevents this from happening. In _most_ cases, tests should be using this class instead of the "normal" Config, with the exception of select tests for the PHPCS Config class itself. Includes tests covering the new class.
1 parent 567d785 commit bfa5acd

File tree

3 files changed

+650
-0
lines changed

3 files changed

+650
-0
lines changed

PHPCSUtils/TestUtils/ConfigDouble.php

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
/**
3+
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSUtils
6+
* @copyright 2019-2024 PHPCSUtils Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSUtils
9+
*/
10+
11+
namespace PHPCSUtils\TestUtils;
12+
13+
use PHP_CodeSniffer\Config;
14+
use PHPCSUtils\BackCompat\Helper;
15+
use ReflectionProperty;
16+
17+
/**
18+
* Config class for use in the tests.
19+
*
20+
* The PHP_CodeSniffer Config class contains a number of static properties.
21+
* As the value of these static properties will be retained between instantiations of the class,
22+
* config values set in one test can influence the results for another test, which makes tests unstable.
23+
*
24+
* This class is a "double" of the Config class which prevents this from happening.
25+
* In _most_ cases, tests should be using this class instead of the "normal" Config,
26+
* with the exception of select tests for the PHPCS Config class itself.
27+
*
28+
* @since 1.1.0
29+
*/
30+
final class ConfigDouble extends Config
31+
{
32+
33+
/**
34+
* The PHPCS version the tests are being run on.
35+
*
36+
* @since 1.1.0
37+
*
38+
* @var string
39+
*/
40+
private $phpcsVersion = '0';
41+
42+
/**
43+
* Whether or not the setting of a standard should be skipped.
44+
*
45+
* @since 1.1.0
46+
*
47+
* @var bool
48+
*/
49+
private $skipSettingStandard = false;
50+
51+
/**
52+
* Creates a clean Config object and populates it with command line values.
53+
*
54+
* @since 1.1.0
55+
*
56+
* @param array<string> $cliArgs An array of values gathered from CLI args.
57+
* @param bool $skipSettingStandard Whether to skip setting a standard to prevent
58+
* the Config class trying to auto-discover a ruleset file.
59+
* Should only be set to `true` for tests which actually test
60+
* the ruleset auto-discovery.
61+
* Note: there is no need to set this to `true` when a standard
62+
* is being passed via the `$cliArgs`. Those settings will always
63+
* respected.
64+
* Defaults to `false`. Will result in the standard being set
65+
* to "PSR1" if not provided via `$cliArgs`.
66+
* @param bool $skipSettingReportWidth Whether to skip setting a report-width to prevent
67+
* the Config class trying to auto-discover the screen width.
68+
* Should only be set to `true` for tests which actually test
69+
* the screen width auto-discovery.
70+
* Note: there is no need to set this to `true` when a report-width
71+
* is being passed via the `$cliArgs`. Those settings will always
72+
* respected.
73+
* Defaults to `false`. Will result in the reportWidth being set
74+
* to "80" if not provided via `$cliArgs`.
75+
*
76+
* @return void
77+
*/
78+
public function __construct(array $cliArgs = [], $skipSettingStandard = false, $skipSettingReportWidth = false)
79+
{
80+
$this->skipSettingStandard = $skipSettingStandard;
81+
$this->phpcsVersion = Helper::getVersion();
82+
83+
$this->resetSelectProperties();
84+
$this->preventReadingCodeSnifferConfFile();
85+
86+
parent::__construct($cliArgs);
87+
88+
if ($skipSettingReportWidth !== true) {
89+
$this->preventAutoDiscoveryScreenWidth();
90+
}
91+
}
92+
93+
/**
94+
* Sets the command line values and optionally prevents a file system search for a custom ruleset.
95+
*
96+
* @since 1.1.0
97+
*
98+
* @param array<string> $args An array of command line arguments to set.
99+
*
100+
* @return void
101+
*/
102+
public function setCommandLineValues(array $args)
103+
{
104+
parent::setCommandLineValues($args);
105+
106+
if ($this->skipSettingStandard !== true) {
107+
$this->preventSearchingForRuleset();
108+
}
109+
}
110+
111+
/**
112+
* Reset a few properties on the Config class to their default values.
113+
*
114+
* @since 1.1.0
115+
*
116+
* @return void
117+
*/
118+
private function resetSelectProperties()
119+
{
120+
$this->setStaticConfigProperty('overriddenDefaults', []);
121+
$this->setStaticConfigProperty('executablePaths', []);
122+
}
123+
124+
/**
125+
* Prevent the values in a potentially available user-specific `CodeSniffer.conf` file
126+
* from influencing the tests.
127+
*
128+
* This also prevents some file system calls which can influence the test runtime.
129+
*
130+
* @since 1.1.0
131+
*
132+
* @return void
133+
*/
134+
private function preventReadingCodeSnifferConfFile()
135+
{
136+
$this->setStaticConfigProperty('configData', []);
137+
$this->setStaticConfigProperty('configDataFile', '');
138+
}
139+
140+
/**
141+
* Prevent searching for a custom ruleset by setting a standard, but only if the test
142+
* being run doesn't set a standard itself.
143+
*
144+
* This also prevents some file system calls which can influence the test runtime.
145+
*
146+
* The standard being set is the smallest one available so the ruleset initialization
147+
* will be the fastest possible.
148+
*
149+
* @since 1.1.0
150+
*
151+
* @return void
152+
*/
153+
private function preventSearchingForRuleset()
154+
{
155+
$overriddenDefaults = $this->getStaticConfigProperty('overriddenDefaults');
156+
if (isset($overriddenDefaults['standards']) === false) {
157+
$this->standards = ['PSR1'];
158+
$overriddenDefaults['standards'] = true;
159+
}
160+
161+
self::setStaticConfigProperty('overriddenDefaults', $overriddenDefaults);
162+
}
163+
164+
/**
165+
* Prevent a call to stty to figure out the screen width, but only if the test being run
166+
* doesn't set a report width itself.
167+
*
168+
* @since 1.1.0
169+
*
170+
* @return void
171+
*/
172+
private function preventAutoDiscoveryScreenWidth()
173+
{
174+
$settings = $this->getSettings();
175+
if ($settings['reportWidth'] === 'auto') {
176+
$this->reportWidth = self::DEFAULT_REPORT_WIDTH;
177+
}
178+
}
179+
180+
/**
181+
* Helper function to retrieve the value of a private static property on the Config class.
182+
*
183+
* Note: As of PHPCS 4.0, the "overriddenDefaults" property is no longer static, but this method
184+
* will still handle this property.
185+
*
186+
* @since 1.1.0
187+
*
188+
* @param string $name The name of the property to retrieve.
189+
*
190+
* @return mixed
191+
*/
192+
public function getStaticConfigProperty($name)
193+
{
194+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
195+
$property->setAccessible(true);
196+
197+
if ($name === 'overriddenDefaults' && \version_compare($this->phpcsVersion, '3.99.99', '>')) {
198+
return $property->getValue($this);
199+
}
200+
201+
return $property->getValue();
202+
}
203+
204+
/**
205+
* Helper function to set the value of a private static property on the Config class.
206+
*
207+
* Note: As of PHPCS 4.0, the "overriddenDefaults" property is no longer static, but this method
208+
* will still handle this property.
209+
*
210+
* @since 1.1.0
211+
*
212+
* @param string $name The name of the property to set.
213+
* @param mixed $value The value to set the property to.
214+
*
215+
* @return void
216+
*/
217+
public function setStaticConfigProperty($name, $value)
218+
{
219+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
220+
$property->setAccessible(true);
221+
222+
if ($name === 'overriddenDefaults' && \version_compare($this->phpcsVersion, '3.99.99', '>')) {
223+
$property->setValue($this, $value);
224+
} else {
225+
$property->setValue(null, $value);
226+
}
227+
228+
$property->setAccessible(false);
229+
}
230+
}

0 commit comments

Comments
 (0)