From 795297d1d80ba2d5e8336c7ffa364b9c7748b948 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 4 Dec 2018 16:56:25 +0100 Subject: [PATCH] GetMethodPropertiesTest: remove even more duplicate code Implement use of the new AbstractMethodUnitTest::getTargetToken() method as well as abstract out the actual testing to a helper method as the logic was the same in all methods. I've chosen not to implement this with a data provider as the separate test methods creating the `$expected` arrays make the tests more readable and easier to understand. --- tests/Core/File/GetMethodPropertiesTest.inc | 3 +- tests/Core/File/GetMethodPropertiesTest.php | 209 +++----------------- 2 files changed, 33 insertions(+), 179 deletions(-) diff --git a/tests/Core/File/GetMethodPropertiesTest.inc b/tests/Core/File/GetMethodPropertiesTest.inc index dd51e2c008..5d183dda5b 100644 --- a/tests/Core/File/GetMethodPropertiesTest.inc +++ b/tests/Core/File/GetMethodPropertiesTest.inc @@ -7,7 +7,8 @@ function myFunction() {} /* testReturnFunction */ function myFunction(array ...$arrays): array { - return array_map(/* testNestedClosure */function(array $array): int { + /* testNestedClosure */ + return array_map(function(array $array): int { return array_sum($array); }, $arrays); } diff --git a/tests/Core/File/GetMethodPropertiesTest.php b/tests/Core/File/GetMethodPropertiesTest.php index 9204fc170e..c534c10051 100644 --- a/tests/Core/File/GetMethodPropertiesTest.php +++ b/tests/Core/File/GetMethodPropertiesTest.php @@ -33,18 +33,7 @@ public function testBasicFunction() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBasicFunction */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 2)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBasicFunction() @@ -67,18 +56,7 @@ public function testReturnFunction() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnFunction */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 2)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnFunction() @@ -101,18 +79,7 @@ public function testNestedClosure() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNestedClosure */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 1)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNestedClosure() @@ -135,18 +102,7 @@ public function testBasicMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBasicMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBasicMethod() @@ -169,18 +125,7 @@ public function testPrivateStaticMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPrivateStaticMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPrivateStaticMethod() @@ -203,18 +148,7 @@ public function testFinalMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testFinalMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testFinalMethod() @@ -237,18 +171,7 @@ public function testProtectedReturnMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testProtectedReturnMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testProtectedReturnMethod() @@ -271,18 +194,7 @@ public function testPublicReturnMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPublicReturnMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPublicReturnMethod() @@ -305,18 +217,7 @@ public function testNullableReturnMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNullableReturnMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNullableReturnMethod() @@ -339,18 +240,7 @@ public function testMessyNullableReturnMethod() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testMessyNullableReturnMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testMessyNullableReturnMethod() @@ -373,18 +263,7 @@ public function testReturnNamespace() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnNamespace */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnNamespace() @@ -407,18 +286,7 @@ public function testReturnMultilineNamespace() 'has_body' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testReturnMultilineNamespace */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 3)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testReturnMultilineNamespace() @@ -441,18 +309,7 @@ public function testAbstractMethod() 'has_body' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testAbstractMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 5)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testAbstractMethod() @@ -475,18 +332,7 @@ public function testAbstractReturnMethod() 'has_body' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testAbstractReturnMethod */' - ); - - $found = self::$phpcsFile->getMethodProperties(($function + 7)); - unset($found['return_type_token']); - $this->assertSame($expected, $found); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testAbstractReturnMethod() @@ -509,20 +355,27 @@ public function testInterfaceMethod() 'has_body' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testInterfaceMethod */' - ); + $this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected); + + }//end testInterfaceMethod() + - $found = self::$phpcsFile->getMethodProperties(($function + 3)); + /** + * Test helper. + * + * @param string $commentString The comment which preceeds the test. + * @param array $expected The expected function output. + * + * @return void + */ + private function getMethodPropertiesTestHelper($commentString, $expected) + { + $function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE]); + $found = self::$phpcsFile->getMethodProperties($function); unset($found['return_type_token']); $this->assertSame($expected, $found); - }//end testInterfaceMethod() + }//end getMethodPropertiesTestHelper() }//end class