From a36e96797a76defc0b7ec1f3d29c36c387a6484e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 8 Aug 2019 18:24:53 +0200 Subject: [PATCH] GetMethodParametersTest: 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. Also switched the actual assertion to `assertArraySubset()` which removes the need for all the `unset()`s for the exact token positions. Ref: https://phpunit.de/manual/4.8/en/appendixes.assertions.html#appendixes.assertions.assertArraySubset --- tests/Core/File/GetMethodParametersTest.inc | 3 +- tests/Core/File/GetMethodParametersTest.php | 206 +++----------------- 2 files changed, 29 insertions(+), 180 deletions(-) diff --git a/tests/Core/File/GetMethodParametersTest.inc b/tests/Core/File/GetMethodParametersTest.inc index a0ae183d42..a2c48b2919 100644 --- a/tests/Core/File/GetMethodParametersTest.inc +++ b/tests/Core/File/GetMethodParametersTest.inc @@ -19,7 +19,8 @@ function defaultValues($var1=1, $var2='value') {} function typeHint(foo $var1, bar $var2) {} class MyClass { -/* testSelfTypeHint */ function typeSelfHint(self $var) {} + /* testSelfTypeHint */ + function typeSelfHint(self $var) {} } /* testNullableTypeHint */ diff --git a/tests/Core/File/GetMethodParametersTest.php b/tests/Core/File/GetMethodParametersTest.php index e724762721..57d5e8af6b 100644 --- a/tests/Core/File/GetMethodParametersTest.php +++ b/tests/Core/File/GetMethodParametersTest.php @@ -32,23 +32,7 @@ public function testPassByReference() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testPassByReference */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testPassByReference() @@ -70,23 +54,7 @@ public function testArrayHint() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testArrayHint */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testArrayHint() @@ -117,29 +85,7 @@ public function testTypeHint() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testTypeHint */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testTypeHint() @@ -161,23 +107,7 @@ public function testSelfTypeHint() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testSelfTypeHint */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testSelfTypeHint() @@ -208,29 +138,7 @@ public function testNullableTypeHint() 'nullable_type' => true, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testNullableTypeHint */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testNullableTypeHint() @@ -252,23 +160,7 @@ public function testVariable() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testVariable */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testVariable() @@ -291,25 +183,7 @@ public function testSingleDefaultValue() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testSingleDefaultValue */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[0]['default_token']); - unset($found[0]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testSingleDefaultValue() @@ -341,33 +215,7 @@ public function testDefaultValues() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testDefaultValues */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[1]['token']); - unset($found[0]['type_hint_token']); - unset($found[1]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[1]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[1]['comma_token']); - unset($found[0]['reference_token']); - unset($found[1]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[1]['variadic_token']); - unset($found[0]['default_token']); - unset($found[1]['default_token']); - unset($found[0]['default_equal_token']); - unset($found[1]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testDefaultValues() @@ -390,27 +238,27 @@ public function testBitwiseAndConstantExpressionDefaultValue() 'nullable_type' => false, ]; - $start = (self::$phpcsFile->numTokens - 1); - $function = self::$phpcsFile->findPrevious( - T_COMMENT, - $start, - null, - false, - '/* testBitwiseAndConstantExpressionDefaultValue */' - ); - - $found = self::$phpcsFile->getMethodParameters(($function + 2)); - unset($found[0]['token']); - unset($found[0]['type_hint_token']); - unset($found[0]['type_hint_end_token']); - unset($found[0]['comma_token']); - unset($found[0]['reference_token']); - unset($found[0]['variadic_token']); - unset($found[0]['default_token']); - unset($found[0]['default_equal_token']); - $this->assertSame($expected, $found); + $this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected); }//end testBitwiseAndConstantExpressionDefaultValue() + /** + * Test helper. + * + * @param string $commentString The comment which preceeds the test. + * @param array $expected The expected function output. + * + * @return void + */ + private function getMethodParametersTestHelper($commentString, $expected) + { + $function = $this->getTargetToken($commentString, [T_FUNCTION]); + $found = self::$phpcsFile->getMethodParameters($function); + + $this->assertArraySubset($expected, $found, true); + + }//end getMethodParametersTestHelper() + + }//end class