Skip to content

Commit 68c1867

Browse files
committed
ErrorSuppressionTest::testEnableSelected(): 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 888479a commit 68c1867

File tree

1 file changed

+156
-154
lines changed

1 file changed

+156
-154
lines changed

tests/Core/ErrorSuppressionTest.php

Lines changed: 156 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -879,174 +879,176 @@ public function dataDisableSelected()
879879
/**
880880
* Test re-enabling specific sniffs that have been disabled.
881881
*
882+
* @param string $code Code pattern to check.
883+
* @param int $expectedErrors Number of errors expected.
884+
* @param int $expectedWarnings Number of warnings expected.
885+
*
886+
* @dataProvider dataEnableSelected
887+
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
888+
*
882889
* @return void
883890
*/
884-
public function testEnableSelected()
891+
public function testEnableSelected($code, $expectedErrors, $expectedWarnings)
885892
{
886-
$config = new Config();
887-
$config->standards = ['Generic'];
888-
$config->sniffs = [
889-
'Generic.PHP.LowerCaseConstant',
890-
'Generic.Commenting.Todo',
891-
];
892-
893-
$ruleset = new Ruleset($config);
894-
895-
// Suppress a single sniff and re-enable.
896-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code';
897-
$file = new DummyFile($content, $ruleset, $config);
898-
$file->process();
899-
900-
$errors = $file->getErrors();
901-
$numErrors = $file->getErrorCount();
902-
$warnings = $file->getWarnings();
903-
$numWarnings = $file->getWarningCount();
904-
$this->assertEquals(1, $numErrors);
905-
$this->assertCount(1, $errors);
906-
$this->assertEquals(1, $numWarnings);
907-
$this->assertCount(1, $warnings);
908-
909-
// Suppress multiple sniffs and re-enable.
910-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'$var = FALSE;';
911-
$file = new DummyFile($content, $ruleset, $config);
912-
$file->process();
913-
914-
$errors = $file->getErrors();
915-
$numErrors = $file->getErrorCount();
916-
$warnings = $file->getWarnings();
917-
$numWarnings = $file->getWarningCount();
918-
$this->assertEquals(1, $numErrors);
919-
$this->assertCount(1, $errors);
920-
$this->assertEquals(1, $numWarnings);
921-
$this->assertCount(1, $warnings);
922-
923-
// Suppress multiple sniffs and re-enable one.
924-
$content = '<?php '.PHP_EOL.'# phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'# phpcs:enable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'$var = FALSE;';
925-
$file = new DummyFile($content, $ruleset, $config);
926-
$file->process();
927-
928-
$errors = $file->getErrors();
929-
$numErrors = $file->getErrorCount();
930-
$warnings = $file->getWarnings();
931-
$numWarnings = $file->getWarningCount();
932-
$this->assertEquals(0, $numErrors);
933-
$this->assertCount(0, $errors);
934-
$this->assertEquals(1, $numWarnings);
935-
$this->assertCount(1, $warnings);
936-
937-
// Suppress a category of sniffs and re-enable.
938-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting'.PHP_EOL.'//TODO: write some code';
939-
$file = new DummyFile($content, $ruleset, $config);
940-
$file->process();
941-
942-
$errors = $file->getErrors();
943-
$numErrors = $file->getErrorCount();
944-
$warnings = $file->getWarnings();
945-
$numWarnings = $file->getWarningCount();
946-
$this->assertEquals(1, $numErrors);
947-
$this->assertCount(1, $errors);
948-
$this->assertEquals(1, $numWarnings);
949-
$this->assertCount(1, $warnings);
950-
951-
// Suppress a whole standard and re-enable.
952-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic'.PHP_EOL.'//TODO: write some code';
953-
$file = new DummyFile($content, $ruleset, $config);
954-
$file->process();
955-
956-
$errors = $file->getErrors();
957-
$numErrors = $file->getErrorCount();
958-
$warnings = $file->getWarnings();
959-
$numWarnings = $file->getWarningCount();
960-
$this->assertEquals(0, $numErrors);
961-
$this->assertCount(0, $errors);
962-
$this->assertEquals(1, $numWarnings);
963-
$this->assertCount(1, $warnings);
964-
965-
// Suppress a whole standard and re-enable a category.
966-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting'.PHP_EOL.'//TODO: write some code';
967-
$file = new DummyFile($content, $ruleset, $config);
968-
$file->process();
969-
970-
$errors = $file->getErrors();
971-
$numErrors = $file->getErrorCount();
972-
$warnings = $file->getWarnings();
973-
$numWarnings = $file->getWarningCount();
974-
$this->assertEquals(0, $numErrors);
975-
$this->assertCount(0, $errors);
976-
$this->assertEquals(1, $numWarnings);
977-
$this->assertCount(1, $warnings);
978-
979-
// Suppress a category and re-enable a whole standard.
980-
$content = '<?php '.PHP_EOL.'# phpcs:disable Generic.Commenting'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'# phpcs:enable Generic'.PHP_EOL.'//TODO: write some code';
981-
$file = new DummyFile($content, $ruleset, $config);
982-
$file->process();
983-
984-
$errors = $file->getErrors();
985-
$numErrors = $file->getErrorCount();
986-
$warnings = $file->getWarnings();
987-
$numWarnings = $file->getWarningCount();
988-
$this->assertEquals(1, $numErrors);
989-
$this->assertCount(1, $errors);
990-
$this->assertEquals(1, $numWarnings);
991-
$this->assertCount(1, $warnings);
893+
static $config, $ruleset;
992894

993-
// Suppress a sniff and re-enable a category.
994-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting'.PHP_EOL.'//TODO: write some code';
995-
$file = new DummyFile($content, $ruleset, $config);
996-
$file->process();
895+
if (isset($config, $ruleset) === false) {
896+
$config = new Config();
897+
$config->standards = ['Generic'];
898+
$config->sniffs = [
899+
'Generic.PHP.LowerCaseConstant',
900+
'Generic.Commenting.Todo',
901+
];
997902

998-
$errors = $file->getErrors();
999-
$numErrors = $file->getErrorCount();
1000-
$warnings = $file->getWarnings();
1001-
$numWarnings = $file->getWarningCount();
1002-
$this->assertEquals(1, $numErrors);
1003-
$this->assertCount(1, $errors);
1004-
$this->assertEquals(1, $numWarnings);
1005-
$this->assertCount(1, $warnings);
903+
$ruleset = new Ruleset($config);
904+
}
1006905

1007-
// Suppress a whole standard and re-enable a sniff.
1008-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code';
906+
$content = '<?php '.$code;
1009907
$file = new DummyFile($content, $ruleset, $config);
1010908
$file->process();
1011909

1012-
$errors = $file->getErrors();
1013-
$numErrors = $file->getErrorCount();
1014-
$warnings = $file->getWarnings();
1015-
$numWarnings = $file->getWarningCount();
1016-
$this->assertEquals(0, $numErrors);
1017-
$this->assertCount(0, $errors);
1018-
$this->assertEquals(1, $numWarnings);
1019-
$this->assertCount(1, $warnings);
910+
$this->assertSame($expectedErrors, $file->getErrorCount());
911+
$this->assertCount($expectedErrors, $file->getErrors());
1020912

1021-
// Suppress a whole standard and re-enable and re-disable a sniff.
1022-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:disable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable'.PHP_EOL.'//TODO: write some code';
1023-
$file = new DummyFile($content, $ruleset, $config);
1024-
$file->process();
913+
$this->assertSame($expectedWarnings, $file->getWarningCount());
914+
$this->assertCount($expectedWarnings, $file->getWarnings());
1025915

1026-
$errors = $file->getErrors();
1027-
$numErrors = $file->getErrorCount();
1028-
$warnings = $file->getWarnings();
1029-
$numWarnings = $file->getWarningCount();
1030-
$this->assertEquals(0, $numErrors);
1031-
$this->assertCount(0, $errors);
1032-
$this->assertEquals(2, $numWarnings);
1033-
$this->assertCount(2, $warnings);
916+
}//end testEnableSelected()
1034917

