Skip to content

Commit 41c1601

Browse files
committed
ErrorSuppressionTest::testCommenting(): 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 46f0100 commit 41c1601

File tree

1 file changed

+83
-71
lines changed

1 file changed

+83
-71
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,90 +1146,102 @@ public function dataIgnoreSelected()
11461146
/**
11471147
* Test ignoring specific sniffs.
11481148
*
1149+
* @param string $code Code pattern to check.
1150+
* @param int $expectedErrors Number of errors expected.
1151+
* @param int $expectedWarnings Number of warnings expected.
1152+
*
1153+
* @dataProvider dataCommenting
1154+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
1155+
*
11491156
* @return void
11501157
*/
1151-
public function testCommenting()
1158+
public function testCommenting($code, $expectedErrors, $expectedWarnings)
11521159
{
1153-
$config = new Config();
1154-
$config->standards = ['Generic'];
1155-
$config->sniffs = [
1156-
'Generic.PHP.LowerCaseConstant',
1157-
'Generic.Commenting.Todo',
1158-
];
1160+
static $config, $ruleset;
11591161

1160-
$ruleset = new Ruleset($config);
1162+
if (isset($config, $ruleset) === false) {
1163+
$config = new Config();
1164+
$config->standards = ['Generic'];
1165+
$config->sniffs = [
1166+
'Generic.PHP.LowerCaseConstant',
1167+
'Generic.Commenting.Todo',
1168+
];
11611169

1162-
// Suppress a single sniff.
1163-
$content = '<?php '.PHP_EOL.'// phpcs:ignore Generic.Commenting.Todo -- Because reasons'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1164-
$file = new DummyFile($content, $ruleset, $config);
1165-
$file->process();
1170+
$ruleset = new Ruleset($config);
1171+
}
11661172

1167-
$errors = $file->getErrors();
1168-
$numErrors = $file->getErrorCount();
1169-
$warnings = $file->getWarnings();
1170-
$numWarnings = $file->getWarningCount();
1171-
$this->assertEquals(2, $numErrors);
1172-
$this->assertCount(2, $errors);
1173-
$this->assertEquals(1, $numWarnings);
1174-
$this->assertCount(1, $warnings);
1175-
1176-
// Suppress a single sniff and re-enable.
1177-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo --Because reasons'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo -- Because reasons'.PHP_EOL.'//TODO: write some code';
1173+
$content = '<?php '.$code;
11781174
$file = new DummyFile($content, $ruleset, $config);
11791175
$file->process();
11801176

1181-
$errors = $file->getErrors();
1182-
$numErrors = $file->getErrorCount();
1183-
$warnings = $file->getWarnings();
1184-
$numWarnings = $file->getWarningCount();
1185-
$this->assertEquals(1, $numErrors);
1186-
$this->assertCount(1, $errors);
1187-
$this->assertEquals(1, $numWarnings);
1188-
$this->assertCount(1, $warnings);
1189-
1190-
// Suppress a single sniff using block comments.
1191-
$content = '<?php '.PHP_EOL.'/*'.PHP_EOL.' Disable some checks'.PHP_EOL.' phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'*/'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code';
1192-
$file = new DummyFile($content, $ruleset, $config);
1193-
$file->process();
1177+
$this->assertSame($expectedErrors, $file->getErrorCount());
1178+
$this->assertCount($expectedErrors, $file->getErrors());
11941179

1195-
$errors = $file->getErrors();
1196-
$numErrors = $file->getErrorCount();
1197-
$warnings = $file->getWarnings();
1198-
$numWarnings = $file->getWarningCount();
1199-
$this->assertEquals(1, $numErrors);
1200-
$this->assertCount(1, $errors);
1201-
$this->assertEquals(0, $numWarnings);
1202-
$this->assertCount(0, $warnings);
1203-
1204-
// Suppress a single sniff with a multi-line comment.
1205-
$content = '<?php '.PHP_EOL.'// Turn off a check for the next line of code.'.PHP_EOL.'// phpcs:ignore Generic.Commenting.Todo'.PHP_EOL.'$var = FALSE; //TODO: write some code'.PHP_EOL.'$var = FALSE; //TODO: write some code';
1206-
$file = new DummyFile($content, $ruleset, $config);
1207-
$file->process();
1180+
$this->assertSame($expectedWarnings, $file->getWarningCount());
1181+
$this->assertCount($expectedWarnings, $file->getWarnings());
12081182

1209-
$errors = $file->getErrors();
1210-
$numErrors = $file->getErrorCount();
1211-
$warnings = $file->getWarnings();
1212-
$numWarnings = $file->getWarningCount();
1213-
$this->assertEquals(2, $numErrors);
1214-
$this->assertCount(2, $errors);
1215-
$this->assertEquals(1, $numWarnings);
1216-
$this->assertCount(1, $warnings);
1217-
1218-
// Ignore an enable before a disable.
1219-
$content = '<?php '.PHP_EOL.'// phpcs:enable Generic.PHP.NoSilencedErrors -- Because reasons'.PHP_EOL.'$var = @delete( $filename );'.PHP_EOL;
1220-
$file = new DummyFile($content, $ruleset, $config);
1221-
$file->process();
1183+
}//end testCommenting()
12221184

