Skip to content

Commit

Permalink
ErrorSuppressionTest::testDisableSelected(): refactor to data provider
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
jrfnl committed May 8, 2021
1 parent 99d83b1 commit b1e74cc
Showing 1 changed file with 82 additions and 139 deletions.
221 changes: 82 additions & 139 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'# phpcs:disable Generic.Commenting.Todo -- for reasons'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:disable Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
$content = <<<EOD
<?php
$before
\$var = FALSE;
//TODO: write some code
EOD;
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'/**
'.PHP_EOL.' * phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ '.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'/**
'.PHP_EOL.' * @phpcs:disable Generic.Commenting.Todo'.PHP_EOL.' */ '.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'/**
'.PHP_EOL.' * phpcs:disable Generic.Files'.PHP_EOL.' */ '.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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 = '<?php '.PHP_EOL.'/**
'.PHP_EOL.' * @phpcs:disable Generic.Files'.PHP_EOL.' */ '.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->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()


/**
Expand Down

0 comments on commit b1e74cc

Please sign in to comment.