10
10
namespace PHP_CodeSniffer \Tests \Core \Config ;
11
11
12
12
use PHP_CodeSniffer \Config ;
13
- use PHPUnit \Framework \TestCase ;
14
- use ReflectionProperty ;
13
+ use PHP_CodeSniffer \Tests \Core \Config \AbstractRealConfigTestCase ;
15
14
16
15
/**
17
16
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
18
17
*
19
18
* @covers \PHP_CodeSniffer\Config::__get
20
19
*/
21
- final class ReportWidthTest extends TestCase
20
+ final class ReportWidthTest extends AbstractRealConfigTestCase
22
21
{
23
22
24
23
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
-
71
24
/**
72
25
* Test that report width without overrules will always be set to a non-0 positive integer.
73
26
*
@@ -78,7 +31,7 @@ public static function tearDownAfterClass(): void
78
31
*/
79
32
public function testReportWidthDefault ()
80
33
{
81
- $ config = new Config ();
34
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
82
35
83
36
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
84
37
$ this ->assertIsInt ($ config ->reportWidth , 'Report width is not an integer ' );
@@ -102,9 +55,9 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
102
55
'show_warnings ' => '0 ' ,
103
56
];
104
57
105
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
58
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
106
59
107
- $ config = new Config ();
60
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
108
61
109
62
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
110
63
$ this ->assertIsInt ($ config ->reportWidth , 'Report width is not an integer ' );
@@ -129,9 +82,9 @@ public function testReportWidthCanBeSetFromConfFile()
129
82
'report_width ' => '120 ' ,
130
83
];
131
84
132
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
85
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
133
86
134
- $ config = new Config ();
87
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
135
88
$ this ->assertSame (120 , $ config ->reportWidth );
136
89
137
90
}//end testReportWidthCanBeSetFromConfFile()
@@ -149,6 +102,7 @@ public function testReportWidthCanBeSetFromCLI()
149
102
{
150
103
$ _SERVER ['argv ' ] = [
151
104
'phpcs ' ,
105
+ '--standard=PSR1 ' ,
152
106
'--report-width=100 ' ,
153
107
];
154
108
@@ -170,6 +124,7 @@ public function testReportWidthWhenSetFromCLIFirstValuePrevails()
170
124
{
171
125
$ _SERVER ['argv ' ] = [
172
126
'phpcs ' ,
127
+ '--standard=PSR1 ' ,
173
128
'--report-width=100 ' ,
174
129
'--report-width=200 ' ,
175
130
];
@@ -199,10 +154,11 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
199
154
'report_width ' => '120 ' ,
200
155
];
201
156
202
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
157
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
203
158
204
159
$ cliArgs = [
205
160
'phpcs ' ,
161
+ '--standard=PSR1 ' ,
206
162
'--report-width=180 ' ,
207
163
];
208
164
@@ -221,7 +177,7 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
221
177
*/
222
178
public function testReportWidthInputHandlingForAuto ()
223
179
{
224
- $ config = new Config ();
180
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
225
181
$ config ->reportWidth = 'auto ' ;
226
182
227
183
// 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()
244
200
*/
245
201
public function testReportWidthInputHandling ($ value , $ expected )
246
202
{
247
- $ config = new Config ();
203
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
248
204
$ config ->reportWidth = $ value ;
249
205
250
206
$ this ->assertSame ($ expected , $ config ->reportWidth );
@@ -301,22 +257,4 @@ public static function dataReportWidthInputHandling()
301
257
}//end dataReportWidthInputHandling()
302
258
303
259
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
-
322
260
}//end class
0 commit comments