Skip to content

Commit

Permalink
ErrorSuppressionTest::testSuppressWarning(): refactor to data provider
Browse files Browse the repository at this point in the history
* 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 0d31521 commit 58e8734
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,77 +247,85 @@ public function dataSuppressSomeErrors()
/**
* Test suppressing a single warning.
*
* @param string $before Annotation to place before the code.
* @param string $after Annotation to place after the code.
* @param int $expectedWarnings Optional. Number of warnings expected.
* Defaults to 0.
*
* @dataProvider dataSuppressWarning
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
*
* @return void
*/
public function testSuppressWarning()
public function testSuppressWarning($before, $after, $expectedWarnings=0)
{
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.Commenting.Todo'];

$ruleset = new Ruleset($config);

// Process without suppression.
$content = '<?php '.PHP_EOL.'//TODO: write some code';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(1, $numWarnings);
$this->assertCount(1, $warnings);
static $config, $ruleset;

// Process with suppression.
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
if (isset($config, $ruleset) === false) {
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.Commenting.Todo'];

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(0, $numWarnings);
$this->assertCount(0, $warnings);
$ruleset = new Ruleset($config);
}

// Process with @ suppression.
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @phpcs:enable';
$content = <<<EOD
<?php
$before
//TODO: write some code.
$after
EOD;
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(0, $numWarnings);
$this->assertCount(0, $warnings);
$this->assertSame($expectedWarnings, $file->getWarningCount());
$this->assertCount($expectedWarnings, $file->getWarnings());

// Process with suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// @codingStandardsIgnoreEnd';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(0, $numWarnings);
$this->assertCount(0, $warnings);
}//end testSuppressWarning()

// Process with a docblock suppression.
$content = '<?php '.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'/** phpcs:enable */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(0, $numWarnings);
$this->assertCount(0, $warnings);
/**
* Data provider.
*
* @see testSuppressWarning()
*
* @return array
*/
public function dataSuppressWarning()
{
return [
'no suppression' => [
'before' => '',
'after' => '',
'expectedWarnings' => 1,
],

// Process with a docblock suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'/** @codingStandardsIgnoreEnd */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
// With suppression.
'disable/enable: slash comment' => [
'before' => '// phpcs:disable',
'after' => '// phpcs:enable',
],
'disable/enable: slash comment, with @' => [
'before' => '// @phpcs:disable',
'after' => '// @phpcs:enable',
],
'disable/enable: single line docblock comment' => [
'before' => '/** phpcs:disable */',
'after' => '/** phpcs:enable */',
],

$warnings = $file->getWarnings();
$numWarnings = $file->getWarningCount();
$this->assertEquals(0, $numWarnings);
$this->assertCount(0, $warnings);
// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart',
'after' => '// @codingStandardsIgnoreEnd',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */',
'after' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end testSuppressWarning()
}//end dataSuppressWarning()


/**
Expand Down

0 comments on commit 58e8734

Please sign in to comment.