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
- * @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
-
81
24
/**
82
25
* Test that report width without overrules will always be set to a non-0 positive integer.
83
26
*
@@ -88,7 +31,7 @@ public static function resetConfigToDefaults()
88
31
*/
89
32
public function testReportWidthDefault ()
90
33
{
91
- $ config = new Config ();
34
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
92
35
93
36
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
94
37
$ this ->assertTrue (is_int ($ config ->reportWidth ), 'Report width is not an integer ' );
@@ -112,9 +55,9 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
112
55
'show_warnings ' => '0 ' ,
113
56
];
114
57
115
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
58
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
116
59
117
- $ config = new Config ();
60
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
118
61
119
62
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
120
63
$ this ->assertTrue (is_int ($ config ->reportWidth ), 'Report width is not an integer ' );
@@ -139,9 +82,9 @@ public function testReportWidthCanBeSetFromConfFile()
139
82
'report_width ' => '120 ' ,
140
83
];
141
84
142
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
85
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
143
86
144
- $ config = new Config ();
87
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
145
88
$ this ->assertSame (120 , $ config ->reportWidth );
146
89
147
90
}//end testReportWidthCanBeSetFromConfFile()
@@ -159,6 +102,7 @@ public function testReportWidthCanBeSetFromCLI()
159
102
{
160
103
$ _SERVER ['argv ' ] = [
161
104
'phpcs ' ,
105
+ '--standard=PSR1 ' ,
162
106
'--report-width=100 ' ,
163
107
];
164
108
@@ -180,6 +124,7 @@ public function testReportWidthWhenSetFromCLIFirstValuePrevails()
180
124
{
181
125
$ _SERVER ['argv ' ] = [
182
126
'phpcs ' ,
127
+ '--standard=PSR1 ' ,
183
128
'--report-width=100 ' ,
184
129
'--report-width=200 ' ,
185
130
];
@@ -209,10 +154,11 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
209
154
'report_width ' => '120 ' ,
210
155
];
211
156
212
- $ this ->setStaticProperty ('configData ' , $ phpCodeSnifferConfig );
157
+ $ this ->setStaticConfigProperty ('configData ' , $ phpCodeSnifferConfig );
213
158
214
159
$ cliArgs = [
215
160
'phpcs ' ,
161
+ '--standard=PSR1 ' ,
216
162
'--report-width=180 ' ,
217
163
];
218
164
@@ -231,7 +177,7 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
231
177
*/
232
178
public function testReportWidthInputHandlingForAuto ()
233
179
{
234
- $ config = new Config ();
180
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
235
181
$ config ->reportWidth = 'auto ' ;
236
182
237
183
// 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()
254
200
*/
255
201
public function testReportWidthInputHandling ($ value , $ expected )
256
202
{
257
- $ config = new Config ();
203
+ $ config = new Config ([ ' --standard=PSR1 ' ] );
258
204
$ config ->reportWidth = $ value ;
259
205
260
206
$ this ->assertSame ($ expected , $ config ->reportWidth );
@@ -311,22 +257,4 @@ public static function dataReportWidthInputHandling()
311
257
}//end dataReportWidthInputHandling()
312
258
313
259
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
-
332
260
}//end class
0 commit comments