Skip to content

Commit f9dfb9b

Browse files
committed
ErrorSuppressionTest::testNestedSuppressLine(): 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 c14b49d commit f9dfb9b

File tree

1 file changed

+70
-77
lines changed

1 file changed

+70
-77
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -484,97 +484,90 @@ public function testSuppressLineWithinDocblock()
484484
/**
485485
* Test that using a single line ignore does not interfere with other suppressions.
486486
*
487+
* @param string $before Annotation to place before the code.
488+
* @param string $after Annotation to place after the code.
489+
*
490+
* @dataProvider dataNestedSuppressLine
491+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
492+
*
487493
* @return void
488494
*/
489-
public function testNestedSuppressLine()
495+
public function testNestedSuppressLine($before, $after)
490496
{
491-
$config = new Config();
492-
$config->standards = ['Generic'];
493-
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
494-
495-
$ruleset = new Ruleset($config);
496-
497-
// Process with disable/enable suppression and no single line suppression.
498-
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// phpcs:enable';
499-
$file = new DummyFile($content, $ruleset, $config);
500-
$file->process();
501-
502-
$errors = $file->getErrors();
503-
$numErrors = $file->getErrorCount();
504-
$this->assertEquals(0, $numErrors);
505-
$this->assertCount(0, $errors);
506-
507-
// Process with disable/enable @ suppression and no single line suppression.
508-
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// @phpcs:enable';
509-
$file = new DummyFile($content, $ruleset, $config);
510-
$file->process();
511-
512-
$errors = $file->getErrors();
513-
$numErrors = $file->getErrorCount();
514-
$this->assertEquals(0, $numErrors);
515-
$this->assertCount(0, $errors);
516-
517-
// Process with disable/enable suppression and no single line suppression (hash comment).
518-
$content = '<?php '.PHP_EOL.'# phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'# phpcs:enable';
519-
$file = new DummyFile($content, $ruleset, $config);
520-
$file->process();
521-
522-
$errors = $file->getErrors();
523-
$numErrors = $file->getErrorCount();
524-
$this->assertEquals(0, $numErrors);
525-
$this->assertCount(0, $errors);
526-
527-
// Process with disable/enable suppression and no single line suppression (deprecated syntax).
528-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// @codingStandardsIgnoreEnd';
529-
$file = new DummyFile($content, $ruleset, $config);
530-
$file->process();
531-
532-
$errors = $file->getErrors();
533-
$numErrors = $file->getErrorCount();
534-
$this->assertEquals(0, $numErrors);
535-
$this->assertCount(0, $errors);
497+
static $config, $ruleset;
536498

537-
// Process with line suppression nested within disable/enable suppression.
538-
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'// phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// phpcs:enable';
539-
$file = new DummyFile($content, $ruleset, $config);
540-
$file->process();
499+
if (isset($config, $ruleset) === false) {
500+
$config = new Config();
501+
$config->standards = ['Generic'];
502+
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
541503

542-
$errors = $file->getErrors();
543-
$numErrors = $file->getErrorCount();
544-
$this->assertEquals(0, $numErrors);
545-
$this->assertCount(0, $errors);
504+
$ruleset = new Ruleset($config);
505+
}
546506

547-
// Process with line @ suppression nested within disable/enable @ suppression.
548-
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'// @phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// @phpcs:enable';
507+
$content = <<<EOD
508+
<?php
509+
$before
510+
\$var = FALSE;
511+
\$var = TRUE;
512+
$after
513+
EOD;
549514
$file = new DummyFile($content, $ruleset, $config);
550515
$file->process();
551516

552-
$errors = $file->getErrors();
553-
$numErrors = $file->getErrorCount();
554-
$this->assertEquals(0, $numErrors);
555-
$this->assertCount(0, $errors);
517+
$this->assertSame(0, $file->getErrorCount());
518+
$this->assertCount(0, $file->getErrors());
556519

557-
// Process with line @ suppression nested within disable/enable @ suppression (hash comment).
558-
$content = '<?php '.PHP_EOL.'# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'# @phpcs:enable';
559-
$file = new DummyFile($content, $ruleset, $config);
560-
$file->process();
520+
}//end testNestedSuppressLine()
561521

562-
$errors = $file->getErrors();
563-
$numErrors = $file->getErrorCount();
564-
$this->assertEquals(0, $numErrors);
565-
$this->assertCount(0, $errors);
566522

567-
// Process with line suppression nested within disable/enable suppression (deprecated syntax).
568-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// @codingStandardsIgnoreEnd';
569-
$file = new DummyFile($content, $ruleset, $config);
570-
$file->process();
523+
/**
524+
* Data provider.
525+
*
526+
* @see testNestedSuppressLine()
527+
*
528+
* @return array
529+
*/
530+
public function dataNestedSuppressLine()
531+
{
532+
return [
533+
// Process with disable/enable suppression and no single line suppression.
534+
'disable/enable: slash comment, no single line suppression' => [
535+
'before' => '// phpcs:disable',
536+
'after' => '// phpcs:enable',
537+
],
538+
'disable/enable: slash comment, with @, no single line suppression' => [
539+
'before' => '// @phpcs:disable',
540+
'after' => '// @phpcs:enable',
541+
],
542+
'disable/enable: hash comment, no single line suppression' => [
543+
'before' => '# phpcs:disable',
544+
'after' => '# phpcs:enable',
545+
],
546+
'old style: slash comment, no single line suppression' => [
547+
'before' => '// @codingStandardsIgnoreStart',
548+
'after' => '// @codingStandardsIgnoreEnd',
549+
],
571550

572-
$errors = $file->getErrors();
573-
$numErrors = $file->getErrorCount();
574-
$this->assertEquals(0, $numErrors);
575-
$this->assertCount(0, $errors);
551+
// Process with line suppression nested within disable/enable suppression.
552+
'disable/enable: slash comment, next line nested single line suppression' => [
553+
'before' => '// phpcs:disable'.PHP_EOL.'// phpcs:ignore',
554+
'after' => '// phpcs:enable',
555+
],
556+
'disable/enable: slash comment, with @, next line nested single line suppression' => [
557+
'before' => '// @phpcs:disable'.PHP_EOL.'// @phpcs:ignore',
558+
'after' => '// @phpcs:enable',
559+
],
560+
'disable/enable: hash comment, next line nested single line suppression' => [
561+
'before' => '# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore',
562+
'after' => '# @phpcs:enable',
563+
],
564+
'old style: slash comment, next line nested single line suppression' => [
565+
'before' => '// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine',
566+
'after' => '// @codingStandardsIgnoreEnd',
567+
],
568+
];
576569

577-
}//end testNestedSuppressLine()
570+
}//end dataNestedSuppressLine()
578571

579572

580573
/**

0 commit comments

Comments
 (0)