Skip to content

Commit 9165a9a

Browse files
committed
ErrorSuppressionTest::testSuppressScope(): refactor to data provider
* 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.
1 parent 32ecc58 commit 9165a9a

File tree

1 file changed

+77
-73
lines changed

1 file changed

+77
-73
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -573,94 +573,98 @@ public function dataNestedSuppressLine()
573573
/**
574574
* Test suppressing a scope opener.
575575
*
576+
* @param string $before Annotation to place before the scope opener.
577+
* @param string $after Annotation to place after the scope opener.
578+
* @param int $expectedErrors Optional. Number of errors expected.
579+
* Defaults to 0.
580+
*
581+
* @dataProvider dataSuppressScope
582+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
583+
*
576584
* @return void
577585
*/
578-
public function testSuppressScope()
586+
public function testSuppressScope($before, $after, $expectedErrors=0)
579587
{
580-
$config = new Config();
581-
$config->standards = ['PEAR'];
582-
$config->sniffs = ['PEAR.Functions.FunctionDeclaration'];
583-
584-
$ruleset = new Ruleset($config);
585-
586-
// Process without suppression.
587-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'function myFunction() {'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
588-
$file = new DummyFile($content, $ruleset, $config);
589-
$file->process();
590-
591-
$errors = $file->getErrors();
592-
$numErrors = $file->getErrorCount();
593-
$this->assertEquals(1, $numErrors);
594-
$this->assertCount(1, $errors);
595-
596-
// Process with suppression.
597-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'//phpcs:disable'.PHP_EOL.'function myFunction() {'.PHP_EOL.'//phpcs:enable'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
598-
$file = new DummyFile($content, $ruleset, $config);
599-
$file->process();
600-
601-
$errors = $file->getErrors();
602-
$numErrors = $file->getErrorCount();
603-
$this->assertEquals(0, $numErrors);
604-
$this->assertCount(0, $errors);
605-
606-
// Process with suppression (hash comment).
607-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'#phpcs:disable'.PHP_EOL.'function myFunction() {'.PHP_EOL.'#phpcs:enable'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
608-
$file = new DummyFile($content, $ruleset, $config);
609-
$file->process();
610-
611-
$errors = $file->getErrors();
612-
$numErrors = $file->getErrorCount();
613-
$this->assertEquals(0, $numErrors);
614-
$this->assertCount(0, $errors);
588+
static $config, $ruleset;
615589

616-
// Process with suppression.
617-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'//@phpcs:disable'.PHP_EOL.'function myFunction() {'.PHP_EOL.'//@phpcs:enable'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
618-
$file = new DummyFile($content, $ruleset, $config);
619-
$file->process();
590+
if (isset($config, $ruleset) === false) {
591+
$config = new Config();
592+
$config->standards = ['PEAR'];
593+
$config->sniffs = ['PEAR.Functions.FunctionDeclaration'];
620594

621-
$errors = $file->getErrors();
622-
$numErrors = $file->getErrorCount();
623-
$this->assertEquals(0, $numErrors);
624-
$this->assertCount(0, $errors);
595+
$ruleset = new Ruleset($config);
596+
}
625597

626-
// Process with suppression (deprecated syntax).
627-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'//@codingStandardsIgnoreStart'.PHP_EOL.'function myFunction() {'.PHP_EOL.'//@codingStandardsIgnoreEnd'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
598+
$content = '<?php '.PHP_EOL.$before.'$var = FALSE;'.$after.PHP_EOL.'$var = FALSE;';
599+
$content = <<<EOD
600+
<?php
601+
class MyClass() {
602+
$before
603+
function myFunction() {
604+
$after
605+
\$this->foo();
606+
}
607+
}
608+
EOD;
628609
$file = new DummyFile($content, $ruleset, $config);
629610
$file->process();
630611

631-
$errors = $file->getErrors();
632-
$numErrors = $file->getErrorCount();
633-
$this->assertEquals(0, $numErrors);
634-
$this->assertCount(0, $errors);
635-
636-
// Process with a docblock suppression.
637-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'/** phpcs:disable */'.PHP_EOL.'function myFunction() {'.PHP_EOL.'/** phpcs:enable */'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
638-
$file = new DummyFile($content, $ruleset, $config);
612+
$this->assertSame($expectedErrors, $file->getErrorCount());
613+
$this->assertCount($expectedErrors, $file->getErrors());
639614

640-
$errors = $file->getErrors();
641-
$numErrors = $file->getErrorCount();
642-
$this->assertEquals(0, $numErrors);
643-
$this->assertCount(0, $errors);
615+
}//end testSuppressScope()
644616

645-
// Process with a docblock @ suppression.
646-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'/** @phpcs:disable */'.PHP_EOL.'function myFunction() {'.PHP_EOL.'/** @phpcs:enable */'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
647-
$file = new DummyFile($content, $ruleset, $config);
648617

649-
$errors = $file->getErrors();
650-
$numErrors = $file->getErrorCount();
651-
$this->assertEquals(0, $numErrors);
652-
$this->assertCount(0, $errors);
618+
/**
619+
* Data provider.
620+
*
621+
* @see testSuppressScope()
622+
*
623+
* @return array
624+
*/
625+
public function dataSuppressScope()
626+
{
627+
return [
628+
'no suppression' => [
629+
'before' => '',
630+
'after' => '',
631+
'expectedErrors' => 1,
632+
],
653633

654-
// Process with a docblock suppression (deprecated syntax).
655-
$content = '<?php '.PHP_EOL.'class MyClass() {'.PHP_EOL.'/** @codingStandardsIgnoreStart */'.PHP_EOL.'function myFunction() {'.PHP_EOL.'/** @codingStandardsIgnoreEnd */'.PHP_EOL.'$this->foo();'.PHP_EOL.'}'.PHP_EOL.'}';
656-
$file = new DummyFile($content, $ruleset, $config);
634+
// Process with suppression.
635+
'disable/enable: slash comment' => [
636+
'before' => '//phpcs:disable',
637+
'after' => '//phpcs:enable',
638+
],
639+
'disable/enable: slash comment, with @' => [
640+
'before' => '//@phpcs:disable',
641+
'after' => '//@phpcs:enable',
642+
],
643+
'disable/enable: hash comment' => [
644+
'before' => '#phpcs:disable',
645+
'after' => '#phpcs:enable',
646+
],
647+
'disable/enable: single line docblock comment' => [
648+
'before' => '/** phpcs:disable */',
649+
'after' => '/** phpcs:enable */',
650+
],
651+
'disable/enable: single line docblock comment, with @' => [
652+
'before' => '/** @phpcs:disable */',
653+
'after' => '/** @phpcs:enable */',
654+
],
657655

658-
$errors = $file->getErrors();
659-
$numErrors = $file->getErrorCount();
660-
$this->assertEquals(0, $numErrors);
661-
$this->assertCount(0, $errors);
656+
// Deprecated syntax.
657+
'old style: start/end, slash comment' => [
658+
'before' => '//@codingStandardsIgnoreStart',
659+
'after' => '//@codingStandardsIgnoreEnd',
660+
],
661+
'old style: start/end, single line docblock comment' => [
662+
'before' => '/** @codingStandardsIgnoreStart */',
663+
'after' => '/** @codingStandardsIgnoreEnd */',
664+
],
665+
];
662666

663-
}//end testSuppressScope()
667+
}//end dataSuppressScope()
664668

665669

666670
/**

0 commit comments

Comments
 (0)