@@ -765,162 +765,105 @@ public function dataSuppressFile()
765765 /**
766766 * Test disabling specific sniffs.
767767 *
768+ * @param string $before Annotation to place before the code.
769+ * @param int $expectedErrors Optional. Number of errors expected.
770+ * Defaults to 0.
771+ * @param int $expectedWarnings Optional. Number of warnings expected.
772+ * Defaults to 0.
773+ *
774+ * @dataProvider dataDisableSelected
775+ * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
776+ *
768777 * @return void
769778 */
770- public function testDisableSelected ()
779+ public function testDisableSelected ($ before , $ expectedErrors = 0 , $ expectedWarnings = 0 )
771780 {
772- $ config = new Config ();
773- $ config ->standards = ['Generic ' ];
774- $ config ->sniffs = [
775- 'Generic.PHP.LowerCaseConstant ' ,
776- 'Generic.Commenting.Todo ' ,
777- ];
778-
779- $ ruleset = new Ruleset ($ config );
780-
781- // Suppress a single sniff.
782- $ content = '<?php ' .PHP_EOL .'// phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
783- $ file = new DummyFile ($ content , $ ruleset , $ config );
784- $ file ->process ();
785-
786- $ errors = $ file ->getErrors ();
787- $ numErrors = $ file ->getErrorCount ();
788- $ warnings = $ file ->getWarnings ();
789- $ numWarnings = $ file ->getWarningCount ();
790- $ this ->assertEquals (1 , $ numErrors );
791- $ this ->assertCount (1 , $ errors );
792- $ this ->assertEquals (0 , $ numWarnings );
793- $ this ->assertCount (0 , $ warnings );
794-
795- // Suppress a single sniff with reason (hash comment).
796- $ content = '<?php ' .PHP_EOL .'# phpcs:disable Generic.Commenting.Todo -- for reasons ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
797- $ file = new DummyFile ($ content , $ ruleset , $ config );
798- $ file ->process ();
799-
800- $ errors = $ file ->getErrors ();
801- $ numErrors = $ file ->getErrorCount ();
802- $ warnings = $ file ->getWarnings ();
803- $ numWarnings = $ file ->getWarningCount ();
804- $ this ->assertEquals (1 , $ numErrors );
805- $ this ->assertCount (1 , $ errors );
806- $ this ->assertEquals (0 , $ numWarnings );
807- $ this ->assertCount (0 , $ warnings );
808-
809- // Suppress multiple sniffs.
810- $ content = '<?php ' .PHP_EOL .'// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
811- $ file = new DummyFile ($ content , $ ruleset , $ config );
812- $ file ->process ();
813-
814- $ errors = $ file ->getErrors ();
815- $ numErrors = $ file ->getErrorCount ();
816- $ warnings = $ file ->getWarnings ();
817- $ numWarnings = $ file ->getWarningCount ();
818- $ this ->assertEquals (0 , $ numErrors );
819- $ this ->assertCount (0 , $ errors );
820- $ this ->assertEquals (0 , $ numWarnings );
821- $ this ->assertCount (0 , $ warnings );
822-
823- // Suppress adding sniffs.
824- $ content = '<?php ' .PHP_EOL .'// phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .'// phpcs:disable Generic.PHP.LowerCaseConstant ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
825- $ file = new DummyFile ($ content , $ ruleset , $ config );
826- $ file ->process ();
827-
828- $ errors = $ file ->getErrors ();
829- $ numErrors = $ file ->getErrorCount ();
830- $ warnings = $ file ->getWarnings ();
831- $ numWarnings = $ file ->getWarningCount ();
832- $ this ->assertEquals (0 , $ numErrors );
833- $ this ->assertCount (0 , $ errors );
834- $ this ->assertEquals (0 , $ numWarnings );
835- $ this ->assertCount (0 , $ warnings );
781+ static $ config , $ ruleset ;
836782
837- // Suppress a category of sniffs.
838- $ content = '<?php ' .PHP_EOL .'// phpcs:disable Generic.Commenting ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
839- $ file = new DummyFile ($ content , $ ruleset , $ config );
840- $ file ->process ();
783+ if (isset ($ config , $ ruleset ) === false ) {
784+ $ config = new Config ();
785+ $ config ->standards = ['Generic ' ];
786+ $ config ->sniffs = [
787+ 'Generic.PHP.LowerCaseConstant ' ,
788+ 'Generic.Commenting.Todo ' ,
789+ ];
841790
842- $ errors = $ file ->getErrors ();
843- $ numErrors = $ file ->getErrorCount ();
844- $ warnings = $ file ->getWarnings ();
845- $ numWarnings = $ file ->getWarningCount ();
846- $ this ->assertEquals (1 , $ numErrors );
847- $ this ->assertCount (1 , $ errors );
848- $ this ->assertEquals (0 , $ numWarnings );
849- $ this ->assertCount (0 , $ warnings );
791+ $ ruleset = new Ruleset ($ config );
792+ }
850793
851- // Suppress a whole standard.
852- $ content = '<?php ' .PHP_EOL .'// phpcs:disable Generic ' .PHP_EOL .'$var = FALSE; ' .PHP_EOL .'//TODO: write some code ' ;
794+ $ content = <<<EOD
795+ <?php
796+ $ before
797+ \$var = FALSE;
798+ //TODO: write some code
799+ EOD ;
853800 $ file = new DummyFile ($ content , $ ruleset , $ config );
854801 $ file ->process ();
855802
856- $ errors = $ file ->getErrors ();
857- $ numErrors = $ file ->getErrorCount ();
858- $ warnings = $ file ->getWarnings ();
859- $ numWarnings = $ file ->getWarningCount ();
860- $ this ->assertEquals (0 , $ numErrors );
861- $ this ->assertCount (0 , $ errors );
862- $ this ->assertEquals (0 , $ numWarnings );
863- $ this ->assertCount (0 , $ warnings );
864-
865- // Suppress using docblocks.
866- $ content = '<?php ' .PHP_EOL .'/**
867- ' .PHP_EOL .' * phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .' */ ' .PHP_EOL .'//TODO: write some code ' ;
868- $ file = new DummyFile ($ content , $ ruleset , $ config );
869- $ file ->process ();
803+ $ this ->assertSame ($ expectedErrors , $ file ->getErrorCount ());
804+ $ this ->assertCount ($ expectedErrors , $ file ->getErrors ());
870805
871- $ errors = $ file ->getErrors ();
872- $ numErrors = $ file ->getErrorCount ();
873- $ warnings = $ file ->getWarnings ();
874- $ numWarnings = $ file ->getWarningCount ();
875- $ this ->assertEquals (0 , $ numErrors );
876- $ this ->assertCount (0 , $ errors );
877- $ this ->assertEquals (0 , $ numWarnings );
878- $ this ->assertCount (0 , $ warnings );
806+ $ this ->assertSame ($ expectedWarnings , $ file ->getWarningCount ());
807+ $ this ->assertCount ($ expectedWarnings , $ file ->getWarnings ());
879808
880- $ content = '<?php ' .PHP_EOL .'/**
881- ' .PHP_EOL .' * @phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .' */ ' .PHP_EOL .'//TODO: write some code ' ;
882- $ file = new DummyFile ($ content , $ ruleset , $ config );
883- $ file ->process ();
809+ }//end testDisableSelected()
884810
885- $ errors = $ file ->getErrors ();
886- $ numErrors = $ file ->getErrorCount ();
887- $ warnings = $ file ->getWarnings ();
888- $ numWarnings = $ file ->getWarningCount ();
889- $ this ->assertEquals (0 , $ numErrors );
890- $ this ->assertCount (0 , $ errors );
891- $ this ->assertEquals (0 , $ numWarnings );
892- $ this ->assertCount (0 , $ warnings );
893811
894- // Suppress wrong category using docblocks.
895- $ content = '<?php ' .PHP_EOL .'/**
896- ' .PHP_EOL .' * phpcs:disable Generic.Files ' .PHP_EOL .' */ ' .PHP_EOL .'//TODO: write some code ' ;
897- $ file = new DummyFile ($ content , $ ruleset , $ config );
898- $ file ->process ();
812+ /**
813+ * Data provider.
814+ *
815+ * @see testDisableSelected()
816+ *
817+ * @return array
818+ */
819+ public function dataDisableSelected ()
820+ {
821+ return [
822+ // Single sniff.
823+ 'disable: single sniff ' => [
824+ 'before ' => '// phpcs:disable Generic.Commenting.Todo ' ,
825+ 'expectedErrors ' => 1 ,
826+ ],
827+ 'disable: single sniff with reason ' => [
828+ 'before ' => '# phpcs:disable Generic.Commenting.Todo -- for reasons ' ,
829+ 'expectedErrors ' => 1 ,
830+ ],
831+ 'disable: single sniff, docblock ' => [
832+ 'before ' => '/** ' .PHP_EOL .' * phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .' */ ' ,
833+ 'expectedErrors ' => 1 ,
834+ ],
835+ 'disable: single sniff, docblock, with @ ' => [
836+ 'before ' => '/** ' .PHP_EOL .' * @phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .' */ ' ,
837+ 'expectedErrors ' => 1 ,
838+ ],
899839
900- $ errors = $ file ->getErrors ();
901- $ numErrors = $ file ->getErrorCount ();
902- $ warnings = $ file ->getWarnings ();
903- $ numWarnings = $ file ->getWarningCount ();
904- $ this ->assertEquals (0 , $ numErrors );
905- $ this ->assertCount (0 , $ errors );
906- $ this ->assertEquals (1 , $ numWarnings );
907- $ this ->assertCount (1 , $ warnings );
840+ // Multiple sniffs.
841+ 'disable: multiple sniffs in one comment ' => ['before ' => '// phpcs:disable Generic.Commenting.Todo,Generic.PHP.LowerCaseConstant ' ],
842+ 'disable: multiple sniff in multiple comments ' => [
843+ 'before ' => '// phpcs:disable Generic.Commenting.Todo ' .PHP_EOL .'// phpcs:disable Generic.PHP.LowerCaseConstant ' ,
844+ ],
908845
909- $ content = '<?php ' .PHP_EOL .'/**
910- ' .PHP_EOL .' * @phpcs:disable Generic.Files ' .PHP_EOL .' */ ' .PHP_EOL .'//TODO: write some code ' ;
911- $ file = new DummyFile ($ content , $ ruleset , $ config );
912- $ file ->process ();
846+ // Selectiveness variations.
847+ 'disable: complete category ' => [
848+ 'before ' => '// phpcs:disable Generic.Commenting ' ,
849+ 'expectedErrors ' => 1 ,
850+ ],
851+ 'disable: whole standard ' => ['before ' => '// phpcs:disable Generic ' ],
913852
914- $ errors = $ file ->getErrors ();
915- $ numErrors = $ file ->getErrorCount ();
916- $ warnings = $ file ->getWarnings ();
917- $ numWarnings = $ file ->getWarningCount ();
918- $ this ->assertEquals (0 , $ numErrors );
919- $ this ->assertCount (0 , $ errors );
920- $ this ->assertEquals (1 , $ numWarnings );
921- $ this ->assertCount (1 , $ warnings );
853+ // Wrong category using docblocks.
854+ 'disable: wrong category, docblock ' => [
855+ 'before ' => '/** ' .PHP_EOL .' * phpcs:disable Generic.Files ' .PHP_EOL .' */ ' ,
856+ 'expectedErrors ' => 1 ,
857+ 'expectedWarnings ' => 1 ,
858+ ],
859+ 'disable: wrong category, docblock, with @ ' => [
860+ 'before ' => '/** ' .PHP_EOL .' * @phpcs:disable Generic.Files ' .PHP_EOL .' */ ' ,
861+ 'expectedErrors ' => 1 ,
862+ 'expectedWarnings ' => 1 ,
863+ ],
864+ ];
922865
923- }//end testDisableSelected ()
866+ }//end dataDisableSelected ()
924867
925868
926869 /**
0 commit comments