Skip to content

Commit

Permalink
ErrorSuppressionTest::testSuppressError(): 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 fde816c commit 7364463
Showing 1 changed file with 115 additions and 178 deletions.
293 changes: 115 additions & 178 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,197 +21,134 @@ class ErrorSuppressionTest extends TestCase
/**
* Test suppressing a single error.
*
* @param string $before Annotation to place before the code.
* @param string $after Annotation to place after the code.
* @param int $expectedErrors Optional. Number of errors expected.
* Defaults to 0.
*
* @dataProvider dataSuppressError
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
*
* @return void
*/
public function testSuppressError()
public function testSuppressError($before, $after, $expectedErrors=0)
{
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
static $config, $ruleset;

$ruleset = new Ruleset($config);

// Process without suppression.
$content = '<?php '.PHP_EOL.'$var = FALSE;';
$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 inline comment suppression.
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// phpcs:enable';
$content = '<?php '.PHP_EOL.$before.'$var = FALSE;'.PHP_EOL.$after;
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

// Process with multi-line inline comment suppression, tab-indented.
$content = '<?php '.PHP_EOL."\t".'// For reasons'.PHP_EOL."\t".'// phpcs:disable'.PHP_EOL."\t".'$var = FALSE;'.PHP_EOL."\t".'// phpcs:enable';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

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

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

// Process with inline comment suppression mixed case.
$content = '<?php '.PHP_EOL.'// PHPCS:Disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// pHPcs:enabLE';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

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

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

// Process with multi-line inline hash comment suppression, tab-indented.
$content = '<?php '.PHP_EOL."\t".'# For reasons'.PHP_EOL."\t".'# phpcs:disable'.PHP_EOL."\t".'$var = FALSE;'.PHP_EOL."\t".'# phpcs:enable';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

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

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

// Process with inline hash comment suppression mixed case.
$content = '<?php '.PHP_EOL.'# PHPCS:Disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'# pHPcs:enabLE';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

// Process with inline comment suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// @codingStandardsIgnoreEnd';
$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(0, $numErrors);
$this->assertCount(0, $errors);

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

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

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

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

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

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

// Process with multi-line block comment suppression, tab-indented.
$content = '<?php '.PHP_EOL."\t".'/*'.PHP_EOL."\t".' * phpcs:disable'.PHP_EOL."\t".' */'.PHP_EOL."\t".'$var = FALSE;'.PHP_EOL."\t".'/*'.PHP_EOL.' * phpcs:enable'.PHP_EOL.' */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

// Process with block comment suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'/* @codingStandardsIgnoreStart */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/* @codingStandardsIgnoreEnd */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

// Process with multi-line block comment suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreStart'.PHP_EOL.' */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/*'.PHP_EOL.' @codingStandardsIgnoreEnd'.PHP_EOL.' */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

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

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

// Process with a docblock suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'/** @codingStandardsIgnoreEnd */';
$file = new DummyFile($content, $ruleset, $config);
$file->process();

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

// Inline slash comments.
'disable/enable: slash comment' => [
'before' => '// phpcs:disable'.PHP_EOL,
'after' => '// phpcs:enable',
],
'disable/enable: multi-line slash comment, tab indented' => [
'before' => "\t".'// For reasons'.PHP_EOL."\t".'// phpcs:disable'.PHP_EOL."\t",
'after' => "\t".'// phpcs:enable',
],
'disable/enable: slash comment, with @' => [
'before' => '// @phpcs:disable'.PHP_EOL,
'after' => '// @phpcs:enable',
],
'disable/enable: slash comment, mixed case' => [
'before' => '// PHPCS:Disable'.PHP_EOL,
'after' => '// pHPcs:enabLE',
],

// Inline hash comments.
'disable/enable: hash comment' => [
'before' => '# phpcs:disable'.PHP_EOL,
'after' => '# phpcs:enable',
],
'disable/enable: multi-line hash comment, tab indented' => [
'before' => "\t".'# For reasons'.PHP_EOL."\t".'# phpcs:disable'.PHP_EOL."\t",
'after' => "\t".'# phpcs:enable',
],
'disable/enable: hash comment, with @' => [
'before' => '# @phpcs:disable'.PHP_EOL,
'after' => '# @phpcs:enable',
],
'disable/enable: hash comment, mixed case' => [
'before' => '# PHPCS:Disable'.PHP_EOL,
'after' => '# pHPcs:enabLE',
],

// Inline star (block) comments.
'disable/enable: star comment' => [
'before' => '/* phpcs:disable */'.PHP_EOL,
'after' => '/* phpcs:enable */',
],
'disable/enable: multi-line star comment' => [
'before' => '/*'.PHP_EOL.' phpcs:disable'.PHP_EOL.' */'.PHP_EOL,
'after' => '/*'.PHP_EOL.' phpcs:enable'.PHP_EOL.' */',
],
'disable/enable: multi-line star comment, each line starred' => [
'before' => '/*'.PHP_EOL.' * phpcs:disable'.PHP_EOL.' */'.PHP_EOL,
'after' => '/*'.PHP_EOL.' * phpcs:enable'.PHP_EOL.' */',
],
'disable/enable: multi-line star comment, each line starred, tab indented' => [
'before' => "\t".'/*'.PHP_EOL."\t".' * phpcs:disable'.PHP_EOL."\t".' */'.PHP_EOL."\t",
'after' => "\t".'/*'.PHP_EOL.' * phpcs:enable'.PHP_EOL.' */',
],

// Docblock comments.
'disable/enable: single line docblock comment' => [
'before' => '/** phpcs:disable */'.PHP_EOL,
'after' => '/** phpcs:enable */',
],

// Deprecated syntax.
'old style: slash comment' => [
'before' => '// @codingStandardsIgnoreStart'.PHP_EOL,
'after' => '// @codingStandardsIgnoreEnd',
],
'old style: star comment' => [
'before' => '/* @codingStandardsIgnoreStart */'.PHP_EOL,
'after' => '/* @codingStandardsIgnoreEnd */',
],
'old style: multi-line star comment' => [
'before' => '/*'.PHP_EOL.' @codingStandardsIgnoreStart'.PHP_EOL.' */'.PHP_EOL,
'after' => '/*'.PHP_EOL.' @codingStandardsIgnoreEnd'.PHP_EOL.' */',
],
'old style: single line docblock comment' => [
'before' => '/** @codingStandardsIgnoreStart */'.PHP_EOL,
'after' => '/** @codingStandardsIgnoreEnd */',
],
];

}//end testSuppressError()
}//end dataSuppressError()


/**
Expand Down

0 comments on commit 7364463

Please sign in to comment.