@@ -1054,104 +1054,93 @@ public function dataEnableSelected()
1054
1054
/**
1055
1055
* Test ignoring specific sniffs.
1056
1056
*
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
+ *
1057
1064
* @return void
1058
1065
*/
1059
- public function testIgnoreSelected ()
1066
+ public function testIgnoreSelected ($ before , $ expectedErrors , $ expectedWarnings )
1060
1067
{
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 ;
1083
1069
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
+ ];
1088
1077
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
+ }
1097
1080
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 ;
1100
1087
$ file = new DummyFile ($ content , $ ruleset , $ config );
1101
1088
$ file ->process ();
1102
1089
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 ());
1116
1092
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 ());
1125
1095
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()
1130
1097
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 );
1139
1098
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
+ ],
1144
1114
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
+ ];
1153
1142
1154
- }//end testIgnoreSelected ()
1143
+ }//end dataIgnoreSelected ()
1155
1144
1156
1145
1157
1146
/**
0 commit comments