From 976547dbc5997bcc570ec31b4532111cdd10a2f7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 29 Dec 2023 09:24:45 +0100 Subject: [PATCH 1/3] Tests/FindExtendedClassNameTest: improve test markers Make the test marker names more descriptive --- tests/Core/File/FindExtendedClassNameTest.inc | 10 +++++----- tests/Core/File/FindExtendedClassNameTest.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/Core/File/FindExtendedClassNameTest.inc b/tests/Core/File/FindExtendedClassNameTest.inc index aead06cd9d..cbbec87f60 100644 --- a/tests/Core/File/FindExtendedClassNameTest.inc +++ b/tests/Core/File/FindExtendedClassNameTest.inc @@ -4,22 +4,22 @@ namespace PHP_CodeSniffer\Tests\Core\File; class testFECNClass {} -/* testExtendedClass */ +/* testExtendsUnqualifiedClass */ class testFECNExtendedClass extends testFECNClass {} -/* testNamespacedClass */ +/* testExtendsFullyQualifiedClass */ class testFECNNamespacedClass extends \PHP_CodeSniffer\Tests\Core\File\testFECNClass {} /* testNonExtendedClass */ class testFECNNonExtendedClass {} -/* testInterface */ +/* testNonExtendedInterface */ interface testFECNInterface {} -/* testInterfaceThatExtendsInterface */ +/* testInterfaceExtendsUnqualifiedInterface */ interface testInterfaceThatExtendsInterface extends testFECNInterface{} -/* testInterfaceThatExtendsFQCNInterface */ +/* testInterfaceExtendsFullyQualifiedInterface */ interface testInterfaceThatExtendsFQCNInterface extends \PHP_CodeSniffer\Tests\Core\File\testFECNInterface{} /* testNestedExtendedClass */ diff --git a/tests/Core/File/FindExtendedClassNameTest.php b/tests/Core/File/FindExtendedClassNameTest.php index c54b9cc120..9246742ef6 100644 --- a/tests/Core/File/FindExtendedClassNameTest.php +++ b/tests/Core/File/FindExtendedClassNameTest.php @@ -51,11 +51,11 @@ public function dataExtendedClass() { return [ [ - '/* testExtendedClass */', + '/* testExtendsUnqualifiedClass */', 'testFECNClass', ], [ - '/* testNamespacedClass */', + '/* testExtendsFullyQualifiedClass */', '\PHP_CodeSniffer\Tests\Core\File\testFECNClass', ], [ @@ -63,15 +63,15 @@ public function dataExtendedClass() false, ], [ - '/* testInterface */', + '/* testNonExtendedInterface */', false, ], [ - '/* testInterfaceThatExtendsInterface */', + '/* testInterfaceExtendsUnqualifiedInterface */', 'testFECNInterface', ], [ - '/* testInterfaceThatExtendsFQCNInterface */', + '/* testInterfaceExtendsFullyQualifiedInterface */', '\PHP_CodeSniffer\Tests\Core\File\testFECNInterface', ], [ From f627f0e58e2c4bbd399707b2905d6f275020c91a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 29 Dec 2023 09:11:24 +0100 Subject: [PATCH 2/3] Tests/FindExtendedClassNameTest: use named data sets With non-named data sets, when a test fails, PHPUnit will display the number of the test which failed. With tests which have a _lot_ of data sets, this makes it _interesting_ (and time-consuming) to debug those, as one now has to figure out which of the data sets in the data provider corresponds to that number. Using named data sets makes debugging failing tests more straight forward as PHPUnit will display the data set name instead of the number. Using named data sets also documents what exactly each data set is testing. Aside from adding the data set name, this commit also adds the parameter name for each item in the data set, this time in an effort to make it more straight forward to update and add tests as it will be more obvious what each key in the data set signifies. Includes fixing the data types in the docblocks and making them more specific, where relevant. --- tests/Core/File/FindExtendedClassNameTest.php | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/Core/File/FindExtendedClassNameTest.php b/tests/Core/File/FindExtendedClassNameTest.php index 9246742ef6..0ba6c47927 100644 --- a/tests/Core/File/FindExtendedClassNameTest.php +++ b/tests/Core/File/FindExtendedClassNameTest.php @@ -24,8 +24,8 @@ class FindExtendedClassNameTest extends AbstractMethodUnitTest * Test retrieving the name of the class being extended by another class * (or interface). * - * @param string $identifier Comment which precedes the test case. - * @param bool $expected Expected function output. + * @param string $identifier Comment which precedes the test case. + * @param string|false $expected Expected function output. * * @dataProvider dataExtendedClass * @@ -45,50 +45,50 @@ public function testFindExtendedClassName($identifier, $expected) * * @see testFindExtendedClassName() * - * @return array + * @return array> */ public function dataExtendedClass() { return [ - [ - '/* testExtendsUnqualifiedClass */', - 'testFECNClass', + 'class extends unqualified class' => [ + 'identifier' => '/* testExtendsUnqualifiedClass */', + 'expected' => 'testFECNClass', ], - [ - '/* testExtendsFullyQualifiedClass */', - '\PHP_CodeSniffer\Tests\Core\File\testFECNClass', + 'class extends fully qualified class' => [ + 'identifier' => '/* testExtendsFullyQualifiedClass */', + 'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNClass', ], - [ - '/* testNonExtendedClass */', - false, + 'class does not extend' => [ + 'identifier' => '/* testNonExtendedClass */', + 'expected' => false, ], - [ - '/* testNonExtendedInterface */', - false, + 'interface does not extend' => [ + 'identifier' => '/* testNonExtendedInterface */', + 'expected' => false, ], - [ - '/* testInterfaceExtendsUnqualifiedInterface */', - 'testFECNInterface', + 'interface extends unqualified interface' => [ + 'identifier' => '/* testInterfaceExtendsUnqualifiedInterface */', + 'expected' => 'testFECNInterface', ], - [ - '/* testInterfaceExtendsFullyQualifiedInterface */', - '\PHP_CodeSniffer\Tests\Core\File\testFECNInterface', + 'interface extends fully qualified interface' => [ + 'identifier' => '/* testInterfaceExtendsFullyQualifiedInterface */', + 'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNInterface', ], - [ - '/* testNestedExtendedClass */', - false, + 'class does not extend but contains anon class which extends' => [ + 'identifier' => '/* testNestedExtendedClass */', + 'expected' => false, ], - [ - '/* testNestedExtendedAnonClass */', - 'testFECNAnonClass', + 'anon class extends, nested in non-extended class' => [ + 'identifier' => '/* testNestedExtendedAnonClass */', + 'expected' => 'testFECNAnonClass', ], - [ - '/* testClassThatExtendsAndImplements */', - 'testFECNClass', + 'class extends and implements' => [ + 'identifier' => '/* testClassThatExtendsAndImplements */', + 'expected' => 'testFECNClass', ], - [ - '/* testClassThatImplementsAndExtends */', - 'testFECNClass', + 'class implements and extends' => [ + 'identifier' => '/* testClassThatImplementsAndExtends */', + 'expected' => 'testFECNClass', ], ]; From 92ca078a5283b63f649d6bd0e03c6a9e2ff740aa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 29 Dec 2023 09:52:41 +0100 Subject: [PATCH 3/3] Tests/FindExtendedClassNameTest: add extra tests This adds some extra tests which were already in use in PHPCSUtils. This brings test coverage for this method up to 100%. It also cleans up the test case file a little by removing some code which isn't actually used in the tests (namespace declaration) and moves the "class not extended" test up. --- tests/Core/File/FindExtendedClassNameTest.inc | 23 ++++++-- tests/Core/File/FindExtendedClassNameTest.php | 53 +++++++++++++++++-- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/tests/Core/File/FindExtendedClassNameTest.inc b/tests/Core/File/FindExtendedClassNameTest.inc index cbbec87f60..dae2cd9692 100644 --- a/tests/Core/File/FindExtendedClassNameTest.inc +++ b/tests/Core/File/FindExtendedClassNameTest.inc @@ -1,8 +1,10 @@ findExtendedClassName(100000); + $this->assertFalse($result); + + }//end testNonExistentToken() + + + /** + * Test getting a `false` result when a token other than one of the supported tokens is passed. + * + * @return void + */ + public function testNotAClass() + { + $token = $this->getTargetToken('/* testNotAClass */', [T_FUNCTION]); + $result = self::$phpcsFile->findExtendedClassName($token); + $this->assertFalse($result); + + }//end testNotAClass() + + /** * Test retrieving the name of the class being extended by another class * (or interface). @@ -50,6 +77,10 @@ public function testFindExtendedClassName($identifier, $expected) public function dataExtendedClass() { return [ + 'class does not extend' => [ + 'identifier' => '/* testNonExtendedClass */', + 'expected' => false, + ], 'class extends unqualified class' => [ 'identifier' => '/* testExtendsUnqualifiedClass */', 'expected' => 'testFECNClass', @@ -58,9 +89,9 @@ public function dataExtendedClass() 'identifier' => '/* testExtendsFullyQualifiedClass */', 'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNClass', ], - 'class does not extend' => [ - 'identifier' => '/* testNonExtendedClass */', - 'expected' => false, + 'class extends partially qualified class' => [ + 'identifier' => '/* testExtendsPartiallyQualifiedClass */', + 'expected' => 'Core\File\RelativeClass', ], 'interface does not extend' => [ 'identifier' => '/* testNonExtendedInterface */', @@ -74,6 +105,10 @@ public function dataExtendedClass() 'identifier' => '/* testInterfaceExtendsFullyQualifiedInterface */', 'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNInterface', ], + 'anon class extends unqualified class' => [ + 'identifier' => '/* testExtendedAnonClass */', + 'expected' => 'testFECNExtendedAnonClass', + ], 'class does not extend but contains anon class which extends' => [ 'identifier' => '/* testNestedExtendedClass */', 'expected' => false, @@ -90,6 +125,18 @@ public function dataExtendedClass() 'identifier' => '/* testClassThatImplementsAndExtends */', 'expected' => 'testFECNClass', ], + 'interface extends multiple interfaces (not supported)' => [ + 'identifier' => '/* testInterfaceMultiExtends */', + 'expected' => '\Package\FooInterface', + ], + 'parse error - extends keyword, but no class name' => [ + 'identifier' => '/* testMissingExtendsName */', + 'expected' => false, + ], + 'parse error - live coding - no curly braces' => [ + 'identifier' => '/* testParseError */', + 'expected' => false, + ], ]; }//end dataExtendedClass()