From 6bba777f09c32e75ef01f0c14c7200c9e29be9a8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 28 Apr 2024 15:43:17 +0100 Subject: [PATCH] Increase test coverage for --sniffs and --exclude --- tests/Core/Config/SniffListTest.php | 140 ++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/Core/Config/SniffListTest.php diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php new file mode 100644 index 0000000000..80027e73eb --- /dev/null +++ b/tests/Core/Config/SniffListTest.php @@ -0,0 +1,140 @@ + + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Config; + +use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\ConfigDouble; + +/** + * Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. + * + * @covers \PHP_CodeSniffer\Config::processLongArgument + */ +final class SniffListTest extends TestCase +{ + + + /** + * Ensure that the expected error message is returned for invalid arguments + * + * @param string $argument sniffs or exclude + * @param string $value list of sniffs to include / exclude + * @param string $message expected error message text + * + * @return void + * @dataProvider dataInvalidSniffs + */ + public function testInvalid($argument, $value, $message) + { + $config = new ConfigDouble(); + $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; + + if (method_exists($this, 'expectException') === true) { + // PHPUnit 5+. + $this->expectException($exception); + $this->expectExceptionMessage($message); + } else { + // PHPUnit 4. + $this->setExpectedException($exception, $message); + } + + $config->processLongArgument($argument.'='.$value, 0); + + }//end testInvalid() + + + /** + * Date provider for testInvalid() + * + * @see self::testInvalid() + * @return array + */ + public static function dataInvalidSniffs() + { + $arguments = [ + 'sniffs', + 'exclude', + ]; + $result = []; + + $sniffs = []; + + $types = [ + 'Standard', + 'Standard.Category', + 'Standard.Category.Sniff.Code', + ]; + foreach ($types as $value) { + $sniffs[$value] = $value; + $sniffs['Standard.Category.Sniff,B'.$value] = 'B'.$value; + foreach ($types as $extra) { + $sniffs['A'.$value.',B'.$extra] = 'A'.$value; + } + } + + $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; + foreach ($arguments as $argument) { + foreach ($sniffs as $input => $output) { + $result[] = [ + 'argument' => $argument, + 'value' => $input, + 'message' => sprintf($messageTemplate, $output), + ]; + } + } + + return $result; + + }//end dataInvalidSniffs() + + + /** + * Ensure that the valid data does not throw an exception + * + * @param string $argument sniffs or exclude + * @param string $value list of sniffs to include or exclude + * + * @return void + * @dataProvider dataValidSniffs + */ + public function testValid($argument, $value) + { + $config = new ConfigDouble(); + $config->processLongArgument($argument.'='.$value, 0); + + }//end testValid() + + + /** + * Data provider for testValid() + * + * @see self::testValid() + * @return array + */ + public static function dataValidSniffs() + { + $arguments = [ + 'sniffs', + 'exclude', + ]; + $result = []; + + foreach ($arguments as $argument) { + $result[] = [ + 'argument' => $argument, + 'value' => 'Standard.Category.Sniff', + ]; + } + + return $result; + + }//end dataValidSniffs() + + +}//end class