Skip to content

Commit 99d83b1

Browse files
committed
ErrorSuppressionTest::testSuppressFile(): 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 e67a5df commit 99d83b1

File tree

1 file changed

+73
-144
lines changed

1 file changed

+73
-144
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 73 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -670,167 +670,96 @@ public function dataSuppressScope()
670670
/**
671671
* Test suppressing a whole file.
672672
*
673+
* @param string $before Annotation to place before the code.
674+
* @param string $after Optional. Annotation to place after the code.
675+
* Defaults to an empty string.
676+
* @param int $expectedWarnings Optional. Number of warnings expected.
677+
* Defaults to 0.
678+
*
679+
* @dataProvider dataSuppressFile
680+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
681+
*
673682
* @return void
674683
*/
675-
public function testSuppressFile()
684+
public function testSuppressFile($before, $after='', $expectedWarnings=0)
676685
{
677-
$config = new Config();
678-
$config->standards = ['Generic'];
679-
$config->sniffs = ['Generic.Commenting.Todo'];
680-
681-
$ruleset = new Ruleset($config);
682-
683-
// Process without suppression.
684-
$content = '<?php '.PHP_EOL.'//TODO: write some code';
685-
$file = new DummyFile($content, $ruleset, $config);
686-
$file->process();
687-
688-
$warnings = $file->getWarnings();
689-
$numWarnings = $file->getWarningCount();
690-
$this->assertEquals(1, $numWarnings);
691-
$this->assertCount(1, $warnings);
692-
693-
// Process with suppression.
694-
$content = '<?php '.PHP_EOL.'// phpcs:ignoreFile'.PHP_EOL.'//TODO: write some code';
695-
$file = new DummyFile($content, $ruleset, $config);
696-
$file->process();
697-
698-
$warnings = $file->getWarnings();
699-
$numWarnings = $file->getWarningCount();
700-
$this->assertEquals(0, $numWarnings);
701-
$this->assertCount(0, $warnings);
702-
703-
// Process with @ suppression.
704-
$content = '<?php '.PHP_EOL.'// @phpcs:ignoreFile'.PHP_EOL.'//TODO: write some code';
705-
$file = new DummyFile($content, $ruleset, $config);
706-
$file->process();
707-
708-
$warnings = $file->getWarnings();
709-
$numWarnings = $file->getWarningCount();
710-
$this->assertEquals(0, $numWarnings);
711-
$this->assertCount(0, $warnings);
712-
713-
// Process with suppression (hash comment).
714-
$content = '<?php '.PHP_EOL.'# phpcs:ignoreFile'.PHP_EOL.'//TODO: write some code';
715-
$file = new DummyFile($content, $ruleset, $config);
716-
$file->process();
717-
718-
$warnings = $file->getWarnings();
719-
$numWarnings = $file->getWarningCount();
720-
$this->assertEquals(0, $numWarnings);
721-
$this->assertCount(0, $warnings);
722-
723-
// Process with @ suppression (hash comment).
724-
$content = '<?php '.PHP_EOL.'# @phpcs:ignoreFile'.PHP_EOL.'//TODO: write some code';
725-
$file = new DummyFile($content, $ruleset, $config);
726-
$file->process();
727-
728-
$warnings = $file->getWarnings();
729-
$numWarnings = $file->getWarningCount();
730-
$this->assertEquals(0, $numWarnings);
731-
$this->assertCount(0, $warnings);
732-
733-
// Process with suppression (deprecated syntax).
734-
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreFile'.PHP_EOL.'//TODO: write some code';
735-
$file = new DummyFile($content, $ruleset, $config);
736-
$file->process();
737-
738-
$warnings = $file->getWarnings();
739-
$numWarnings = $file->getWarningCount();
740-
$this->assertEquals(0, $numWarnings);
741-
$this->assertCount(0, $warnings);
742-
743-
// Process mixed case.
744-
$content = '<?php '.PHP_EOL.'// PHPCS:Ignorefile'.PHP_EOL.'//TODO: write some code';
745-
$file = new DummyFile($content, $ruleset, $config);
746-
$file->process();
747-
748-
$warnings = $file->getWarnings();
749-
$numWarnings = $file->getWarningCount();
750-
$this->assertEquals(0, $numWarnings);
751-
$this->assertCount(0, $warnings);
752-
753-
// Process late comment.
754-
$content = '<?php '.PHP_EOL.'class MyClass {}'.PHP_EOL.'$foo = new MyClass();'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:ignoreFile';
755-
$file = new DummyFile($content, $ruleset, $config);
756-
$file->process();
757-
758-
$warnings = $file->getWarnings();
759-
$numWarnings = $file->getWarningCount();
760-
$this->assertEquals(0, $numWarnings);
761-
$this->assertCount(0, $warnings);
762-
763-
// Process late comment (deprecated syntax).
764-
$content = '<?php '.PHP_EOL.'class MyClass {}'.PHP_EOL.'$foo = new MyClass();'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @codingStandardsIgnoreFile';
765-
$file = new DummyFile($content, $ruleset, $config);
766-
$file->process();
767-
768-
$warnings = $file->getWarnings();
769-
$numWarnings = $file->getWarningCount();
770-
$this->assertEquals(0, $numWarnings);
771-
$this->assertCount(0, $warnings);
686+
static $config, $ruleset;
772687

773-
// Process with a block comment suppression.
774-
$content = '<?php '.PHP_EOL.'/* phpcs:ignoreFile */'.PHP_EOL.'//TODO: write some code';
775-
$file = new DummyFile($content, $ruleset, $config);
776-
$file->process();
688+
if (isset($config, $ruleset) === false) {
689+
$config = new Config();
690+
$config->standards = ['Generic'];
691+
$config->sniffs = ['Generic.Commenting.Todo'];
777692

778-
$warnings = $file->getWarnings();
779-
$numWarnings = $file->getWarningCount();
780-
$this->assertEquals(0, $numWarnings);
781-
$this->assertCount(0, $warnings);
693+
$ruleset = new Ruleset($config);
694+
}
782695

783-
// Process with a multi-line block comment suppression.
784-
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' phpcs:ignoreFile'.PHP_EOL.' */'.PHP_EOL.'//TODO: write some code';
696+
$content = <<<EOD
697+
<?php
698+
$before
699+
class MyClass {}
700+
\$foo = new MyClass();
701+
//TODO: write some code
702+
$after
703+
EOD;
785704
$file = new DummyFile($content, $ruleset, $config);
786705
$file->process();
787706

788-
$warnings = $file->getWarnings();
789-
$numWarnings = $file->getWarningCount();
790-
$this->assertEquals(0, $numWarnings);
791-
$this->assertCount(0, $warnings);
792-
793-
// Process with a block comment suppression (deprecated syntax).
794-
$content = '<?php '.PHP_EOL.'/* @codingStandardsIgnoreFile */'.PHP_EOL.'//TODO: write some code';
795-
$file = new DummyFile($content, $ruleset, $config);
796-
$file->process();
707+
$this->assertSame($expectedWarnings, $file->getWarningCount());
708+
$this->assertCount($expectedWarnings, $file->getWarnings());
797709

798-
$warnings = $file->getWarnings();
799-
$numWarnings = $file->getWarningCount();
800-
$this->assertEquals(0, $numWarnings);
801-
$this->assertCount(0, $warnings);
710+
}//end testSuppressFile()
802711

