From b1e74cc102ed330581efd236d8cb0429853ee96d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 7 May 2021 18:38:25 +0200 Subject: [PATCH] ErrorSuppressionTest::testDisableSelected(): refactor to data provider **Note**: the "docblock" tests have changed to use the same basic code sample as the other tests. In practice this means that instead of having 0 errors and 0/1 warnings, they will now yield 1 error and 0/1 warnings. Functionally these tests still test the same principle. * 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. --- tests/Core/ErrorSuppressionTest.php | 221 +++++++++++----------------- 1 file changed, 82 insertions(+), 139 deletions(-) diff --git a/tests/Core/ErrorSuppressionTest.php b/tests/Core/ErrorSuppressionTest.php index 3ecb4e1021..e4151a5a7a 100644 --- a/tests/Core/ErrorSuppressionTest.php +++ b/tests/Core/ErrorSuppressionTest.php @@ -765,162 +765,105 @@ public function dataSuppressFile() /** * Test disabling specific sniffs. * + * @param string $before Annotation to place before the code. + * @param int $expectedErrors Optional. Number of errors expected. + * Defaults to 0. + * @param int $expectedWarnings Optional. Number of warnings expected. + * Defaults to 0. + * + * @dataProvider dataDisableSelected + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap + * * @return void */ - public function testDisableSelected() + public function testDisableSelected($before, $expectedErrors=0, $expectedWarnings=0) { - $config = new Config(); - $config->standards = ['Generic']; - $config->sniffs = [ - 'Generic.PHP.LowerCaseConstant', - 'Generic.Commenting.Todo', - ]; - - $ruleset = new Ruleset($config); - - // Suppress a single sniff. - $content = 'process(); - - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(1, $numErrors); - $this->assertCount(1, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); - - // Suppress a single sniff with reason (hash comment). - $content = 'process(); - - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(1, $numErrors); - $this->assertCount(1, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); - - // Suppress multiple sniffs. - $content = 'process(); - - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); - - // Suppress adding sniffs. - $content = 'process(); - - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); + static $config, $ruleset; - // Suppress a category of sniffs. - $content = 'process(); + if (isset($config, $ruleset) === false) { + $config = new Config(); + $config->standards = ['Generic']; + $config->sniffs = [ + 'Generic.PHP.LowerCaseConstant', + 'Generic.Commenting.Todo', + ]; - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(1, $numErrors); - $this->assertCount(1, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); + $ruleset = new Ruleset($config); + } - // Suppress a whole standard. - $content = 'process(); - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); - - // Suppress using docblocks. - $content = 'process(); + $this->assertSame($expectedErrors, $file->getErrorCount()); + $this->assertCount($expectedErrors, $file->getErrors()); - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); + $this->assertSame($expectedWarnings, $file->getWarningCount()); + $this->assertCount($expectedWarnings, $file->getWarnings()); - $content = 'process(); + }//end testDisableSelected() - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(0, $numWarnings); - $this->assertCount(0, $warnings); - // Suppress wrong category using docblocks. - $content = 'process(); + /** + * Data provider. + * + * @see testDisableSelected() + * + * @return array + */ + public function dataDisableSelected() + { + return [ + // Single sniff. + 'disable: single sniff' => [ + 'before' => '// phpcs:disable Generic.Commenting.Todo', + 'expectedErrors' => 1, + ], + 'disable: single sniff with reason' => [ + 'before' => '# phpcs:disable Generic.Commenting.Todo -- for reasons', + 'expectedErrors' => 1, + ], + 'disable: single sniff, docblock' => [ + 'before' => '/**'.PHP_EOL.' * phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ ', + 'expectedErrors' => 1, + ], + 'disable: single sniff, docblock, with @' => [ + 'before' => '/**'.PHP_EOL.' * @phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ ', + 'expectedErrors' => 1, + ], - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(1, $numWarnings); - $this->assertCount(1, $warnings); + // Multiple sniffs. + 'disable: multiple sniffs in one comment' => ['before' => '// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'], + 'disable: multiple sniff in multiple comments' => [ + 'before' => '// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:disable Generic.PHP.LowerCaseConstant', + ], - $content = 'process(); + // Selectiveness variations. + 'disable: complete category' => [ + 'before' => '// phpcs:disable Generic.Commenting', + 'expectedErrors' => 1, + ], + 'disable: whole standard' => ['before' => '// phpcs:disable Generic'], - $errors = $file->getErrors(); - $numErrors = $file->getErrorCount(); - $warnings = $file->getWarnings(); - $numWarnings = $file->getWarningCount(); - $this->assertEquals(0, $numErrors); - $this->assertCount(0, $errors); - $this->assertEquals(1, $numWarnings); - $this->assertCount(1, $warnings); + // Wrong category using docblocks. + 'disable: wrong category, docblock' => [ + 'before' => '/**'.PHP_EOL.' * phpcs:disable Generic.Files'.PHP_EOL.' */ ', + 'expectedErrors' => 1, + 'expectedWarnings' => 1, + ], + 'disable: wrong category, docblock, with @' => [ + 'before' => '/**'.PHP_EOL.' * @phpcs:disable Generic.Files'.PHP_EOL.' */ ', + 'expectedErrors' => 1, + 'expectedWarnings' => 1, + ], + ]; - }//end testDisableSelected() + }//end dataDisableSelected() /**