Skip to content

Commit

Permalink
ErrorSuppressionTest::testNestedSuppressLine(): 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 c14b49d commit f9dfb9b
Showing 1 changed file with 70 additions and 77 deletions.
147 changes: 70 additions & 77 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,97 +484,90 @@ public function testSuppressLineWithinDocblock()
/**
* Test that using a single line ignore does not interfere with other suppressions.
*
* @param string $before Annotation to place before the code.
* @param string $after Annotation to place after the code.
*
* @dataProvider dataNestedSuppressLine
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
*
* @return void
*/
public function testNestedSuppressLine()
public function testNestedSuppressLine($before, $after)
{
$config = new Config();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

$ruleset = new Ruleset($config);

// Process with disable/enable suppression and no single line suppression.
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.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 disable/enable @ suppression and no single line suppression.
$content = '<?php '.PHP_EOL.'// @phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.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 disable/enable suppression and no single line suppression (hash comment).
$content = '<?php '.PHP_EOL.'# phpcs:disable'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.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 disable/enable suppression and no single line suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.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);
static $config, $ruleset;

// Process with line suppression nested within disable/enable suppression.
$content = '<?php '.PHP_EOL.'// phpcs:disable'.PHP_EOL.'// phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.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.PHP.LowerCaseConstant'];

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

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

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

// Process with line @ suppression nested within disable/enable @ suppression (hash comment).
$content = '<?php '.PHP_EOL.'# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'# @phpcs:enable';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
}//end testNestedSuppressLine()

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

// Process with line suppression nested within disable/enable suppression (deprecated syntax).
$content = '<?php '.PHP_EOL.'// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'$var = TRUE;'.PHP_EOL.'// @codingStandardsIgnoreEnd';
$file = new DummyFile($content, $ruleset, $config);
$file->process();
/**
* Data provider.
*
* @see testNestedSuppressLine()
*
* @return array
*/
public function dataNestedSuppressLine()
{
return [
// Process with disable/enable suppression and no single line suppression.
'disable/enable: slash comment, no single line suppression' => [
'before' => '// phpcs:disable',
'after' => '// phpcs:enable',
],
'disable/enable: slash comment, with @, no single line suppression' => [
'before' => '// @phpcs:disable',
'after' => '// @phpcs:enable',
],
'disable/enable: hash comment, no single line suppression' => [
'before' => '# phpcs:disable',
'after' => '# phpcs:enable',
],
'old style: slash comment, no single line suppression' => [
'before' => '// @codingStandardsIgnoreStart',
'after' => '// @codingStandardsIgnoreEnd',
],

$errors = $file->getErrors();
$numErrors = $file->getErrorCount();
$this->assertEquals(0, $numErrors);
$this->assertCount(0, $errors);
// Process with line suppression nested within disable/enable suppression.
'disable/enable: slash comment, next line nested single line suppression' => [
'before' => '// phpcs:disable'.PHP_EOL.'// phpcs:ignore',
'after' => '// phpcs:enable',
],
'disable/enable: slash comment, with @, next line nested single line suppression' => [
'before' => '// @phpcs:disable'.PHP_EOL.'// @phpcs:ignore',
'after' => '// @phpcs:enable',
],
'disable/enable: hash comment, next line nested single line suppression' => [
'before' => '# @phpcs:disable'.PHP_EOL.'# @phpcs:ignore',
'after' => '# @phpcs:enable',
],
'old style: slash comment, next line nested single line suppression' => [
'before' => '// @codingStandardsIgnoreStart'.PHP_EOL.'// @codingStandardsIgnoreLine',
'after' => '// @codingStandardsIgnoreEnd',
],
];

}//end testNestedSuppressLine()
}//end dataNestedSuppressLine()


/**
Expand Down

0 comments on commit f9dfb9b

Please sign in to comment.