803-
// Process with a multi-line block comment suppression (deprecated syntax).
804-
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreFile'.PHP_EOL.' */'.PHP_EOL.'//TODO: write some code';
805-
$file = new DummyFile($content, $ruleset, $config);
806-
$file->process();
807712

808-
$warnings = $file->getWarnings();
809-
$numWarnings = $file->getWarningCount();
810-
$this->assertEquals(0, $numWarnings);
811-
$this->assertCount(0, $warnings);
713+
/**
714+
* Data provider.
715+
*
716+
* @see testSuppressFile()
717+
*
718+
* @return array
719+
*/
720+
public function dataSuppressFile()
721+
{
722+
return [
723+
'no suppression' => [
724+
'before' => '',
725+
'after' => '',
726+
'expectedErrors' => 1,
727+
],
812728

813-
// Process with docblock suppression.
814-
$content = '<?php '.PHP_EOL.'/** phpcs:ignoreFile */'.PHP_EOL.'//TODO: write some code';
815-
$file = new DummyFile($content, $ruleset, $config);
816-
$file->process();
729+
// Process with suppression.
730+
'ignoreFile: start of file, slash comment' => ['before' => '// phpcs:ignoreFile'],
731+
'ignoreFile: start of file, slash comment, with @' => ['before' => '// @phpcs:ignoreFile'],
732+
'ignoreFile: start of file, slash comment, mixed case' => ['before' => '// PHPCS:Ignorefile'],
733+
'ignoreFile: start of file, hash comment' => ['before' => '# phpcs:ignoreFile'],
734+
'ignoreFile: start of file, hash comment, with @' => ['before' => '# @phpcs:ignoreFile'],
735+
'ignoreFile: start of file, single-line star comment' => ['before' => '/* phpcs:ignoreFile */'],
736+
'ignoreFile: start of file, multi-line star comment' => [
737+
'before' => '/*'.PHP_EOL.' phpcs:ignoreFile'.PHP_EOL.' */',
738+
],
739+
'ignoreFile: start of file, single-line docblock comment' => ['before' => '/** phpcs:ignoreFile */'],
817740

818-
$warnings = $file->getWarnings();
819-
$numWarnings = $file->getWarningCount();
820-
$this->assertEquals(0, $numWarnings);
821-
$this->assertCount(0, $warnings);
741+
// Process late comment.
742+
'ignoreFile: late comment, slash comment' => [
743+
'before' => '',
744+
'after' => '// phpcs:ignoreFile',
745+
],
822746

823-
// Process with docblock suppression (deprecated syntax).
824-
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreFile */'.PHP_EOL.'//TODO: write some code';
825-
$file = new DummyFile($content, $ruleset, $config);
826-
$file->process();
747+
// Deprecated syntax.
748+
'old style: start of file, slash comment' => ['before' => '// @codingStandardsIgnoreFile'],
749+
'old style: start of file, single-line star comment' => ['before' => '/* @codingStandardsIgnoreFile */'],
750+
'old style: start of file, multi-line star comment' => [
751+
'before' => '/*'.PHP_EOL.' @codingStandardsIgnoreFile'.PHP_EOL.' */',
752+
],
753+
'old style: start of file, single-line docblock comment' => ['before' => '/** @codingStandardsIgnoreFile */'],
827754

828-
$warnings = $file->getWarnings();
829-
$numWarnings = $file->getWarningCount();
830-
$this->assertEquals(0, $numWarnings);
831-
$this->assertCount(0, $warnings);
755+
// Deprecated syntax, late comment.
756+
'old style: late comment, slash comment' => [
757+
'before' => '',
758+
'after' => '// @codingStandardsIgnoreFile',
759+
],
760+
];
832761

833-
}//end testSuppressFile()
762+
}//end dataSuppressFile()
834763

835764

836765
/**

0 commit comments

Comments
 (0)