@@ -21,197 +21,134 @@ class ErrorSuppressionTest extends TestCase
2121 /**
2222 * Test suppressing a single error.
2323 *
24+ * @param string $before Annotation to place before the code.
25+ * @param string $after Annotation to place after the code.
26+ * @param int $expectedErrors Optional. Number of errors expected.
27+ * Defaults to 0.
28+ *
29+ * @dataProvider dataSuppressError
30+ * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
31+ *
2432 * @return void
2533 */
26- public function testSuppressError ()
34+ public function testSuppressError ($ before , $ after , $ expectedErrors = 0 )
2735 {
28- $ config = new Config ();
29- $ config ->standards = ['Generic ' ];
30- $ config ->sniffs = ['Generic.PHP.LowerCaseConstant ' ];
36+ static $ config , $ ruleset ;
3137
32- $ ruleset = new Ruleset ($ config );
33-
34- // Process without suppression.
35- $ content = '<?php ' .PHP_EOL .'$var = FALSE; ' ;
36- $ file = new DummyFile ($ content , $ ruleset , $ config );
37- $ file ->process ();
38+ if (isset ($ config , $ ruleset ) === false ) {
39+ $ config = new Config ();
40+ $ config ->standards = ['Generic ' ];
41+ $ config ->sniffs = ['Generic.PHP.LowerCaseConstant ' ];
3842
39- $ errors = $ file ->getErrors ();
40- $ numErrors = $ file ->getErrorCount ();
41- $ this ->assertEquals (1 , $ numErrors );
42- $ this ->assertCount (1 , $ errors );
43+ $ ruleset = new Ruleset ($ config );
44+ }
4345
44- // Process with inline comment suppression.
45- $ content = '<?php ' .PHP_EOL .'// phpcs:disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'// phpcs:enable ' ;
46+ $ content = '<?php ' .PHP_EOL .$ before .'$var = FALSE; ' .PHP_EOL .$ after ;
4647 $ file = new DummyFile ($ content , $ ruleset , $ config );
4748 $ file ->process ();
4849
49- $ errors = $ file ->getErrors ();
50- $ numErrors = $ file ->getErrorCount ();
51- $ this ->assertEquals (0 , $ numErrors );
52- $ this ->assertCount (0 , $ errors );
53-
54- // Process with multi-line inline comment suppression, tab-indented.
55- $ content = '<?php ' .PHP_EOL ."\t" .'// For reasons ' .PHP_EOL ."\t" .'// phpcs:disable ' .PHP_EOL ."\t" .'$var = FALSE; ' .PHP_EOL ."\t" .'// phpcs:enable ' ;
56- $ file = new DummyFile ($ content , $ ruleset , $ config );
57- $ file ->process ();
58-
59- $ errors = $ file ->getErrors ();
60- $ numErrors = $ file ->getErrorCount ();
61- $ this ->assertEquals (0 , $ numErrors );
62- $ this ->assertCount (0 , $ errors );
63-
64- // Process with inline @ comment suppression.
65- $ content = '<?php ' .PHP_EOL .'// @phpcs:disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'// @phpcs:enable ' ;
66- $ file = new DummyFile ($ content , $ ruleset , $ config );
67- $ file ->process ();
68-
69- $ errors = $ file ->getErrors ();
70- $ numErrors = $ file ->getErrorCount ();
71- $ this ->assertEquals (0 , $ numErrors );
72- $ this ->assertCount (0 , $ errors );
73-
74- // Process with inline comment suppression mixed case.
75- $ content = '<?php ' .PHP_EOL .'// PHPCS:Disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'// pHPcs:enabLE ' ;
76- $ file = new DummyFile ($ content , $ ruleset , $ config );
77- $ file ->process ();
78-
79- $ errors = $ file ->getErrors ();
80- $ numErrors = $ file ->getErrorCount ();
81- $ this ->assertEquals (0 , $ numErrors );
82- $ this ->assertCount (0 , $ errors );
83-
84- // Process with inline hash comment suppression.
85- $ content = '<?php ' .PHP_EOL .'# phpcs:disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'# phpcs:enable ' ;
86- $ file = new DummyFile ($ content , $ ruleset , $ config );
87- $ file ->process ();
88-
89- $ errors = $ file ->getErrors ();
90- $ numErrors = $ file ->getErrorCount ();
91- $ this ->assertEquals (0 , $ numErrors );
92- $ this ->assertCount (0 , $ errors );
93-
94- // Process with multi-line inline hash comment suppression, tab-indented.
95- $ content = '<?php ' .PHP_EOL ."\t" .'# For reasons ' .PHP_EOL ."\t" .'# phpcs:disable ' .PHP_EOL ."\t" .'$var = FALSE; ' .PHP_EOL ."\t" .'# phpcs:enable ' ;
96- $ file = new DummyFile ($ content , $ ruleset , $ config );
97- $ file ->process ();
98-
99- $ errors = $ file ->getErrors ();
100- $ numErrors = $ file ->getErrorCount ();
101- $ this ->assertEquals (0 , $ numErrors );
102- $ this ->assertCount (0 , $ errors );
103-
104- // Process with inline hash @ comment suppression.
105- $ content = '<?php ' .PHP_EOL .'# @phpcs:disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'# @phpcs:enable ' ;
106- $ file = new DummyFile ($ content , $ ruleset , $ config );
107- $ file ->process ();
108-
109- $ errors = $ file ->getErrors ();
110- $ numErrors = $ file ->getErrorCount ();
111- $ this ->assertEquals (0 , $ numErrors );
112- $ this ->assertCount (0 , $ errors );
113-
114- // Process with inline hash comment suppression mixed case.
115- $ content = '<?php ' .PHP_EOL .'# PHPCS:Disable ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'# pHPcs:enabLE ' ;
116- $ file = new DummyFile ($ content , $ ruleset , $ config );
117- $ file ->process ();
118-
119- $ errors = $ file ->getErrors ();
120- $ numErrors = $ file ->getErrorCount ();
121- $ this ->assertEquals (0 , $ numErrors );
122- $ this ->assertCount (0 , $ errors );
123-
124- // Process with inline comment suppression (deprecated syntax).
125- $ content = '<?php ' .PHP_EOL .'// @codingStandardsIgnoreStart ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'// @codingStandardsIgnoreEnd ' ;
126- $ file = new DummyFile ($ content , $ ruleset , $ config );
127- $ file ->process ();
50+ $ this ->assertSame ($ expectedErrors , $ file ->getErrorCount ());
51+ $ this ->assertCount ($ expectedErrors , $ file ->getErrors ());
12852
129- $ errors = $ file ->getErrors ();
130- $ numErrors = $ file ->getErrorCount ();
131- $ this ->assertEquals (0 , $ numErrors );
132- $ this ->assertCount (0 , $ errors );
133-
134- // Process with block comment suppression.
135- $ content = '<?php ' .PHP_EOL .'/* phpcs:disable */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/* phpcs:enable */ ' ;
136- $ file = new DummyFile ($ content , $ ruleset , $ config );
137- $ file ->process ();
138-
139- $ errors = $ file ->getErrors ();
140- $ numErrors = $ file ->getErrorCount ();
141- $ this ->assertEquals (0 , $ numErrors );
142- $ this ->assertCount (0 , $ errors );
143-
144- // Process with multi-line block comment suppression.
145- $ content = '<?php ' .PHP_EOL .'/* ' .PHP_EOL .' phpcs:disable ' .PHP_EOL .' */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/* ' .PHP_EOL .' phpcs:enable ' .PHP_EOL .' */ ' ;
146- $ file = new DummyFile ($ content , $ ruleset , $ config );
147- $ file ->process ();
148-
149- $ errors = $ file ->getErrors ();
150- $ numErrors = $ file ->getErrorCount ();
151- $ this ->assertEquals (0 , $ numErrors );
152- $ this ->assertCount (0 , $ errors );
153-
154- // Process with multi-line block comment suppression, each line starred.
155- $ content = '<?php ' .PHP_EOL .'/* ' .PHP_EOL .' * phpcs:disable ' .PHP_EOL .' */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/* ' .PHP_EOL .' * phpcs:enable ' .PHP_EOL .' */ ' ;
156- $ file = new DummyFile ($ content , $ ruleset , $ config );
157- $ file ->process ();
158-
159- $ errors = $ file ->getErrors ();
160- $ numErrors = $ file ->getErrorCount ();
161- $ this ->assertEquals (0 , $ numErrors );
162- $ this ->assertCount (0 , $ errors );
163-
164- // Process with multi-line block comment suppression, tab-indented.
165- $ content = '<?php ' .PHP_EOL ."\t" .'/* ' .PHP_EOL ."\t" .' * phpcs:disable ' .PHP_EOL ."\t" .' */ ' .PHP_EOL ."\t" .'$var = FALSE; ' .PHP_EOL ."\t" .'/* ' .PHP_EOL .' * phpcs:enable ' .PHP_EOL .' */ ' ;
166- $ file = new DummyFile ($ content , $ ruleset , $ config );
167- $ file ->process ();
168-
169- $ errors = $ file ->getErrors ();
170- $ numErrors = $ file ->getErrorCount ();
171- $ this ->assertEquals (0 , $ numErrors );
172- $ this ->assertCount (0 , $ errors );
173-
174- // Process with block comment suppression (deprecated syntax).
175- $ content = '<?php ' .PHP_EOL .'/* @codingStandardsIgnoreStart */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/* @codingStandardsIgnoreEnd */ ' ;
176- $ file = new DummyFile ($ content , $ ruleset , $ config );
177- $ file ->process ();
178-
179- $ errors = $ file ->getErrors ();
180- $ numErrors = $ file ->getErrorCount ();
181- $ this ->assertEquals (0 , $ numErrors );
182- $ this ->assertCount (0 , $ errors );
183-
184- // Process with multi-line block comment suppression (deprecated syntax).
185- $ content = '<?php ' .PHP_EOL .'/* ' .PHP_EOL .' @codingStandardsIgnoreStart ' .PHP_EOL .' */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/* ' .PHP_EOL .' @codingStandardsIgnoreEnd ' .PHP_EOL .' */ ' ;
186- $ file = new DummyFile ($ content , $ ruleset , $ config );
187- $ file ->process ();
188-
189- $ errors = $ file ->getErrors ();
190- $ numErrors = $ file ->getErrorCount ();
191- $ this ->assertEquals (0 , $ numErrors );
192- $ this ->assertCount (0 , $ errors );
193-
194- // Process with a docblock suppression.
195- $ content = '<?php ' .PHP_EOL .'/** phpcs:disable */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/** phpcs:enable */ ' ;
196- $ file = new DummyFile ($ content , $ ruleset , $ config );
197- $ file ->process ();
198-
199- $ errors = $ file ->getErrors ();
200- $ numErrors = $ file ->getErrorCount ();
201- $ this ->assertEquals (0 , $ numErrors );
202- $ this ->assertCount (0 , $ errors );
53+ }//end testSuppressError()
20354
204- // Process with a docblock suppression (deprecated syntax).
205- $ content = '<?php ' .PHP_EOL .'/** @codingStandardsIgnoreStart */ ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'/** @codingStandardsIgnoreEnd */ ' ;
206- $ file = new DummyFile ($ content , $ ruleset , $ config );
207- $ file ->process ();
20855
209- $ errors = $ file ->getErrors ();
210- $ numErrors = $ file ->getErrorCount ();
211- $ this ->assertEquals (0 , $ numErrors );
212- $ this ->assertCount (0 , $ errors );
56+ /**
57+ * Data provider.
58+ *
59+ * @see testSuppressError()
60+ *
61+ * @return array
62+ */
63+ public function dataSuppressError ()
64+ {
65+ return [
66+ 'no suppression ' => [
67+ 'before ' => '' ,
68+ 'after ' => '' ,
69+ 'expectedErrors ' => 1 ,
70+ ],
71+
72+ // Inline slash comments.
73+ 'disable/enable: slash comment ' => [
74+ 'before ' => '// phpcs:disable ' .PHP_EOL ,
75+ 'after ' => '// phpcs:enable ' ,
76+ ],
77+ 'disable/enable: multi-line slash comment, tab indented ' => [
78+ 'before ' => "\t" .'// For reasons ' .PHP_EOL ."\t" .'// phpcs:disable ' .PHP_EOL ."\t" ,
79+ 'after ' => "\t" .'// phpcs:enable ' ,
80+ ],
81+ 'disable/enable: slash comment, with @ ' => [
82+ 'before ' => '// @phpcs:disable ' .PHP_EOL ,
83+ 'after ' => '// @phpcs:enable ' ,
84+ ],
85+ 'disable/enable: slash comment, mixed case ' => [
86+ 'before ' => '// PHPCS:Disable ' .PHP_EOL ,
87+ 'after ' => '// pHPcs:enabLE ' ,
88+ ],
89+
90+ // Inline hash comments.
91+ 'disable/enable: hash comment ' => [
92+ 'before ' => '# phpcs:disable ' .PHP_EOL ,
93+ 'after ' => '# phpcs:enable ' ,
94+ ],
95+ 'disable/enable: multi-line hash comment, tab indented ' => [
96+ 'before ' => "\t" .'# For reasons ' .PHP_EOL ."\t" .'# phpcs:disable ' .PHP_EOL ."\t" ,
97+ 'after ' => "\t" .'# phpcs:enable ' ,
98+ ],
99+ 'disable/enable: hash comment, with @ ' => [
100+ 'before ' => '# @phpcs:disable ' .PHP_EOL ,
101+ 'after ' => '# @phpcs:enable ' ,
102+ ],
103+ 'disable/enable: hash comment, mixed case ' => [
104+ 'before ' => '# PHPCS:Disable ' .PHP_EOL ,
105+ 'after ' => '# pHPcs:enabLE ' ,
106+ ],
107+
108+ // Inline star (block) comments.
109+ 'disable/enable: star comment ' => [
110+ 'before ' => '/* phpcs:disable */ ' .PHP_EOL ,
111+ 'after ' => '/* phpcs:enable */ ' ,
112+ ],
113+ 'disable/enable: multi-line star comment ' => [
114+ 'before ' => '/* ' .PHP_EOL .' phpcs:disable ' .PHP_EOL .' */ ' .PHP_EOL ,
115+ 'after ' => '/* ' .PHP_EOL .' phpcs:enable ' .PHP_EOL .' */ ' ,
116+ ],
117+ 'disable/enable: multi-line star comment, each line starred ' => [
118+ 'before ' => '/* ' .PHP_EOL .' * phpcs:disable ' .PHP_EOL .' */ ' .PHP_EOL ,
119+ 'after ' => '/* ' .PHP_EOL .' * phpcs:enable ' .PHP_EOL .' */ ' ,
120+ ],
121+ 'disable/enable: multi-line star comment, each line starred, tab indented ' => [
122+ 'before ' => "\t" .'/* ' .PHP_EOL ."\t" .' * phpcs:disable ' .PHP_EOL ."\t" .' */ ' .PHP_EOL ."\t" ,
123+ 'after ' => "\t" .'/* ' .PHP_EOL .' * phpcs:enable ' .PHP_EOL .' */ ' ,
124+ ],
125+
126+ // Docblock comments.
127+ 'disable/enable: single line docblock comment ' => [
128+ 'before ' => '/** phpcs:disable */ ' .PHP_EOL ,
129+ 'after ' => '/** phpcs:enable */ ' ,
130+ ],
131+
132+ // Deprecated syntax.
133+ 'old style: slash comment ' => [
134+ 'before ' => '// @codingStandardsIgnoreStart ' .PHP_EOL ,
135+ 'after ' => '// @codingStandardsIgnoreEnd ' ,
136+ ],
137+ 'old style: star comment ' => [
138+ 'before ' => '/* @codingStandardsIgnoreStart */ ' .PHP_EOL ,
139+ 'after ' => '/* @codingStandardsIgnoreEnd */ ' ,
140+ ],
141+ 'old style: multi-line star comment ' => [
142+ 'before ' => '/* ' .PHP_EOL .' @codingStandardsIgnoreStart ' .PHP_EOL .' */ ' .PHP_EOL ,
143+ 'after ' => '/* ' .PHP_EOL .' @codingStandardsIgnoreEnd ' .PHP_EOL .' */ ' ,
144+ ],
145+ 'old style: single line docblock comment ' => [
146+ 'before ' => '/** @codingStandardsIgnoreStart */ ' .PHP_EOL ,
147+ 'after ' => '/** @codingStandardsIgnoreEnd */ ' ,
148+ ],
149+ ];
213150
214- }//end testSuppressError ()
151+ }//end dataSuppressError ()
215152
216153
217154 /**
0 commit comments