1223-
$errors = $file->getErrors();
1224-
$numErrors = $file->getErrorCount();
1225-
$warnings = $file->getWarnings();
1226-
$numWarnings = $file->getWarningCount();
1227-
$this->assertEquals(0, $numErrors);
1228-
$this->assertCount(0, $errors);
1229-
$this->assertEquals(0, $numWarnings);
1230-
$this->assertCount(0, $warnings);
12311185

1232-
}//end testCommenting()
1186+
/**
1187+
* Data provider.
1188+
*
1189+
* @see testCommenting()
1190+
*
1191+
* @return array
1192+
*/
1193+
public function dataCommenting()
1194+
{
1195+
return [
1196+
'ignore: single sniff' => [
1197+
'code' => '
1198+
// phpcs:ignore Generic.Commenting.Todo -- Because reasons
1199+
$var = FALSE; //TODO: write some code
1200+
$var = FALSE; //TODO: write some code',
1201+
'expectedErrors' => 2,
1202+
'expectedWarnings' => 1,
1203+
],
1204+
'disable: single sniff; enable: same sniff - test whitespace handling around reason delimiter' => [
1205+
'code' => '
1206+
// phpcs:disable Generic.Commenting.Todo --Because reasons
1207+
$var = FALSE;
1208+
//TODO: write some code
1209+
// phpcs:enable Generic.Commenting.Todo -- Because reasons
1210+
//TODO: write some code',
1211+
'expectedErrors' => 1,
1212+
'expectedWarnings' => 1,
1213+
],
1214+
'disable: single sniff, multi-line comment' => [
1215+
'code' => '
1216+
/*
1217+
Disable some checks
1218+
phpcs:disable Generic.Commenting.Todo
1219+
*/
1220+
$var = FALSE;
1221+
//TODO: write some code',
1222+
'expectedErrors' => 1,
1223+
'expectedWarnings' => 0,
1224+
],
1225+
'ignore: single sniff, multi-line slash comment' => [
1226+
'code' => '
1227+
// Turn off a check for the next line of code.
1228+
// phpcs:ignore Generic.Commenting.Todo
1229+
$var = FALSE; //TODO: write some code
1230+
$var = FALSE; //TODO: write some code',
1231+
'expectedErrors' => 2,
1232+
'expectedWarnings' => 1,
1233+
],
1234+
'enable before disable, sniff not in standard' => [
1235+
'code' => '
1236+
// phpcs:enable Generic.PHP.NoSilencedErrors -- Because reasons
1237+
$var = @delete( $filename );
1238+
',
1239+
'expectedErrors' => 0,
1240+
'expectedWarnings' => 0,
1241+
],
1242+
];
1243+
1244+
}//end dataCommenting()
12331245

12341246

12351247
}//end class

0 commit comments

Comments
 (0)