Skip to content

Commit 72dbc34

Browse files
committed
Tests: introduce new AbstractRealConfigTestCase
As the `Config` class contains a number of `static` properties, it is important to avoid tests influencing each other if the tests would cause changes to the value of the static properties. This was already handled correctly in the two places were the _real_ `Config` class is being used in the tests, but that meant those test classes now contain duplicate code. This new `AbstractRealConfigTestCase` class removes that code duplication. This test case should be used in the rare circumstances that the _real_ `Config` class needs to be used in the tests. In most cases, using the `ConfigDouble` class is much preferred, but for select tests for the `Config` class itself + for tests involving the `Runner` class, using the real `Config` class cannot be avoided.
1 parent 1c8f9da commit 72dbc34

File tree

3 files changed

+105
-149
lines changed

3 files changed

+105
-149
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Test case with helper methods for tests which need to use the *real* Config class (instead of the ConfigDouble).
4+
*
5+
* This test case should be used sparingly and only when it cannot be avoided.
6+
*
7+
* @copyright 2025 PHPCSStandards and contributors
8+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9+
*/
10+
11+
namespace PHP_CodeSniffer\Tests\Core\Config;
12+
13+
use PHPUnit\Framework\TestCase;
14+
use ReflectionProperty;
15+
16+
abstract class AbstractRealConfigTestCase extends TestCase
17+
{
18+
19+
20+
/**
21+
* Set static properties in the Config class to prevent tests influencing each other.
22+
*
23+
* @before
24+
*
25+
* @return void
26+
*/
27+
protected function setConfigStatics()
28+
{
29+
// Set to the property's default value to clear out potentially set values from other tests.
30+
self::setStaticConfigProperty('executablePaths', []);
31+
32+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
33+
// from being read and influencing the tests.
34+
self::setStaticConfigProperty('configData', []);
35+
self::setStaticConfigProperty('configDataFile', '');
36+
37+
}//end setConfigStatics()
38+
39+
40+
/**
41+
* Clean up after each finished test.
42+
*
43+
* @after
44+
*
45+
* @return void
46+
*/
47+
protected function clearArgv()
48+
{
49+
$_SERVER['argv'] = [];
50+
51+
}//end clearArgv()
52+
53+
54+
/**
55+
* Reset the static properties in the Config class to their true defaults to prevent this class
56+
* from influencing other tests.
57+
*
58+
* @afterClass
59+
*
60+
* @return void
61+
*/
62+
public static function resetConfigToDefaults()
63+
{
64+
self::setStaticConfigProperty('executablePaths', []);
65+
self::setStaticConfigProperty('configData', null);
66+
self::setStaticConfigProperty('configDataFile', null);
67+
$_SERVER['argv'] = [];
68+
69+
}//end resetConfigToDefaults()
70+
71+
72+
/**
73+
* Helper function to set a static property on the Config class.
74+
*
75+
* @param string $name The name of the property to set.
76+
* @param mixed $value The value to set the property to.
77+
*
78+
* @return void
79+
*/
80+
protected static function setStaticConfigProperty($name, $value)
81+
{
82+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
83+
$property->setAccessible(true);
84+
$property->setValue(null, $value);
85+
$property->setAccessible(false);
86+
87+
}//end setStaticConfigProperty()
88+
89+
90+
}//end class

tests/Core/Config/ReportWidthTest.php

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,17 @@
1010
namespace PHP_CodeSniffer\Tests\Core\Config;
1111

1212
use PHP_CodeSniffer\Config;
13-
use PHPUnit\Framework\TestCase;
14-
use ReflectionProperty;
13+
use PHP_CodeSniffer\Tests\Core\Config\AbstractRealConfigTestCase;
1514

1615
/**
1716
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
1817
*
1918
* @covers \PHP_CodeSniffer\Config::__get
2019
*/
21-
final class ReportWidthTest extends TestCase
20+
final class ReportWidthTest extends AbstractRealConfigTestCase
2221
{
2322

2423

25-
/**
26-
* Set static properties in the Config class to prevent tests influencing each other.
27-
*
28-
* @return void
29-
*/
30-
protected function setUp(): void
31-
{
32-
// Set to the property's default value to clear out potentially set values from other tests.
33-
self::setStaticProperty('executablePaths', []);
34-
35-
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
36-
// from being read and influencing the tests.
37-
self::setStaticProperty('configData', []);
38-
self::setStaticProperty('configDataFile', '');
39-
40-
}//end setUp()
41-
42-
43-
/**
44-
* Clean up after each finished test.
45-
*
46-
* @return void
47-
*/
48-
protected function tearDown(): void
49-
{
50-
$_SERVER['argv'] = [];
51-
52-
}//end tearDown()
53-
54-
55-
/**
56-
* Reset the static properties in the Config class to their true defaults to prevent this class
57-
* from influencing other tests.
58-
*
59-
* @return void
60-
*/
61-
public static function tearDownAfterClass(): void
62-
{
63-
self::setStaticProperty('executablePaths', []);
64-
self::setStaticProperty('configData', null);
65-
self::setStaticProperty('configDataFile', null);
66-
$_SERVER['argv'] = [];
67-
68-
}//end tearDownAfterClass()
69-
70-
7124
/**
7225
* Test that report width without overrules will always be set to a non-0 positive integer.
7326
*
@@ -78,7 +31,7 @@ public static function tearDownAfterClass(): void
7831
*/
7932
public function testReportWidthDefault()
8033
{
81-
$config = new Config();
34+
$config = new Config(['--standard=PSR1']);
8235

8336
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
8437
$this->assertIsInt($config->reportWidth, 'Report width is not an integer');
@@ -102,9 +55,9 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
10255
'show_warnings' => '0',
10356
];
10457

105-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
58+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
10659

107-
$config = new Config();
60+
$config = new Config(['--standard=PSR1']);
10861

10962
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
11063
$this->assertIsInt($config->reportWidth, 'Report width is not an integer');
@@ -129,9 +82,9 @@ public function testReportWidthCanBeSetFromConfFile()
12982
'report_width' => '120',
13083
];
13184

