1010namespace PHP_CodeSniffer \Tests \Core \Config ;
1111
1212use 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
0 commit comments