From d2d4b4780d5da28cda1dcc2294edbd80611ec2c4 Mon Sep 17 00:00:00 2001 From: Bernhard Zwein <52607214+benno5020@users.noreply.github.com> Date: Sat, 21 Jun 2025 21:43:03 +0200 Subject: [PATCH 1/4] Util/Common: refactor IsCamelCapsTest to use data providers This commit moves all existing test cases to data providers and adds descriptive names to better convey what they intend to test. See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/846 --- tests/Core/Util/Common/IsCamelCapsTest.php | 312 ++++++++++++++++++--- 1 file changed, 270 insertions(+), 42 deletions(-) diff --git a/tests/Core/Util/Common/IsCamelCapsTest.php b/tests/Core/Util/Common/IsCamelCapsTest.php index 951d761871..dfc08a9136 100644 --- a/tests/Core/Util/Common/IsCamelCapsTest.php +++ b/tests/Core/Util/Common/IsCamelCapsTest.php @@ -24,117 +24,345 @@ final class IsCamelCapsTest extends TestCase /** * Test valid public function/method names. * + * @param string $name The tested name. + * @param bool $strict Value of the $strict flag. + * + * @dataProvider dataValidNotClassFormatPublic + * * @return void */ - public function testValidNotClassFormatPublic() + public function testValidNotClassFormatPublic($name, $strict) { - $this->assertTrue(Common::isCamelCaps('thisIsCamelCaps', false, true, true)); - $this->assertTrue(Common::isCamelCaps('thisISCamelCaps', false, true, false)); + $this->assertTrue(Common::isCamelCaps($name, false, true, $strict)); }//end testValidNotClassFormatPublic() + /** + * Data provider. + * + * @see testValidNotClassFormatPublic() + * + * @return array> + */ + public static function dataValidNotClassFormatPublic() + { + return [ + 'lower camelCase string in strict mode' => [ + 'name' => 'thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with acronym in relaxed mode' => [ + 'name' => 'thisISCamelCaps', + 'strict' => false, + ], + ]; + + }//end dataValidNotClassFormatPublic() + + /** * Test invalid public function/method names. * + * @param string $name The tested name. + * + * @dataProvider dataInvalidNotClassFormatPublic + * * @return void */ - public function testInvalidNotClassFormatPublic() + public function testInvalidNotClassFormatPublic($name) { - $this->assertFalse(Common::isCamelCaps('_thisIsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('thisISCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('ThisIsCamelCaps', false, true, true)); + $this->assertFalse(Common::isCamelCaps($name, false, true, true)); - $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, true, true)); + }//end testInvalidNotClassFormatPublic() - $this->assertFalse(Common::isCamelCaps('this*IsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('this-IsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('this_IsCamelCaps', false, true, true)); - $this->assertFalse(Common::isCamelCaps('this_is_camel_caps', false, true, true)); - }//end testInvalidNotClassFormatPublic() + /** + * Data provider. + * + * @see testInvalidNotClassFormatPublic() + * + * @return array> + */ + public static function dataInvalidNotClassFormatPublic() + { + return [ + 'string with initial underscore (invalid when $public is true)' => [ + 'name' => '_thisIsCamelCaps', + ], + 'lower camelCase string with acronym (invalid when $strict is true)' => [ + 'name' => 'thisISCamelCaps', + ], + 'PascalCase string' => [ + 'name' => 'ThisIsCamelCaps', + ], + 'lower camelCase string with initial digit' => [ + 'name' => '3thisIsCamelCaps', + ], + 'lower camelCase string with initial [^a-zA-z_] character: *' => [ + 'name' => '*thisIsCamelCaps', + ], + 'lower camelCase string with initial [^a-zA-z_] character: -' => [ + 'name' => '-thisIsCamelCaps', + ], + 'lower camelCase string with medial [^a-zA-z_] character: *' => [ + 'name' => 'this*IsCamelCaps', + ], + 'lower camelCase string with medial [^a-zA-z_] character: -' => [ + 'name' => 'this-IsCamelCaps', + ], + 'lower camelCase string with single medial underscore' => [ + 'name' => 'this_IsCamelCaps', + ], + 'snake_case string' => [ + 'name' => 'this_is_camel_caps', + ], + ]; + + }//end dataInvalidNotClassFormatPublic() /** * Test valid private method names. * + * @param string $name The tested name. + * @param bool $strict Value of the $strict flag. + * + * @dataProvider dataValidNotClassFormatPrivate + * * @return void */ - public function testValidNotClassFormatPrivate() + public function testValidNotClassFormatPrivate($name, $strict) { - $this->assertTrue(Common::isCamelCaps('_thisIsCamelCaps', false, false, true)); - $this->assertTrue(Common::isCamelCaps('_thisISCamelCaps', false, false, false)); - $this->assertTrue(Common::isCamelCaps('_i18N', false, false, true)); - $this->assertTrue(Common::isCamelCaps('_i18n', false, false, true)); + $this->assertTrue(Common::isCamelCaps($name, false, false, $strict)); }//end testValidNotClassFormatPrivate() + /** + * Data provider. + * + * @see testValidNotClassFormatPrivate() + * + * @return array> + */ + public static function dataValidNotClassFormatPrivate() + { + return [ + 'lower camelCase string with initial underscore' => [ + 'name' => '_thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with acronym and initial underscore' => [ + 'name' => '_thisISCamelCaps', + 'strict' => false, + ], + '_i18N' => [ + 'name' => '_i18N', + 'strict' => true, + ], + '_i18n' => [ + 'name' => '_i18n', + 'strict' => true, + ], + ]; + + }//end dataValidNotClassFormatPrivate() + + /** * Test invalid private method names. * + * @param string $name The tested name. + * @param bool $strict Value of the $strict flag. + * + * @dataProvider dataInvalidNotClassFormatPrivate + * * @return void */ - public function testInvalidNotClassFormatPrivate() + public function testInvalidNotClassFormatPrivate($name, $strict) { - $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('_thisISCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('__thisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('__thisISCamelCaps', false, false, false)); - - $this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, false, true)); - $this->assertFalse(Common::isCamelCaps('_this_is_camel_caps', false, false, true)); + $this->assertFalse(Common::isCamelCaps($name, false, false, $strict)); }//end testInvalidNotClassFormatPrivate() + /** + * Data provider. + * + * @see testInvalidNotClassFormatPrivate() + * + * @return array> + */ + public static function dataInvalidNotClassFormatPrivate() + { + return [ + 'lower camelCase string without initial underscore' => [ + 'name' => 'thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with initial underscore, but with an acronym, in strict mode' => [ + 'name' => '_thisISCamelCaps', + 'strict' => true, + ], + 'PascalCase string with initial underscore' => [ + 'name' => '_ThisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with two initial underscores' => [ + 'name' => '__thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with two initial underscores and acronym in relaxed mode' => [ + 'name' => '__thisISCamelCaps', + 'strict' => false, + ], + 'lower camelCase string with initial digit' => [ + 'name' => '3thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with initial [^a-zA-Z_] character: *' => [ + 'name' => '*thisIsCamelCaps', + 'strict' => true, + ], + 'lower camelCase string with initial [^a-zA-Z_] character: -' => [ + 'name' => '-thisIsCamelCaps', + 'strict' => true, + ], + 'snake_case string with initial underscore' => [ + 'name' => '_this_is_camel_caps', + 'strict' => true, + ], + ]; + + }//end dataInvalidNotClassFormatPrivate() + + /** * Test valid class names. * + * @param string $name The tested name. + * @param bool $strict Value of the $strict flag. + * + * @dataProvider dataValidClassFormatPublic + * * @return void */ - public function testValidClassFormatPublic() + public function testValidClassFormatPublic($name, $strict) { - $this->assertTrue(Common::isCamelCaps('ThisIsCamelCaps', true, true, true)); - $this->assertTrue(Common::isCamelCaps('ThisISCamelCaps', true, true, false)); - $this->assertTrue(Common::isCamelCaps('This3IsCamelCaps', true, true, false)); + $this->assertTrue(Common::isCamelCaps($name, true, true, $strict)); }//end testValidClassFormatPublic() + /** + * Data provider. + * + * @see testValidClassFormatPublic() + * + * @return array> + */ + public static function dataValidClassFormatPublic() + { + return [ + 'PascalCase string' => [ + 'name' => 'ThisIsCamelCaps', + 'strict' => true, + ], + 'PascalCase string with acronym' => [ + 'name' => 'ThisISCamelCaps', + 'strict' => false, + ], + 'PascalCase string with digit between words' => [ + 'name' => 'This3IsCamelCaps', + 'strict' => false, + ], + ]; + + }//end dataValidClassFormatPublic() + + /** * Test invalid class names. * + * @param string $name The tested name. + * + * @dataProvider dataInvalidClassFormat + * * @return void */ - public function testInvalidClassFormat() + public function testInvalidClassFormat($name) { - $this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', true)); - $this->assertFalse(Common::isCamelCaps('This-IsCamelCaps', true)); - $this->assertFalse(Common::isCamelCaps('This_Is_Camel_Caps', true)); + $this->assertFalse(Common::isCamelCaps($name, true)); }//end testInvalidClassFormat() + /** + * Data provider. + * + * @see testInvalidClassFormat() + * + * @return array> + */ + public static function dataInvalidClassFormat() + { + return [ + 'lower camelCase string' => [ + 'name' => 'thisIsCamelCaps', + ], + 'PascalCase string with medial illegal character: -' => [ + 'name' => 'This-IsCamelCaps', + ], + 'capitalised snake case' => [ + 'name' => 'This_Is_Camel_Caps', + ], + ]; + + }//end dataInvalidClassFormat() + + /** * Test invalid class names with the private flag set. * * Note that the private flag is ignored if the class format * flag is set, so these names are all invalid. * + * @param string $name The tested name. + * @param bool $public Value of the $public flag. + * + * @dataProvider dataInvalidClassFormatPrivate + * * @return void */ - public function testInvalidClassFormatPrivate() + public function testInvalidClassFormatPrivate($name, $public) { - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, true)); - $this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, false)); + $this->assertFalse(Common::isCamelCaps($name, true, $public)); }//end testInvalidClassFormatPrivate() + /** + * Data provider. + * + * @see testInvalidClassFormatPrivate() + * + * @return array> + */ + public static function dataInvalidClassFormatPrivate() + { + return [ + 'PascalCase string with initial underscore (public)' => [ + 'name' => '_ThisIsCamelCaps', + 'public' => true, + ], + 'PascalCase string with initial underscore (private)' => [ + 'name' => '_ThisIsCamelCaps', + 'public' => false, + ], + ]; + + }//end dataInvalidClassFormatPrivate() + + }//end class From e2052af783fb139108a7c840d579cec539856b3c Mon Sep 17 00:00:00 2001 From: Bernhard Zwein <52607214+benno5020@users.noreply.github.com> Date: Sun, 22 Jun 2025 14:24:07 +0200 Subject: [PATCH 2/4] Util/Common: add tests to IsCamelCapsTest This commit aims to further document existing behaviour and tries to anticipate potential mistakes if the regular expressions are ever changed. See https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/846 --- .cspell.json | 1 + tests/Core/Util/Common/IsCamelCapsTest.php | 176 +++++++++++++++++++-- 2 files changed, 161 insertions(+), 16 deletions(-) diff --git a/.cspell.json b/.cspell.json index a495f45094..dc3c938471 100644 --- a/.cspell.json +++ b/.cspell.json @@ -43,6 +43,7 @@ "mins", "nekudotayim", "nowdoc", + "numeronym", "paamayim", "pcre", "php's", diff --git a/tests/Core/Util/Common/IsCamelCapsTest.php b/tests/Core/Util/Common/IsCamelCapsTest.php index dfc08a9136..205baceb43 100644 --- a/tests/Core/Util/Common/IsCamelCapsTest.php +++ b/tests/Core/Util/Common/IsCamelCapsTest.php @@ -56,6 +56,10 @@ public static function dataValidNotClassFormatPublic() 'name' => 'thisISCamelCaps', 'strict' => false, ], + 'lower camelCase string with initial acronym' => [ + 'name' => 'ISThisCamelCaps', + 'strict' => false, + ], ]; }//end dataValidNotClassFormatPublic() @@ -87,36 +91,49 @@ public function testInvalidNotClassFormatPublic($name) public static function dataInvalidNotClassFormatPublic() { return [ - 'string with initial underscore (invalid when $public is true)' => [ + 'string with initial underscore (invalid when $public is true)' => [ 'name' => '_thisIsCamelCaps', ], - 'lower camelCase string with acronym (invalid when $strict is true)' => [ + 'lower camelCase string with acronym (invalid when $strict is true)' => [ 'name' => 'thisISCamelCaps', ], - 'PascalCase string' => [ + 'lower camelCase string with initial acronym (invalid when $strict is true)' => [ + 'name' => 'ISThisCamelCaps', + ], + 'PascalCase string' => [ 'name' => 'ThisIsCamelCaps', ], - 'lower camelCase string with initial digit' => [ + 'lower camelCase string with initial digit' => [ 'name' => '3thisIsCamelCaps', ], - 'lower camelCase string with initial [^a-zA-z_] character: *' => [ + 'lower camelCase string with initial illegal character: *' => [ 'name' => '*thisIsCamelCaps', ], - 'lower camelCase string with initial [^a-zA-z_] character: -' => [ + 'lower camelCase string with initial illegal character: -' => [ 'name' => '-thisIsCamelCaps', ], - 'lower camelCase string with medial [^a-zA-z_] character: *' => [ + 'lower camelCase string with initial illegal character: é' => [ + 'name' => 'éCamelCaps', + ], + 'lower camelCase string with medial illegal character: *' => [ 'name' => 'this*IsCamelCaps', ], - 'lower camelCase string with medial [^a-zA-z_] character: -' => [ + 'lower camelCase string with medial illegal character: -' => [ 'name' => 'this-IsCamelCaps', ], - 'lower camelCase string with single medial underscore' => [ + 'lower camelCase string with medial illegal character: é' => [ + // No camels were harmed in the cspell:disable-next-line. + 'name' => 'thisIsCamélCaps', + ], + 'lower camelCase string with single medial underscore' => [ 'name' => 'this_IsCamelCaps', ], - 'snake_case string' => [ + 'snake_case string' => [ 'name' => 'this_is_camel_caps', ], + 'empty string' => [ + 'name' => '', + ], ]; }//end dataInvalidNotClassFormatPublic() @@ -149,19 +166,23 @@ public function testValidNotClassFormatPrivate($name, $strict) public static function dataValidNotClassFormatPrivate() { return [ - 'lower camelCase string with initial underscore' => [ + 'lower camelCase string with initial underscore' => [ 'name' => '_thisIsCamelCaps', 'strict' => true, ], - 'lower camelCase string with acronym and initial underscore' => [ + 'lower camelCase string with acronym and initial underscore' => [ 'name' => '_thisISCamelCaps', 'strict' => false, ], - '_i18N' => [ + 'lower camelCase string with acronym after initial underscore' => [ + 'name' => '_ISThisCamelCaps', + 'strict' => false, + ], + 'numeronym with initial underscore and capital after digit' => [ 'name' => '_i18N', 'strict' => true, ], - '_i18n' => [ + 'numeronym with initial underscore and lowercase character after digit' => [ 'name' => '_i18n', 'strict' => true, ], @@ -221,18 +242,30 @@ public static function dataInvalidNotClassFormatPrivate() 'name' => '3thisIsCamelCaps', 'strict' => true, ], - 'lower camelCase string with initial [^a-zA-Z_] character: *' => [ + 'lower camelCase string with initial illegal character: *' => [ 'name' => '*thisIsCamelCaps', 'strict' => true, ], - 'lower camelCase string with initial [^a-zA-Z_] character: -' => [ + 'lower camelCase string with initial illegal character: -' => [ 'name' => '-thisIsCamelCaps', 'strict' => true, ], + 'lower camelCase string with initial illegal character: é' => [ + 'name' => 'éCamelCaps', + 'strict' => true, + ], 'snake_case string with initial underscore' => [ 'name' => '_this_is_camel_caps', 'strict' => true, ], + 'single underscore' => [ + 'name' => '_', + 'strict' => true, + ], + 'empty string' => [ + 'name' => '', + 'strict' => true, + ], ]; }//end dataInvalidNotClassFormatPrivate() @@ -277,6 +310,26 @@ public static function dataValidClassFormatPublic() 'name' => 'This3IsCamelCaps', 'strict' => false, ], + 'PascalCase string with digit inside word' => [ + 'name' => 'Th1sIsCamelCaps', + 'strict' => false, + ], + 'Single capital (strict)' => [ + 'name' => 'A', + 'strict' => true, + ], + 'Single capital with digit (strict)' => [ + 'name' => 'A1', + 'strict' => true, + ], + 'Single capital (relaxed)' => [ + 'name' => 'A', + 'strict' => false, + ], + 'Single capital with digit (relaxed)' => [ + 'name' => 'A1', + 'strict' => false, + ], ]; }//end dataValidClassFormatPublic() @@ -317,6 +370,9 @@ public static function dataInvalidClassFormat() 'capitalised snake case' => [ 'name' => 'This_Is_Camel_Caps', ], + 'empty string' => [ + 'name' => '', + ], ]; }//end dataInvalidClassFormat() @@ -360,9 +416,97 @@ public static function dataInvalidClassFormatPrivate() 'name' => '_ThisIsCamelCaps', 'public' => false, ], + 'empty string (public)' => [ + 'name' => '', + 'public' => true, + ], + 'empty string (private)' => [ + 'name' => '', + 'public' => false, + ], ]; }//end dataInvalidClassFormatPrivate() + /** + * Test valid strings with default arguments. + * + * @param string $name The tested name. + * + * @dataProvider dataValidDefaultArguments + * + * @return void + */ + public function testValidDefaultArguments($name) + { + $this->assertTrue(Common::isCamelCaps($name)); + + }//end testValidDefaultArguments() + + + /** + * Data provider. + * + * @see testValidDefaultArguments() + * + * @return array> + */ + public static function dataValidDefaultArguments() + { + return [ + 'lower camelCase string' => [ + 'name' => 'thisIsCamelCaps', + ], + 'lower camelCase string with medial digit' => [ + 'name' => 'this3IsCamelCaps', + ], + ]; + + }//end dataValidDefaultArguments() + + + /** + * Test invalid strings with default arguments. + * + * @param string $name The tested name. + * + * @dataProvider dataInvalidDefaultArguments + * + * @return void + */ + public function testInvalidDefaultArguments($name) + { + $this->assertFalse(Common::isCamelCaps($name)); + + }//end testInvalidDefaultArguments() + + + /** + * Data provider. + * + * @see testInvalidDefaultArguments() + * + * @return array> + */ + public static function dataInvalidDefaultArguments() + { + return [ + 'PascalCase string' => [ + 'name' => 'ThisIsCamelCaps', + ], + 'PascalCase string with acronym' => [ + 'name' => 'ThisISCamelCaps', + ], + 'lower camelCase string with initial underscore' => [ + 'name' => '_thisIsCamelCaps', + ], + 'lower camelCase string with acronym' => [ + 'name' => 'thisISCamelCaps', + ], + ]; + + }//end dataInvalidDefaultArguments() + + }//end class From 7ca7ed6890b0ddbf8b56fc2b44f92bff935b1cc2 Mon Sep 17 00:00:00 2001 From: Bernhard Zwein <52607214+benno5020@users.noreply.github.com> Date: Sun, 22 Jun 2025 14:50:44 +0200 Subject: [PATCH 3/4] Util/Common: fix typo in DocBlock --- src/Util/Common.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/Common.php b/src/Util/Common.php index cb6965f62c..eb935f4a31 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -330,7 +330,7 @@ public static function stripColors($text) /** * Returns true if the specified string is in the camel caps format. * - * @param string $string The string the verify. + * @param string $string The string to verify. * @param boolean $classFormat If true, check to see if the string is in the * class format. Class format strings must start * with a capital letter and contain no From ff8e0e03787b352909d1954fbff5ceeffdb38236 Mon Sep 17 00:00:00 2001 From: Bernhard Zwein <52607214+benno5020@users.noreply.github.com> Date: Thu, 26 Jun 2025 22:04:32 +0200 Subject: [PATCH 4/4] Util/Common: rename test method --- tests/Core/Util/Common/IsCamelCapsTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Core/Util/Common/IsCamelCapsTest.php b/tests/Core/Util/Common/IsCamelCapsTest.php index 205baceb43..609334aa6d 100644 --- a/tests/Core/Util/Common/IsCamelCapsTest.php +++ b/tests/Core/Util/Common/IsCamelCapsTest.php @@ -379,33 +379,33 @@ public static function dataInvalidClassFormat() /** - * Test invalid class names with the private flag set. + * Test invalid class names with the public flag set. * - * Note that the private flag is ignored if the class format + * Note that the public flag is ignored if the class format * flag is set, so these names are all invalid. * * @param string $name The tested name. * @param bool $public Value of the $public flag. * - * @dataProvider dataInvalidClassFormatPrivate + * @dataProvider dataInvalidClassFormatWithPublicFlag * * @return void */ - public function testInvalidClassFormatPrivate($name, $public) + public function testInvalidClassFormatWithPublicFlag($name, $public) { $this->assertFalse(Common::isCamelCaps($name, true, $public)); - }//end testInvalidClassFormatPrivate() + }//end testInvalidClassFormatWithPublicFlag() /** * Data provider. * - * @see testInvalidClassFormatPrivate() + * @see testInvalidClassFormatWithPublicFlag() * * @return array> */ - public static function dataInvalidClassFormatPrivate() + public static function dataInvalidClassFormatWithPublicFlag() { return [ 'PascalCase string with initial underscore (public)' => [ @@ -426,7 +426,7 @@ public static function dataInvalidClassFormatPrivate() ], ]; - }//end dataInvalidClassFormatPrivate() + }//end dataInvalidClassFormatWithPublicFlag() /**