132-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
85+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
13386

134-
$config = new Config();
87+
$config = new Config(['--standard=PSR1']);
13588
$this->assertSame(120, $config->reportWidth);
13689

13790
}//end testReportWidthCanBeSetFromConfFile()
@@ -149,6 +102,7 @@ public function testReportWidthCanBeSetFromCLI()
149102
{
150103
$_SERVER['argv'] = [
151104
'phpcs',
105+
'--standard=PSR1',
152106
'--report-width=100',
153107
];
154108

@@ -170,6 +124,7 @@ public function testReportWidthWhenSetFromCLIFirstValuePrevails()
170124
{
171125
$_SERVER['argv'] = [
172126
'phpcs',
127+
'--standard=PSR1',
173128
'--report-width=100',
174129
'--report-width=200',
175130
];
@@ -199,10 +154,11 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
199154
'report_width' => '120',
200155
];
201156

202-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
157+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
203158

204159
$cliArgs = [
205160
'phpcs',
161+
'--standard=PSR1',
206162
'--report-width=180',
207163
];
208164

@@ -221,7 +177,7 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
221177
*/
222178
public function testReportWidthInputHandlingForAuto()
223179
{
224-
$config = new Config();
180+
$config = new Config(['--standard=PSR1']);
225181
$config->reportWidth = 'auto';
226182

227183
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
@@ -244,7 +200,7 @@ public function testReportWidthInputHandlingForAuto()
244200
*/
245201
public function testReportWidthInputHandling($value, $expected)
246202
{
247-
$config = new Config();
203+
$config = new Config(['--standard=PSR1']);
248204
$config->reportWidth = $value;
249205

250206
$this->assertSame($expected, $config->reportWidth);
@@ -301,22 +257,4 @@ public static function dataReportWidthInputHandling()
301257
}//end dataReportWidthInputHandling()
302258

303259

304-
/**
305-
* Helper function to set a static property on the Config class.
306-
*
307-
* @param string $name The name of the property to set.
308-
* @param mixed $value The value to set the property to.
309-
*
310-
* @return void
311-
*/
312-
public static function setStaticProperty($name, $value)
313-
{
314-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
315-
$property->setAccessible(true);
316-
$property->setValue(null, $value);
317-
$property->setAccessible(false);
318-
319-
}//end setStaticProperty()
320-
321-
322260
}//end class

tests/Core/Runner/AbstractRunnerTestCase.php

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,81 +11,9 @@
1111

1212
namespace PHP_CodeSniffer\Tests\Core\Runner;
1313

14-
use PHPUnit\Framework\TestCase;
15-
use ReflectionProperty;
14+
use PHP_CodeSniffer\Tests\Core\Config\AbstractRealConfigTestCase;
1615

17-
abstract class AbstractRunnerTestCase extends TestCase
16+
abstract class AbstractRunnerTestCase extends AbstractRealConfigTestCase
1817
{
1918

20-
21-
/**
22-
* Set static properties in the Config class to prevent tests influencing each other.
23-
*
24-
* @before
25-
*
26-
* @return void
27-
*/
28-
public function setConfigStatics()
29-
{
30-
// Set to the property's default value to clear out potentially set values from other tests.
31-
self::setStaticConfigProperty('executablePaths', []);
32-
33-
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
34-
// from being read and influencing the tests.
35-
self::setStaticConfigProperty('configData', []);
36-
self::setStaticConfigProperty('configDataFile', '');
37-
38-
}//end setConfigStatics()
39-
40-
41-
/**
42-
* Clean up after each finished test.
43-
*
44-
* @after
45-
*
46-
* @return void
47-
*/
48-
public function clearArgv()
49-
{
50-
$_SERVER['argv'] = [];
51-
52-
}//end clearArgv()
53-
54-
55-
/**
56-
* Reset the static properties in the Config class to their true defaults to prevent this class
57-
* from influencing other tests.
58-
*
59-
* @afterClass
60-
*
61-
* @return void
62-
*/
63-
public static function reset()
64-
{
65-
self::setStaticConfigProperty('executablePaths', []);
66-
self::setStaticConfigProperty('configData', null);
67-
self::setStaticConfigProperty('configDataFile', null);
68-
$_SERVER['argv'] = [];
69-
70-
}//end reset()
71-
72-
73-
/**
74-
* Helper function to set a static property on the Config class.
75-
*
76-
* @param string $name The name of the property to set.
77-
* @param mixed $value The value to set the property to.
78-
*
79-
* @return void
80-
*/
81-
public static function setStaticConfigProperty($name, $value)
82-
{
83-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
84-
$property->setAccessible(true);
85-
$property->setValue(null, $value);
86-
$property->setAccessible(false);
87-
88-
}//end setStaticConfigProperty()
89-
90-
9119
}//end class

0 commit comments

Comments
 (0)