Skip to content

Commit 360e30a

Browse files
authored
Merge pull request #909 from PHPCSStandards/feature/tests-add-abstract-real-config-testcase
Tests: introduce new `AbstractRealConfigTestCase`
2 parents 615e25a + c157dc4 commit 360e30a

File tree

3 files changed

+107
-161
lines changed

3 files changed

+107
-161
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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('overriddenDefaults', []);
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+
protected 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 resetConfigToDefaults()
64+
{
65+
self::setStaticConfigProperty('overriddenDefaults', []);
66+
self::setStaticConfigProperty('executablePaths', []);
67+
self::setStaticConfigProperty('configData', null);
68+
self::setStaticConfigProperty('configDataFile', null);
69+
$_SERVER['argv'] = [];
70+
71+
}//end resetConfigToDefaults()
72+
73+
74+
/**
75+
* Helper function to set a static property on the Config class.
76+
*
77+
* @param string $name The name of the property to set.
78+
* @param mixed $value The value to set the property to.
79+
*
80+
* @return void
81+
*/
82+
protected static function setStaticConfigProperty($name, $value)
83+
{
84+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
85+
$property->setAccessible(true);
86+
$property->setValue(null, $value);
87+
$property->setAccessible(false);
88+
89+
}//end setStaticConfigProperty()
90+
91+
92+
}//end class

tests/Core/Config/ReportWidthTest.php

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +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-
* @before
29-
*
30-
* @return void
31-
*/
32-
protected function cleanConfig()
33-
{
34-
// Set to the property's default value to clear out potentially set values from other tests.
35-
self::setStaticProperty('executablePaths', []);
36-
37-
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
38-
self::setStaticProperty('overriddenDefaults', ['standards' => ['PSR1']]);
39-
40-
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
41-
// from being read and influencing the tests.
42-
self::setStaticProperty('configData', []);
43-
self::setStaticProperty('configDataFile', '');
44-
45-
}//end cleanConfig()
46-
47-
48-
/**
49-
* Clean up after each finished test.
50-
*
51-
* @after
52-
*
53-
* @return void
54-
*/
55-
protected function resetConfig()
56-
{
57-
$_SERVER['argv'] = [];
58-
59-
}//end resetConfig()
60-
61-
62-
/**
63-
* Reset the static properties in the Config class to their true defaults to prevent this class
64-
* from influencing other tests.
65-
*
66-
* @afterClass
67-
*
68-
* @return void
69-
*/
70-
public static function resetConfigToDefaults()
71-
{
72-
self::setStaticProperty('overriddenDefaults', []);
73-
self::setStaticProperty('executablePaths', []);
74-
self::setStaticProperty('configData', null);
75-
self::setStaticProperty('configDataFile', null);
76-
$_SERVER['argv'] = [];
77-
78-
}//end resetConfigToDefaults()
79-
80-
8124
/**
8225
* Test that report width without overrules will always be set to a non-0 positive integer.
8326
*
@@ -88,7 +31,7 @@ public static function resetConfigToDefaults()
8831
*/
8932
public function testReportWidthDefault()
9033
{
91-
$config = new Config();
34+
$config = new Config(['--standard=PSR1']);
9235

9336
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
9437
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
@@ -112,9 +55,9 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
11255
'show_warnings' => '0',
11356
];
11457

115-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
58+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
11659

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

11962
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
12063
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
@@ -139,9 +82,9 @@ public function testReportWidthCanBeSetFromConfFile()
13982
'report_width' => '120',
14083
];
14184

142-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
85+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
14386

144-
$config = new Config();
87+
$config = new Config(['--standard=PSR1']);
14588
$this->assertSame(120, $config->reportWidth);
14689

14790
}//end testReportWidthCanBeSetFromConfFile()
@@ -159,6 +102,7 @@ public function testReportWidthCanBeSetFromCLI()
159102
{
160103
$_SERVER['argv'] = [
161104
'phpcs',
105+
'--standard=PSR1',
162106
'--report-width=100',
163107
];
164108

@@ -180,6 +124,7 @@ public function testReportWidthWhenSetFromCLIFirstValuePrevails()
180124
{
181125
$_SERVER['argv'] = [
182126
'phpcs',
127+
'--standard=PSR1',
183128
'--report-width=100',
184129
'--report-width=200',
185130
];
@@ -209,10 +154,11 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
209154
'report_width' => '120',
210155
];
211156

212-
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
157+
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);
213158

214159
$cliArgs = [
215160
'phpcs',
161+
'--standard=PSR1',
216162
'--report-width=180',
217163
];
218164

@@ -231,7 +177,7 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
231177
*/
232178
public function testReportWidthInputHandlingForAuto()
233179
{
234-
$config = new Config();
180+
$config = new Config(['--standard=PSR1']);
235181
$config->reportWidth = 'auto';
236182

237183
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
@@ -254,7 +200,7 @@ public function testReportWidthInputHandlingForAuto()
254200
*/
255201
public function testReportWidthInputHandling($value, $expected)
256202
{
257-
$config = new Config();
203+
$config = new Config(['--standard=PSR1']);
258204
$config->reportWidth = $value;
259205

260206
$this->assertSame($expected, $config->reportWidth);
@@ -311,22 +257,4 @@ public static function dataReportWidthInputHandling()
311257
}//end dataReportWidthInputHandling()
312258

313259

314-
/**
315-
* Helper function to set a static property on the Config class.
316-
*
317-
* @param string $name The name of the property to set.
318-
* @param mixed $value The value to set the property to.
319-
*
320-
* @return void
321-
*/
322-
public static function setStaticProperty($name, $value)
323-
{
324-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
325-
$property->setAccessible(true);
326-
$property->setValue(null, $value);
327-
$property->setAccessible(false);
328-
329-
}//end setStaticProperty()
330-
331-
332260
}//end class

tests/Core/Runner/AbstractRunnerTestCase.php

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,83 +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('overriddenDefaults', []);
32-
self::setStaticConfigProperty('executablePaths', []);
33-
34-
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
35-
// from being read and influencing the tests.
36-
self::setStaticConfigProperty('configData', []);
37-
self::setStaticConfigProperty('configDataFile', '');
38-
39-
}//end setConfigStatics()
40-
41-
42-
/**
43-
* Clean up after each finished test.
44-
*
45-
* @after
46-
*
47-
* @return void
48-
*/
49-
public function clearArgv()
50-
{
51-
$_SERVER['argv'] = [];
52-
53-
}//end clearArgv()
54-
55-
56-
/**
57-
* Reset the static properties in the Config class to their true defaults to prevent this class
58-
* from influencing other tests.
59-
*
60-
* @afterClass
61-
*
62-
* @return void
63-
*/
64-
public static function reset()
65-
{
66-
self::setStaticConfigProperty('overriddenDefaults', []);
67-
self::setStaticConfigProperty('executablePaths', []);
68-
self::setStaticConfigProperty('configData', null);
69-
self::setStaticConfigProperty('configDataFile', null);
70-
$_SERVER['argv'] = [];
71-
72-
}//end reset()
73-
74-
75-
/**
76-
* Helper function to set a static property on the Config class.
77-
*
78-
* @param string $name The name of the property to set.
79-
* @param mixed $value The value to set the property to.
80-
*
81-
* @return void
82-
*/
83-
public static function setStaticConfigProperty($name, $value)
84-
{
85-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
86-
$property->setAccessible(true);
87-
$property->setValue(null, $value);
88-
$property->setAccessible(false);
89-
90-
}//end setStaticConfigProperty()
91-
92-
9319
}//end class

0 commit comments

Comments
 (0)