Skip to content

Commit

Permalink
ErrorSuppressionTest::testSuppressSomeErrors(): refactor to data prov…
Browse files Browse the repository at this point in the history
…ider

* 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 7364463 commit 0d31521
Showing 1 changed file with 73 additions and 76 deletions.
149 changes: 73 additions & 76 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,97 +154,94 @@ public function dataSuppressError()
/**
* Test suppressing 1 out of 2 errors.
*
* @param string $before Annotation to place before the code.
* @param string $between Annotation to place between the code.
* @param int $expectedErrors Optional. Number of errors expected.
* Defaults to 1.
*
* @dataProvider dataSuppressSomeErrors
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
*
* @return void
*/
public function testSuppressSomeErrors()
public function testSuppressSomeErrors($before, $between, $expectedErrors=1)
{
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

$ruleset = new Ruleset($config);

// Process without suppression.
$content = '<?php '.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(2, $numErrors);
$this->assertCount(2, $errors);

// Process with suppression.
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// phpcs:enable'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);

// Process with @ suppression.
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @phpcs:enable'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);
static $config, $ruleset;

// Process with suppression (hash comment).
$content = '<?php '.PHP_EOL.'# phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'# phpcs:enable'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
if (isset($config, $ruleset) === false) {
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);
$ruleset = new Ruleset($config);
}

// Process with @ suppression (hash comment).
$content = '<?php '.PHP_EOL.'# @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'# @phpcs:enable'.PHP_EOL.'$var = TRUE;';
$content = <<<EOD
<?php
$before
\$var = FALSE;
$between
\$var = TRUE;
EOD;
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);

// Process with suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @codingStandardsIgnoreEnd'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
$this->assertSame($expectedErrors, $file->getErrorCount());
$this->assertCount($expectedErrors, $file->getErrors());

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);
}//end testSuppressSomeErrors()

// Process with a PHPDoc block suppression.
$content = '<?php '.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** phpcs:enable */'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);
/**
* Data provider.
*
* @see testSuppressSomeErrors()
*
* @return array
*/
public function dataSuppressSomeErrors()
{
return [
'no suppression' => [
'before' => '',
'between' => '',
'expectedErrors' => 2,
],

// Process with a PHPDoc block suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** @codingStandardsIgnoreEnd */'.PHP_EOL.'$var = TRUE;';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
// With suppression.
'disable/enable: slash comment' => [
'before' => '// phpcs:disable',
'between' => '// phpcs:enable',
],
'disable/enable: slash comment, with @' => [
'before' => '// @phpcs:disable',
'between' => '// @phpcs:enable',
],
'disable/enable: hash comment' => [
'before' => '# phpcs:disable',
'between' => '# phpcs:enable',
],
'disable/enable: hash comment, with @' => [
'before' => '# @phpcs:disable',
'between' => '# @phpcs:enable',
],
'disable/enable: single line docblock comment' => [
'before' => '/** phpcs:disable */',
'between' => '/** phpcs:enable */',
],

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(1, $numErrors);
$this->assertCount(1, $errors);
// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart',
'between' => '// @codingStandardsIgnoreEnd',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */',
'between' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end testSuppressSomeErrors()
}//end dataSuppressSomeErrors()


/**
Expand Down

0 comments on commit 0d31521

Please sign in to comment.