@@ -247,77 +247,85 @@ public function dataSuppressSomeErrors()
247
247
/**
248
248
* Test suppressing a single warning.
249
249
*
250
+ * @param string $before Annotation to place before the code.
251
+ * @param string $after Annotation to place after the code.
252
+ * @param int $expectedWarnings Optional. Number of warnings expected.
253
+ * Defaults to 0.
254
+ *
255
+ * @dataProvider dataSuppressWarning
256
+ * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
257
+ *
250
258
* @return void
251
259
*/
252
- public function testSuppressWarning ()
260
+ public function testSuppressWarning ($ before , $ after , $ expectedWarnings = 0 )
253
261
{
254
- $ config = new Config ();
255
- $ config ->standards = ['Generic ' ];
256
- $ config ->sniffs = ['Generic.Commenting.Todo ' ];
257
-
258
- $ ruleset = new Ruleset ($ config );
259
-
260
- // Process without suppression.
261
- $ content = '<?php ' .PHP_EOL .'//TODO: write some code ' ;
262
- $ file = new DummyFile ($ content , $ ruleset , $ config );
263
- $ file ->process ();
264
-
265
- $ warnings = $ file ->getWarnings ();
266
- $ numWarnings = $ file ->getWarningCount ();
267
- $ this ->assertEquals (1 , $ numWarnings );
268
- $ this ->assertCount (1 , $ warnings );
262
+ static $ config , $ ruleset ;
269
263
270
- // Process with suppression.
271
- $ content = ' <?php ' . PHP_EOL . ' // phpcs:disable ' . PHP_EOL . ' //TODO: write some code ' . PHP_EOL . ' // phpcs:enable ' ;
272
- $ file = new DummyFile ( $ content , $ ruleset , $ config ) ;
273
- $ file -> process () ;
264
+ if ( isset ( $ config , $ ruleset ) === false ) {
265
+ $ config = new Config () ;
266
+ $ config -> standards = [ ' Generic ' ] ;
267
+ $ config -> sniffs = [ ' Generic.Commenting.Todo ' ] ;
274
268
275
- $ warnings = $ file ->getWarnings ();
276
- $ numWarnings = $ file ->getWarningCount ();
277
- $ this ->assertEquals (0 , $ numWarnings );
278
- $ this ->assertCount (0 , $ warnings );
269
+ $ ruleset = new Ruleset ($ config );
270
+ }
279
271
280
- // Process with @ suppression.
281
- $ content = '<?php ' .PHP_EOL .'// @phpcs:disable ' .PHP_EOL .'//TODO: write some code ' .PHP_EOL .'// @phpcs:enable ' ;
272
+ $ content = <<<EOD
273
+ <?php
274
+ $ before
275
+ //TODO: write some code.
276
+ $ after
277
+ EOD ;
282
278
$ file = new DummyFile ($ content , $ ruleset , $ config );
283
279
$ file ->process ();
284
280
285
- $ warnings = $ file ->getWarnings ();
286
- $ numWarnings = $ file ->getWarningCount ();
287
- $ this ->assertEquals (0 , $ numWarnings );
288
- $ this ->assertCount (0 , $ warnings );
281
+ $ this ->assertSame ($ expectedWarnings , $ file ->getWarningCount ());
282
+ $ this ->assertCount ($ expectedWarnings , $ file ->getWarnings ());
289
283
290
- // Process with suppression (deprecated syntax).
291
- $ content = '<?php ' .PHP_EOL .'// @codingStandardsIgnoreStart ' .PHP_EOL .'//TODO: write some code ' .PHP_EOL .'// @codingStandardsIgnoreEnd ' ;
292
- $ file = new DummyFile ($ content , $ ruleset , $ config );
293
- $ file ->process ();
294
-
295
- $ warnings = $ file ->getWarnings ();
296
- $ numWarnings = $ file ->getWarningCount ();
297
- $ this ->assertEquals (0 , $ numWarnings );
298
- $ this ->assertCount (0 , $ warnings );
284
+ }//end testSuppressWarning()
299
285
300
- // Process with a docblock suppression.
301
- $ content = '<?php ' .PHP_EOL .'/** phpcs:disable */ ' .PHP_EOL .'//TODO: write some code ' .PHP_EOL .'/** phpcs:enable */ ' ;
302
- $ file = new DummyFile ($ content , $ ruleset , $ config );
303
- $ file ->process ();
304
286
305
- $ warnings = $ file ->getWarnings ();
306
- $ numWarnings = $ file ->getWarningCount ();
307
- $ this ->assertEquals (0 , $ numWarnings );
308
- $ this ->assertCount (0 , $ warnings );
287
+ /**
288
+ * Data provider.
289
+ *
290
+ * @see testSuppressWarning()
291
+ *
292
+ * @return array
293
+ */
294
+ public function dataSuppressWarning ()
295
+ {
296
+ return [
297
+ 'no suppression ' => [
298
+ 'before ' => '' ,
299
+ 'after ' => '' ,
300
+ 'expectedWarnings ' => 1 ,
301
+ ],
309
302
310
- // Process with a docblock suppression (deprecated syntax).
311
- $ content = '<?php ' .PHP_EOL .'/** @codingStandardsIgnoreStart */ ' .PHP_EOL .'//TODO: write some code ' .PHP_EOL .'/** @codingStandardsIgnoreEnd */ ' ;
312
- $ file = new DummyFile ($ content , $ ruleset , $ config );
313
- $ file ->process ();
303
+ // With suppression.
304
+ 'disable/enable: slash comment ' => [
305
+ 'before ' => '// phpcs:disable ' ,
306
+ 'after ' => '// phpcs:enable ' ,
307
+ ],
308
+ 'disable/enable: slash comment, with @ ' => [
309
+ 'before ' => '// @phpcs:disable ' ,
310
+ 'after ' => '// @phpcs:enable ' ,
311
+ ],
312
+ 'disable/enable: single line docblock comment ' => [
313
+ 'before ' => '/** phpcs:disable */ ' ,
314
+ 'after ' => '/** phpcs:enable */ ' ,
315
+ ],
314
316
315
- $ warnings = $ file ->getWarnings ();
316
- $ numWarnings = $ file ->getWarningCount ();
317
- $ this ->assertEquals (0 , $ numWarnings );
318
- $ this ->assertCount (0 , $ warnings );
317
+ // Deprecated syntax.
318
+ 'old style: slash comment ' => [
319
+ 'before ' => '// @codingStandardsIgnoreStart ' ,
320
+ 'after ' => '// @codingStandardsIgnoreEnd ' ,
321
+ ],
322
+ 'old style: single line docblock comment ' => [
323
+ 'before ' => '/** @codingStandardsIgnoreStart */ ' ,
324
+ 'after ' => '/** @codingStandardsIgnoreEnd */ ' ,
325
+ ],
326
+ ];
319
327
320
- }//end testSuppressWarning ()
328
+ }//end dataSuppressWarning ()
321
329
322
330
323
331
/**
0 commit comments