Skip to content

Commit d465698

Browse files
committed
ErrorSuppressionTest::testSuppressLine(): refactor to data provider
* Maintains the exact same existing tests, now using a data provider. The data provider uses keys which allows for much more descriptive output when using the PHPUnit `--testdox` option, as well as for easier debugging if/when a test would fail. * Orders the tests in logical groups in the data provider. * Switches out `assertEquals()` (loose type comparison) for `assertSame()` (strict type comparison). * Caches the `$config` and `$ruleset` for the test via static local variables to prevent the test run from becoming slower due to the change to the data provider.
1 parent 8f2ec28 commit d465698

File tree

1 file changed

+74
-129
lines changed

1 file changed

+74
-129
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 74 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -331,150 +331,95 @@ public function dataSuppressWarning()
331331
/**
332332
* Test suppressing a single error using a single line ignore.
333333
*
334+
* @param string $before Annotation to place before the code.
335+
* @param string $after Optional. Annotation to place after the code.
336+
* Defaults to an empty string.
337+
* @param int $expectedErrors Optional. Number of errors expected.
338+
* Defaults to 1.
339+
*
340+
* @dataProvider dataSuppressLine
341+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
342+
*
334343
* @return void
335344
*/
336-
public function testSuppressLine()
345+
public function testSuppressLine($before, $after='', $expectedErrors=1)
337346
{
338-
$config = new Config();
339-
$config->standards = ['Generic'];
340-
$config->sniffs = [
341-
'Generic.PHP.LowerCaseConstant',
342-
'Generic.Files.LineLength',
343-
];
344-
345-
$ruleset = new Ruleset($config);
346-
347-
// Process without suppression.
348-
$content = '<?php '.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
349-
$file = new DummyFile($content, $ruleset, $config);
350-
$file->process();
351-
352-
$errors = $file->getErrors();
353-
$numErrors = $file->getErrorCount();
354-
$this->assertEquals(2, $numErrors);
355-
$this->assertCount(2, $errors);
356-
357-
// Process with suppression on line before.
358-
$content = '<?php '.PHP_EOL.'// phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
359-
$file = new DummyFile($content, $ruleset, $config);
360-
$file->process();
361-
362-
$errors = $file->getErrors();
363-
$numErrors = $file->getErrorCount();
364-
$this->assertEquals(1, $numErrors);
365-
$this->assertCount(1, $errors);
366-
367-
// Process with @ suppression on line before.
368-
$content = '<?php '.PHP_EOL.'// @phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
369-
$file = new DummyFile($content, $ruleset, $config);
370-
$file->process();
371-
372-
$errors = $file->getErrors();
373-
$numErrors = $file->getErrorCount();
374-
$this->assertEquals(1, $numErrors);
375-
$this->assertCount(1, $errors);
376-
377-
// Process with suppression on line before (hash comment).
378-
$content = '<?php '.PHP_EOL.'# phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
379-
$file = new DummyFile($content, $ruleset, $config);
380-
$file->process();
381-
382-
$errors = $file->getErrors();
383-
$numErrors = $file->getErrorCount();
384-
$this->assertEquals(1, $numErrors);
385-
$this->assertCount(1, $errors);
386-
387-
// Process with @ suppression on line before (hash comment).
388-
$content = '<?php '.PHP_EOL.'# @phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
389-
$file = new DummyFile($content, $ruleset, $config);
390-
$file->process();
391-
392-
$errors = $file->getErrors();
393-
$numErrors = $file->getErrorCount();
394-
$this->assertEquals(1, $numErrors);
395-
$this->assertCount(1, $errors);
396-
397-
// Process with suppression on line before.
398-
$content = '<?php '.PHP_EOL.'/* phpcs:ignore */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
399-
$file = new DummyFile($content, $ruleset, $config);
400-
$file->process();
401-
402-
$errors = $file->getErrors();
403-
$numErrors = $file->getErrorCount();
404-
$this->assertEquals(1, $numErrors);
405-
$this->assertCount(1, $errors);
406-
407-
// Process with @ suppression on line before.
408-
$content = '<?php '.PHP_EOL.'/* @phpcs:ignore */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
409-
$file = new DummyFile($content, $ruleset, $config);
410-
$file->process();
411-
412-
$errors = $file->getErrors();
413-
$numErrors = $file->getErrorCount();
414-
$this->assertEquals(1, $numErrors);
415-
$this->assertCount(1, $errors);
416-
417-
// Process with suppression on line before (deprecated syntax).
418-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreLine'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = FALSE;';
419-
$file = new DummyFile($content, $ruleset, $config);
420-
$file->process();
421-
422-
$errors = $file->getErrors();
423-
$numErrors = $file->getErrorCount();
424-
$this->assertEquals(1, $numErrors);
425-
$this->assertCount(1, $errors);
347+
static $config, $ruleset;
426348

427-
// Process with suppression on same line.
428-
$content = '<?php '.PHP_EOL.'$var = FALSE; // phpcs:ignore'.PHP_EOL.'$var = FALSE;';
429-
$file = new DummyFile($content, $ruleset, $config);
430-
$file->process();
349+
if (isset($config, $ruleset) === false) {
350+
$config = new Config();
351+
$config->standards = ['Generic'];
352+
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
431353

432-
$errors = $file->getErrors();
433-
$numErrors = $file->getErrorCount();
434-
$this->assertEquals(1, $numErrors);
435-
$this->assertCount(1, $errors);
354+
$ruleset = new Ruleset($config);
355+
}
436356

437-
// Process with @ suppression on same line.
438-
$content = '<?php '.PHP_EOL.'$var = FALSE; // @phpcs:ignore'.PHP_EOL.'$var = FALSE;';
357+
$content = <<<EOD
358+
<?php
359+
$before
360+
\$var = FALSE;$after
361+
\$var = FALSE;
362+
EOD;
439363
$file = new DummyFile($content, $ruleset, $config);
440364
$file->process();
441365

442-
$errors = $file->getErrors();
443-
$numErrors = $file->getErrorCount();
444-
$this->assertEquals(1, $numErrors);
445-
$this->assertCount(1, $errors);
446-
447-
// Process with suppression on same line (hash comment).
448-
$content = '<?php '.PHP_EOL.'$var = FALSE; # phpcs:ignore'.PHP_EOL.'$var = FALSE;';
449-
$file = new DummyFile($content, $ruleset, $config);
450-
$file->process();
366+
$this->assertSame($expectedErrors, $file->getErrorCount());
367+
$this->assertCount($expectedErrors, $file->getErrors());
451368

452-
$errors = $file->getErrors();
453-
$numErrors = $file->getErrorCount();
454-
$this->assertEquals(1, $numErrors);
455-
$this->assertCount(1, $errors);
369+
}//end testSuppressLine()
456370

