Skip to content

Commit 46f0100

Browse files
committed
ErrorSuppressionTest::testIgnoreSelected(): 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 68c1867 commit 46f0100

File tree

1 file changed

+72
-83
lines changed

1 file changed

+72
-83
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 72 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,104 +1054,93 @@ public function dataEnableSelected()
10541054
/**
10551055
* Test ignoring specific sniffs.
10561056
*
1057+
* @param string $before Annotation to place before the code.
1058+
* @param int $expectedErrors Number of errors expected.
1059+
* @param int $expectedWarnings Number of warnings expected.
1060+
*
1061+
* @dataProvider dataIgnoreSelected
1062+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
1063+
*
10571064
* @return void
10581065
*/
1059-
public function testIgnoreSelected()
1066+
public function testIgnoreSelected($before, $expectedErrors, $expectedWarnings)
10601067
{
1061-
$config = new Config();
1062-
$config->standards = ['Generic'];
1063-
$config->sniffs = [
1064-
'Generic.PHP.LowerCaseConstant',
1065-
'Generic.Commenting.Todo',
1066-
];
1067-
1068-
$ruleset = new Ruleset($config);
1069-
1070-
// No suppression.
1071-
$content = '<?php '.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1072-
$file = new DummyFile($content, $ruleset, $config);
1073-
$file->process();
1074-
1075-
$errors = $file->getErrors();
1076-
$numErrors = $file->getErrorCount();
1077-
$warnings = $file->getWarnings();
1078-
$numWarnings = $file->getWarningCount();
1079-
$this->assertEquals(2, $numErrors);
1080-
$this->assertCount(2, $errors);
1081-
$this->assertEquals(2, $numWarnings);
1082-
$this->assertCount(2, $warnings);
1068+
static $config, $ruleset;
10831069

1084-
// Suppress a single sniff.
1085-
$content = '<?php '.PHP_EOL.'// phpcs:ignore Generic.Commenting.Todo'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1086-
$file = new DummyFile($content, $ruleset, $config);
1087-
$file->process();
1070+
if (isset($config, $ruleset) === false) {
1071+
$config = new Config();
1072+
$config->standards = ['Generic'];
1073+
$config->sniffs = [
1074+
'Generic.PHP.LowerCaseConstant',
1075+
'Generic.Commenting.Todo',
1076+
];
10881077

1089-
$errors = $file->getErrors();
1090-
$numErrors = $file->getErrorCount();
1091-
$warnings = $file->getWarnings();
1092-
$numWarnings = $file->getWarningCount();
1093-
$this->assertEquals(2, $numErrors);
1094-
$this->assertCount(2, $errors);
1095-
$this->assertEquals(1, $numWarnings);
1096-
$this->assertCount(1, $warnings);
1078+
$ruleset = new Ruleset($config);
1079+
}
10971080

1098-
// Suppress multiple sniffs.
1099-
$content = '<?php '.PHP_EOL.'// phpcs:ignore Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1081+
$content = <<<EOD
1082+
<?php
1083+
$before
1084+
\$var = FALSE; //TODO: write some code
1085+
\$var = FALSE; //TODO: write some code
1086+
EOD;
11001087
$file = new DummyFile($content, $ruleset, $config);
11011088
$file->process();
11021089

1103-
$errors = $file->getErrors();
1104-
$numErrors = $file->getErrorCount();
1105-
$warnings = $file->getWarnings();
1106-
$numWarnings = $file->getWarningCount();
1107-
$this->assertEquals(1, $numErrors);
1108-
$this->assertCount(1, $errors);
1109-
$this->assertEquals(1, $numWarnings);
1110-
$this->assertCount(1, $warnings);
1111-
1112-
// Add to suppression.
1113-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:ignore Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1114-
$file = new DummyFile($content, $ruleset, $config);
1115-
$file->process();
1090+
$this->assertSame($expectedErrors, $file->getErrorCount());
1091+
$this->assertCount($expectedErrors, $file->getErrors());
11161092

1117-
$errors = $file->getErrors();
1118-
$numErrors = $file->getErrorCount();
1119-
$warnings = $file->getWarnings();
1120-
$numWarnings = $file->getWarningCount();
1121-
$this->assertEquals(1, $numErrors);
1122-
$this->assertCount(1, $errors);
1123-
$this->assertEquals(0, $numWarnings);
1124-
$this->assertCount(0, $warnings);
1093+
$this->assertSame($expectedWarnings, $file->getWarningCount());
1094+
$this->assertCount($expectedWarnings, $file->getWarnings());
11251095

1126-
// Suppress a category of sniffs.
1127-
$content = '<?php '.PHP_EOL.'# phpcs:ignore Generic.Commenting'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1128-
$file = new DummyFile($content, $ruleset, $config);
1129-
$file->process();
1096+
}//end testIgnoreSelected()
11301097

1131-
$errors = $file->getErrors();
1132-
$numErrors = $file->getErrorCount();
1133-
$warnings = $file->getWarnings();
1134-
$numWarnings = $file->getWarningCount();
1135-
$this->assertEquals(2, $numErrors);
1136-
$this->assertCount(2, $errors);
1137-
$this->assertEquals(1, $numWarnings);
1138-
$this->assertCount(1, $warnings);
11391098

1140-
// Suppress a whole standard.
1141-
$content = '<?php '.PHP_EOL.'// phpcs:ignore Generic'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1142-
$file = new DummyFile($content, $ruleset, $config);
1143-
$file->process();
1099+
/**
1100+
* Data provider.
1101+
*
1102+
* @see testIgnoreSelected()
1103+
*
1104+
* @return array
1105+
*/
1106+
public function dataIgnoreSelected()
1107+
{
1108+
return [
1109+
'no suppression' => [
1110+
'before' => '',
1111+
'expectedErrors' => 2,
1112+
'expectedWarnings' => 2,
1113+
],
11441114

1145-
$errors = $file->getErrors();
1146-
$numErrors = $file->getErrorCount();
1147-
$warnings = $file->getWarnings();
1148-
$numWarnings = $file->getWarningCount();
1149-
$this->assertEquals(1, $numErrors);
1150-
$this->assertCount(1, $errors);
1151-
$this->assertEquals(1, $numWarnings);
1152-
$this->assertCount(1, $warnings);
1115+
// With suppression.
1116+
'ignore: single sniff' => [
1117+
'before' => '// phpcs:ignore Generic.Commenting.Todo',
1118+
'expectedErrors' => 2,
1119+
'expectedWarnings' => 1,
1120+
],
1121+
'ignore: multiple sniffs' => [
1122+
'before' => '// phpcs:ignore Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant',
1123+
'expectedErrors' => 1,
1124+
'expectedWarnings' => 1,
1125+
],
1126+
'disable: single sniff; ignore: single sniff' => [
1127+
'before' => '// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'// phpcs:ignore Generic.PHP.LowerCaseConstant',
1128+
'expectedErrors' => 1,
1129+
'expectedWarnings' => 0,
1130+
],
1131+
'ignore: category of sniffs' => [
1132+
'before' => '# phpcs:ignore Generic.Commenting',
1133+
'expectedErrors' => 2,
1134+
'expectedWarnings' => 1,
1135+
],
1136+
'ignore: whole standard' => [
1137+
'before' => '// phpcs:ignore Generic',
1138+
'expectedErrors' => 1,
1139+
'expectedWarnings' => 1,
1140+
],
1141+
];
11531142

1154-
}//end testIgnoreSelected()
1143+
}//end dataIgnoreSelected()
11551144

11561145

11571146
/**

0 commit comments

Comments
 (0)