1035-
// Suppress a whole standard and re-enable 2 specific sniffs independently.
1036-
$content = '<?php '.PHP_EOL.'// phpcs:disable Generic'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'// phpcs:enable Generic.Commenting.Todo'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'$var = FALSE;'.PHP_EOL.'// phpcs:enable Generic.PHP.LowerCaseConstant'.PHP_EOL.'//TODO: write some code'.PHP_EOL.'$var = FALSE;'.PHP_EOL;
1037-
$file = new DummyFile($content, $ruleset, $config);
1038-
$file->process();
1039918

1040-
$errors = $file->getErrors();
1041-
$numErrors = $file->getErrorCount();
1042-
$warnings = $file->getWarnings();
1043-
$numWarnings = $file->getWarningCount();
1044-
$this->assertEquals(1, $numErrors);
1045-
$this->assertCount(1, $errors);
1046-
$this->assertEquals(2, $numWarnings);
1047-
$this->assertCount(2, $warnings);
919+
/**
920+
* Data provider.
921+
*
922+
* @see testEnableSelected()
923+
*
924+
* @return array
925+
*/
926+
public function dataEnableSelected()
927+
{
928+
return [
929+
'disable/enable: a single sniff' => [
930+
'code' => '
931+
// phpcs:disable Generic.Commenting.Todo
932+
$var = FALSE;
933+
//TODO: write some code
934+
// phpcs:enable Generic.Commenting.Todo
935+
//TODO: write some code',
936+
'expectedErrors' => 1,
937+
'expectedWarnings' => 1,
938+
],
939+
'disable/enable: multiple sniffs' => [
940+
'code' => '
941+
// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant
942+
$var = FALSE;
943+
//TODO: write some code
944+
// phpcs:enable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant
945+
//TODO: write some code
946+
$var = FALSE;',
947+
'expectedErrors' => 1,
948+
'expectedWarnings' => 1,
949+
],
950+
'disable: multiple sniffs; enable: one' => [
951+
'code' => '
952+
# phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant
953+
$var = FALSE;
954+
//TODO: write some code
955+
# phpcs:enable Generic.Commenting.Todo
956+
//TODO: write some code
957+
$var = FALSE;',
958+
'expectedErrors' => 0,
959+
'expectedWarnings' => 1,
960+
],
961+
'disable/enable: complete category' => [
962+
'code' => '
963+
// phpcs:disable Generic.Commenting
964+
$var = FALSE;
965+
//TODO: write some code
966+
// phpcs:enable Generic.Commenting
967+
//TODO: write some code',
968+
'expectedErrors' => 1,
969+
'expectedWarnings' => 1,
970+
],
971+
'disable/enable: whole standard' => [
972+
'code' => '
973+
// phpcs:disable Generic
974+
$var = FALSE;
975+
//TODO: write some code
976+
// phpcs:enable Generic
977+
//TODO: write some code',
978+
'expectedErrors' => 0,
979+
'expectedWarnings' => 1,
980+
],
981+
'disable: whole standard; enable: category from the standard' => [
982+
'code' => '
983+
// phpcs:disable Generic
984+
$var = FALSE;
985+
//TODO: write some code
986+
// phpcs:enable Generic.Commenting
987+
//TODO: write some code',
988+
'expectedErrors' => 0,
989+
'expectedWarnings' => 1,
990+
],
991+
'disable: a category; enable: the whole standard containing the category' => [
992+
'code' => '
993+
# phpcs:disable Generic.Commenting
994+
$var = FALSE;
995+
//TODO: write some code
996+
# phpcs:enable Generic
997+
//TODO: write some code',
998+
'expectedErrors' => 1,
999+
'expectedWarnings' => 1,
1000+
],
1001+
'disable: single sniff; enable: the category containing the sniff' => [
1002+
'code' => '
1003+
// phpcs:disable Generic.Commenting.Todo
1004+
$var = FALSE;
1005+
//TODO: write some code
1006+
// phpcs:enable Generic.Commenting
1007+
//TODO: write some code',
1008+
'expectedErrors' => 1,
1009+
'expectedWarnings' => 1,
1010+
],
1011+
'disable: whole standard; enable: single sniff from the standard' => [
1012+
'code' => '
1013+
// phpcs:disable Generic
1014+
$var = FALSE;
1015+
//TODO: write some code
1016+
// phpcs:enable Generic.Commenting.Todo
1017+
//TODO: write some code',
1018+
'expectedErrors' => 0,
1019+
'expectedWarnings' => 1,
1020+
],
1021+
'disable: whole standard; enable: single sniff from the standard; disable: that same sniff; enable: everything' => [
1022+
'code' => '
1023+
// phpcs:disable Generic
1024+
$var = FALSE;
1025+
//TODO: write some code
1026+
// phpcs:enable Generic.Commenting.Todo
1027+
//TODO: write some code
1028+
// phpcs:disable Generic.Commenting.Todo
1029+
//TODO: write some code
1030+
// phpcs:enable
1031+
//TODO: write some code',
1032+
'expectedErrors' => 0,
1033+
'expectedWarnings' => 2,
1034+
],
1035+
'disable: whole standard; enable: single sniff from the standard; enable: other sniff from the standard' => [
1036+
'code' => '
1037+
// phpcs:disable Generic
1038+
$var = FALSE;
1039+
//TODO: write some code
1040+
// phpcs:enable Generic.Commenting.Todo
1041+
//TODO: write some code
1042+
$var = FALSE;
1043+
// phpcs:enable Generic.PHP.LowerCaseConstant
1044+
//TODO: write some code
1045+
$var = FALSE;',
1046+
'expectedErrors' => 1,
1047+
'expectedWarnings' => 2,
1048+
],
1049+
];
10481050

1049-
}//end testEnableSelected()
1051+
}//end dataEnableSelected()
10501052

10511053

10521054
/**

0 commit comments

Comments
 (0)