From 6bba777f09c32e75ef01f0c14c7200c9e29be9a8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 28 Apr 2024 15:43:17 +0100 Subject: [PATCH 01/26] 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 From 2829da79b9b988b0782e16ec962469799b700273 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 10:51:35 +0100 Subject: [PATCH 02/26] Fix tyop --- tests/Core/Config/SniffListTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index 80027e73eb..df0c054667 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -50,7 +50,7 @@ public function testInvalid($argument, $value, $message) /** - * Date provider for testInvalid() + * Data provider for testInvalid() * * @see self::testInvalid() * @return array From 61c7bf975c0f738ccc724bc24f73ac6ed3462e3b Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 10:53:13 +0100 Subject: [PATCH 03/26] Use capital letters in comments --- tests/Core/Config/SniffListTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index df0c054667..bf4e7c5f2f 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -24,8 +24,8 @@ 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 + * @param string $value List of sniffs to include / exclude + * @param string $message Expected error message text * * @return void * @dataProvider dataInvalidSniffs @@ -98,7 +98,7 @@ public static function 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 + * @param string $value List of sniffs to include or exclude * * @return void * @dataProvider dataValidSniffs From c73d0b3ddcd42c9eaf33b1b8a9df28ce9d75c90f Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 10:53:38 +0100 Subject: [PATCH 04/26] Quote fixed strings to avoid confustion with words --- tests/Core/Config/SniffListTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index bf4e7c5f2f..3da3a931c2 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -23,7 +23,7 @@ final class SniffListTest extends TestCase /** * Ensure that the expected error message is returned for invalid arguments * - * @param string $argument sniffs or exclude + * @param string $argument 'sniffs' or 'exclude' * @param string $value List of sniffs to include / exclude * @param string $message Expected error message text * @@ -97,7 +97,7 @@ public static function dataInvalidSniffs() /** * Ensure that the valid data does not throw an exception * - * @param string $argument sniffs or exclude + * @param string $argument 'sniffs' or 'exclude' * @param string $value List of sniffs to include or exclude * * @return void From 8e2603f36ccaa5fba6e7b716f0fde813d2431386 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 10:54:41 +0100 Subject: [PATCH 05/26] Add full-stops --- tests/Core/Config/SniffListTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index 3da3a931c2..989dcad61d 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -21,11 +21,11 @@ final class SniffListTest extends TestCase /** - * Ensure that the expected error message is returned for invalid arguments + * 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 + * @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 @@ -50,7 +50,7 @@ public function testInvalid($argument, $value, $message) /** - * Data provider for testInvalid() + * Data provider for testInvalid(). * * @see self::testInvalid() * @return array @@ -95,10 +95,10 @@ public static function dataInvalidSniffs() /** - * Ensure that the valid data does not throw an exception + * 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 + * @param string $argument 'sniffs' or 'exclude'. + * @param string $value List of sniffs to include or exclude. * * @return void * @dataProvider dataValidSniffs @@ -112,7 +112,7 @@ public function testValid($argument, $value) /** - * Data provider for testValid() + * Data provider for testValid(). * * @see self::testValid() * @return array From 3391c094ea6ac3a0d4d4a93d55f7b04097ad21b2 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 11:01:46 +0100 Subject: [PATCH 06/26] Rename variable --- tests/Core/Config/SniffListTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index 989dcad61d..983e1d9872 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -61,7 +61,7 @@ public static function dataInvalidSniffs() 'sniffs', 'exclude', ]; - $result = []; + $data = []; $sniffs = []; @@ -81,7 +81,7 @@ public static function dataInvalidSniffs() $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; foreach ($arguments as $argument) { foreach ($sniffs as $input => $output) { - $result[] = [ + $data[] = [ 'argument' => $argument, 'value' => $input, 'message' => sprintf($messageTemplate, $output), @@ -89,7 +89,7 @@ public static function dataInvalidSniffs() } } - return $result; + return $data; }//end dataInvalidSniffs() @@ -123,16 +123,16 @@ public static function dataValidSniffs() 'sniffs', 'exclude', ]; - $result = []; + $data = []; foreach ($arguments as $argument) { - $result[] = [ + $data[] = [ 'argument' => $argument, 'value' => 'Standard.Category.Sniff', ]; } - return $result; + return $data; }//end dataValidSniffs() From f14581fd8c4eef621ec9759ef9b01e484bcffc1a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 11:01:56 +0100 Subject: [PATCH 07/26] Sort 'use' statements --- tests/Core/Config/SniffListTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index 983e1d9872..e78c6a527b 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -8,8 +8,8 @@ namespace PHP_CodeSniffer\Tests\Core\Config; -use PHPUnit\Framework\TestCase; use PHP_CodeSniffer\Tests\ConfigDouble; +use PHPUnit\Framework\TestCase; /** * Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. From 7ee42d1f1baf89dd736967714f5aa37484f64970 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 11:52:48 +0100 Subject: [PATCH 08/26] Unwrap foreach; reduce test cases --- tests/Core/Config/SniffListTest.php | 57 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index e78c6a527b..bea488ed33 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -63,31 +63,42 @@ public static function dataInvalidSniffs() ]; $data = []; - $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) { - $data[] = [ - 'argument' => $argument, - 'value' => $input, - 'message' => sprintf($messageTemplate, $output), - ]; - } - } + // A standard is not a valid sniff. + $data[] = [ + 'argument' => $argument, + 'value' => 'Standard', + 'message' => sprintf($messageTemplate, 'Standard'), + ]; + + // A category is not a valid sniff. + $data[] = [ + 'argument' => $argument, + 'value' => 'Standard.Category', + 'message' => sprintf($messageTemplate, 'Standard.Category'), + ]; + + // An error-code is not a valid sniff. + $data[] = [ + 'argument' => $argument, + 'value' => 'Standard.Category', + 'message' => sprintf($messageTemplate, 'Standard.Category'), + ]; + + // Only the first error is reported. + $data[] = [ + 'argument' => $argument, + 'value' => 'StandardOne,StandardTwo', + 'message' => sprintf($messageTemplate, 'StandardOne'), + ]; + $data[] = [ + 'argument' => $argument, + 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category', + 'message' => sprintf($messageTemplate, 'StandardTwo.Category'), + ]; + }//end foreach return $data; From 13edab31f3ad4b7cec993a4e3b9c3dc06028cdfb Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 11:55:13 +0100 Subject: [PATCH 09/26] Name tests --- tests/Core/Config/SniffListTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index bea488ed33..e44b542384 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -67,33 +67,33 @@ public static function dataInvalidSniffs() foreach ($arguments as $argument) { // A standard is not a valid sniff. - $data[] = [ + $data[$argument.'; standard'] = [ 'argument' => $argument, 'value' => 'Standard', 'message' => sprintf($messageTemplate, 'Standard'), ]; // A category is not a valid sniff. - $data[] = [ + $data[$argument.'; category'] = [ 'argument' => $argument, 'value' => 'Standard.Category', 'message' => sprintf($messageTemplate, 'Standard.Category'), ]; // An error-code is not a valid sniff. - $data[] = [ + $data[$argument.'; error-code'] = [ 'argument' => $argument, 'value' => 'Standard.Category', 'message' => sprintf($messageTemplate, 'Standard.Category'), ]; // Only the first error is reported. - $data[] = [ + $data[$argument.'; two errors'] = [ 'argument' => $argument, 'value' => 'StandardOne,StandardTwo', 'message' => sprintf($messageTemplate, 'StandardOne'), ]; - $data[] = [ + $data[$argument.'; valid followed by invalid'] = [ 'argument' => $argument, 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category', 'message' => sprintf($messageTemplate, 'StandardTwo.Category'), @@ -137,7 +137,7 @@ public static function dataValidSniffs() $data = []; foreach ($arguments as $argument) { - $data[] = [ + $data[$argument.'; one valid sniff'] = [ 'argument' => $argument, 'value' => 'Standard.Category.Sniff', ]; From bd314c1dd5ca01a340f75c8159ebf8cd3c549c5e Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 11:57:12 +0100 Subject: [PATCH 10/26] Test an empty string --- tests/Core/Config/SniffListTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListTest.php index e44b542384..fed13ee984 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListTest.php @@ -66,6 +66,13 @@ public static function dataInvalidSniffs() $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; foreach ($arguments as $argument) { + // A standard is not a valid sniff. + $data[$argument.'; empty string'] = [ + 'argument' => $argument, + 'value' => '', + 'message' => sprintf($messageTemplate, ''), + ]; + // A standard is not a valid sniff. $data[$argument.'; standard'] = [ 'argument' => $argument, From 1ffe83a384a78ba43c03c92cb2b3bf8620ba6b28 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 5 May 2024 12:00:39 +0100 Subject: [PATCH 11/26] Rename testscase --- .../Config/{SniffListTest.php => SniffListValidationTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Core/Config/{SniffListTest.php => SniffListValidationTest.php} (98%) diff --git a/tests/Core/Config/SniffListTest.php b/tests/Core/Config/SniffListValidationTest.php similarity index 98% rename from tests/Core/Config/SniffListTest.php rename to tests/Core/Config/SniffListValidationTest.php index fed13ee984..45e149502b 100644 --- a/tests/Core/Config/SniffListTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -16,7 +16,7 @@ * * @covers \PHP_CodeSniffer\Config::processLongArgument */ -final class SniffListTest extends TestCase +final class SniffListValidationTest extends TestCase { From db76be838791c0c665f4d9884b3012443021d991 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:09:10 +0100 Subject: [PATCH 12/26] Add another test case --- tests/Core/Config/SniffListValidationTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index 45e149502b..60cd7e418f 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -144,10 +144,14 @@ public static function dataValidSniffs() $data = []; foreach ($arguments as $argument) { - $data[$argument.'; one valid sniff'] = [ + $data[$argument.'; one valid sniff'] = [ 'argument' => $argument, 'value' => 'Standard.Category.Sniff', ]; + $data[$argument.'; two valid sniffs'] = [ + 'argument' => $argument, + 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff', + ]; } return $data; From 389f112cd8a2dc1b3a2c7fa672343b9e384819bf Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:16:03 +0100 Subject: [PATCH 13/26] Correct indentation --- tests/Core/Config/SniffListValidationTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index 60cd7e418f..dc8aa203e5 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -68,9 +68,9 @@ public static function dataInvalidSniffs() foreach ($arguments as $argument) { // A standard is not a valid sniff. $data[$argument.'; empty string'] = [ - 'argument' => $argument, - 'value' => '', - 'message' => sprintf($messageTemplate, ''), + 'argument' => $argument, + 'value' => '', + 'message' => sprintf($messageTemplate, ''), ]; // A standard is not a valid sniff. @@ -144,14 +144,14 @@ public static function dataValidSniffs() $data = []; foreach ($arguments as $argument) { - $data[$argument.'; one valid sniff'] = [ - 'argument' => $argument, - 'value' => 'Standard.Category.Sniff', - ]; - $data[$argument.'; two valid sniffs'] = [ - 'argument' => $argument, - 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff', - ]; + $data[$argument.'; one valid sniff'] = [ + 'argument' => $argument, + 'value' => 'Standard.Category.Sniff', + ]; + $data[$argument.'; two valid sniffs'] = [ + 'argument' => $argument, + 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff', + ]; } return $data; From bd249fb9e11ed7264a30354c108d9482782080e8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:16:13 +0100 Subject: [PATCH 14/26] Mark test as not-risky --- tests/Core/Config/SniffListValidationTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index dc8aa203e5..ccc9aa0f95 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -120,6 +120,8 @@ public static function dataInvalidSniffs() * * @return void * @dataProvider dataValidSniffs + * + * @doesNotPerformAssertions */ public function testValid($argument, $value) { From e92830ddc18eee8f9ddc6a501d5b173c7e6780ec Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:20:24 +0100 Subject: [PATCH 15/26] Use more specific return type shape in comment --- tests/Core/Config/SniffListValidationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index ccc9aa0f95..8d1bff4ffe 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -53,7 +53,7 @@ public function testInvalid($argument, $value, $message) * Data provider for testInvalid(). * * @see self::testInvalid() - * @return array + * @return array> */ public static function dataInvalidSniffs() { @@ -135,7 +135,7 @@ public function testValid($argument, $value) * Data provider for testValid(). * * @see self::testValid() - * @return array + * @return array> */ public static function dataValidSniffs() { From b6df2599181d296fa2c37e7f1de92ff84ac5e69f Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:32:20 +0100 Subject: [PATCH 16/26] Assert configuration value is set on success --- tests/Core/Config/SniffListValidationTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index 8d1bff4ffe..6c3b73ae05 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -113,21 +113,21 @@ public static function dataInvalidSniffs() /** - * Ensure that the valid data does not throw an exception. + * Ensure that the valid data does not throw an exception, and the value is stored. * * @param string $argument 'sniffs' or 'exclude'. * @param string $value List of sniffs to include or exclude. * * @return void * @dataProvider dataValidSniffs - * - * @doesNotPerformAssertions */ public function testValid($argument, $value) { $config = new ConfigDouble(); $config->processLongArgument($argument.'='.$value, 0); + $this->assertSame(explode(',', $value), $config->$argument); + }//end testValid() From b18272d9a6a71bd3fa7caeaf6034422e31d110ea Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 6 May 2024 17:34:03 +0100 Subject: [PATCH 17/26] Add test case for setting multiple times --- tests/Core/Config/SniffListValidationTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffListValidationTest.php index 6c3b73ae05..31e9c391b3 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffListValidationTest.php @@ -161,4 +161,39 @@ public static function dataValidSniffs() }//end dataValidSniffs() + /** + * Ensure that only the first argument is processed and others are ignored. + * + * @param string $argument 'sniffs' or 'exclude'. + * + * @return void + * @dataProvider dataOnlySetOnce + */ + public function testOnlySetOnce($argument) + { + $config = new ConfigDouble(); + $config->processLongArgument($argument.'=StandardOne.Category.Sniff', 0); + $config->processLongArgument($argument.'=StandardTwo.Category.Sniff', 0); + $config->processLongArgument($argument.'=Standard.AnotherCategory.Sniff', 0); + + $this->assertSame(['StandardOne.Category.Sniff'], $config->$argument); + + }//end testOnlySetOnce() + + + /** + * Data provider for testOnlySetOnce(). + * + * @return string[] + */ + public static function dataOnlySetOnce() + { + return [ + 'sniffs' => ['sniffs'], + 'exclude' => ['exclude'], + ]; + + }//end dataOnlySetOnce() + + }//end class From 6400c61a989ede5cc0375d1e2a78b5ca944aa96d Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 8 May 2024 09:45:39 +0100 Subject: [PATCH 18/26] Rename class again Now that we have tests asserting the result of these arguments and not just validation of their input. --- .../{SniffListValidationTest.php => SniffsExcludeArgsTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Core/Config/{SniffListValidationTest.php => SniffsExcludeArgsTest.php} (99%) diff --git a/tests/Core/Config/SniffListValidationTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php similarity index 99% rename from tests/Core/Config/SniffListValidationTest.php rename to tests/Core/Config/SniffsExcludeArgsTest.php index 31e9c391b3..a34ae557a5 100644 --- a/tests/Core/Config/SniffListValidationTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -16,7 +16,7 @@ * * @covers \PHP_CodeSniffer\Config::processLongArgument */ -final class SniffListValidationTest extends TestCase +final class SniffsExcludeArgsTest extends TestCase { From 35d8a65b256288e7153fadf3d929c1fb2996b4c1 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 May 2024 22:12:57 +0100 Subject: [PATCH 19/26] Clarify shape of return array Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- tests/Core/Config/SniffsExcludeArgsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index a34ae557a5..45d4205f0d 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -53,7 +53,7 @@ public function testInvalid($argument, $value, $message) * Data provider for testInvalid(). * * @see self::testInvalid() - * @return array> + * @return array> */ public static function dataInvalidSniffs() { From f7aa3ef825f30026a42006721381d217944cba89 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 May 2024 22:13:12 +0100 Subject: [PATCH 20/26] Clarify shape of return array Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- tests/Core/Config/SniffsExcludeArgsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index 45d4205f0d..29965499e9 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -135,7 +135,7 @@ public function testValid($argument, $value) * Data provider for testValid(). * * @see self::testValid() - * @return array> + * @return array> */ public static function dataValidSniffs() { From fd5b8c78367f9ce6f513613f7bf793a94efe57e8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 May 2024 22:13:25 +0100 Subject: [PATCH 21/26] Clarify shape of return array Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- tests/Core/Config/SniffsExcludeArgsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index 29965499e9..f9fd92a7d9 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -184,7 +184,7 @@ public function testOnlySetOnce($argument) /** * Data provider for testOnlySetOnce(). * - * @return string[] + * @return array> */ public static function dataOnlySetOnce() { From 801aefaa80e0b3d043b6c0b8bf84fbd056d9899b Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 May 2024 22:15:35 +0100 Subject: [PATCH 22/26] Correct comment Fixes copy/paste error Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- tests/Core/Config/SniffsExcludeArgsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index f9fd92a7d9..538548f56c 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -66,7 +66,7 @@ public static function dataInvalidSniffs() $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; foreach ($arguments as $argument) { - // A standard is not a valid sniff. + // An empty string is not a valid sniff. $data[$argument.'; empty string'] = [ 'argument' => $argument, 'value' => '', From 81bea032d1de91ff036d29354f4e75c1834b8efe Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 13 May 2024 22:24:55 +0100 Subject: [PATCH 23/26] Set 'position' argument --- tests/Core/Config/SniffsExcludeArgsTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index 538548f56c..e4620ba0b0 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -171,10 +171,11 @@ public static function dataValidSniffs() */ public function testOnlySetOnce($argument) { - $config = new ConfigDouble(); - $config->processLongArgument($argument.'=StandardOne.Category.Sniff', 0); - $config->processLongArgument($argument.'=StandardTwo.Category.Sniff', 0); - $config->processLongArgument($argument.'=Standard.AnotherCategory.Sniff', 0); + $position = 0; + $config = new ConfigDouble(); + $config->processLongArgument($argument.'=StandardOne.Category.Sniff', $position++); + $config->processLongArgument($argument.'=StandardTwo.Category.Sniff', $position++); + $config->processLongArgument($argument.'=Standard.AnotherCategory.Sniff', $position++); $this->assertSame(['StandardOne.Category.Sniff'], $config->$argument); From 9d964ee104f9034cf221bd75915177b9ee009f9b Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 14 May 2024 08:50:51 +0100 Subject: [PATCH 24/26] Avoid using internal method --- tests/Core/Config/SniffsExcludeArgsTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index e4620ba0b0..4eedb624ea 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -32,7 +32,6 @@ final class SniffsExcludeArgsTest extends TestCase */ public function testInvalid($argument, $value, $message) { - $config = new ConfigDouble(); $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; if (method_exists($this, 'expectException') === true) { @@ -44,7 +43,7 @@ public function testInvalid($argument, $value, $message) $this->setExpectedException($exception, $message); } - $config->processLongArgument($argument.'='.$value, 0); + new ConfigDouble(["--$argument=$value"]); }//end testInvalid() @@ -123,8 +122,7 @@ public static function dataInvalidSniffs() */ public function testValid($argument, $value) { - $config = new ConfigDouble(); - $config->processLongArgument($argument.'='.$value, 0); + $config = new ConfigDouble(["--$argument=$value"]); $this->assertSame(explode(',', $value), $config->$argument); @@ -171,11 +169,13 @@ public static function dataValidSniffs() */ public function testOnlySetOnce($argument) { - $position = 0; - $config = new ConfigDouble(); - $config->processLongArgument($argument.'=StandardOne.Category.Sniff', $position++); - $config->processLongArgument($argument.'=StandardTwo.Category.Sniff', $position++); - $config->processLongArgument($argument.'=Standard.AnotherCategory.Sniff', $position++); + $config = new ConfigDouble( + [ + "--$argument=StandardOne.Category.Sniff", + "--$argument=StandardTwo.Category.Sniff", + "--$argument=Standard.AnotherCategory.Sniff", + ] + ); $this->assertSame(['StandardOne.Category.Sniff'], $config->$argument); From a0b31d9188b40d73ff0d0ea906cdeaa8ba92496e Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 14 May 2024 08:51:46 +0100 Subject: [PATCH 25/26] Mark internal method as internal --- src/Config.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Config.php b/src/Config.php index 46dd0e43f9..464108b96e 100644 --- a/src/Config.php +++ b/src/Config.php @@ -745,6 +745,11 @@ public function processShortArgument($arg, $pos) * * @return void * @throws \PHP_CodeSniffer\Exceptions\DeepExitException + * + * @internal This method is not intended to be used outside of this class. + * Its visibility will be restricted in a future version. Those + * wishing to pass / process command-line arguments should pass + * these to the __construct() method instead. */ public function processLongArgument($arg, $pos) { From 70358b0162b1f661c6bec56d5019e89ed711a4b7 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 14 May 2024 10:48:13 +0100 Subject: [PATCH 26/26] Revert "Mark internal method as internal" This reverts commit a0b31d9188b40d73ff0d0ea906cdeaa8ba92496e. --- src/Config.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Config.php b/src/Config.php index 464108b96e..46dd0e43f9 100644 --- a/src/Config.php +++ b/src/Config.php @@ -745,11 +745,6 @@ public function processShortArgument($arg, $pos) * * @return void * @throws \PHP_CodeSniffer\Exceptions\DeepExitException - * - * @internal This method is not intended to be used outside of this class. - * Its visibility will be restricted in a future version. Those - * wishing to pass / process command-line arguments should pass - * these to the __construct() method instead. */ public function processLongArgument($arg, $pos) {