457-
// Process with @ suppression on same line (hash comment).
458-
$content = '<?php '.PHP_EOL.'$var = FALSE; # @phpcs:ignore'.PHP_EOL.'$var = FALSE;';
459-
$file = new DummyFile($content, $ruleset, $config);
460-
$file->process();
461371

462-
$errors = $file->getErrors();
463-
$numErrors = $file->getErrorCount();
464-
$this->assertEquals(1, $numErrors);
465-
$this->assertCount(1, $errors);
372+
/**
373+
* Data provider.
374+
*
375+
* @see testSuppressLine()
376+
*
377+
* @return array
378+
*/
379+
public function dataSuppressLine()
380+
{
381+
return [
382+
'no suppression' => [
383+
'before' => '',
384+
'after' => '',
385+
'expectedErrors' => 2,
386+
],
466387

467-
// Process with suppression on same line (deprecated syntax).
468-
$content = '<?php '.PHP_EOL.'$var = FALSE; // @codingStandardsIgnoreLine'.PHP_EOL.'$var = FALSE;';
469-
$file = new DummyFile($content, $ruleset, $config);
470-
$file->process();
388+
// With suppression on line before.
389+
'ignore: line before, slash comment' => ['before' => '// phpcs:ignore'],
390+
'ignore: line before, slash comment, with @' => ['before' => '// @phpcs:ignore'],
391+
'ignore: line before, hash comment' => ['before' => '# phpcs:ignore'],
392+
'ignore: line before, hash comment, with @' => ['before' => '# @phpcs:ignore'],
393+
'ignore: line before, star comment' => ['before' => '/* phpcs:ignore */'],
394+
'ignore: line before, star comment, with @' => ['before' => '/* @phpcs:ignore */'],
395+
396+
// With suppression as trailing comment on code line.
397+
'ignore: end of line, slash comment' => [
398+
'before' => '',
399+
'after' => ' // phpcs:ignore',
400+
],
401+
'ignore: end of line, slash comment, with @' => [
402+
'before' => '',
403+
'after' => ' // @phpcs:ignore',
404+
],
405+
'ignore: end of line, hash comment' => [
406+
'before' => '',
407+
'after' => ' # phpcs:ignore',
408+
],
409+
'ignore: end of line, hash comment, with @' => [
410+
'before' => '',
411+
'after' => ' # @phpcs:ignore',
412+
],
471413

472-
$errors = $file->getErrors();
473-
$numErrors = $file->getErrorCount();
474-
$this->assertEquals(1, $numErrors);
475-
$this->assertCount(1, $errors);
414+
// Deprecated syntax.
415+
'old style: line before, slash comment' => ['before' => '// @codingStandardsIgnoreLine'],
416+
'old style: end of line, slash comment' => [
417+
'before' => '',
418+
'after' => ' // @codingStandardsIgnoreLine',
419+
],
420+
];
476421

477-
}//end testSuppressLine()
422+
}//end dataSuppressLine()
478423

479424

480425
/**

0 commit comments

Comments
 (0)