Skip to content

Commit

Permalink
GetMethodParametersTest: remove even more duplicate code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Aug 27, 2019
1 parent 281fc7d commit a36e967
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 180 deletions.
3 changes: 2 additions & 1 deletion tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
206 changes: 27 additions & 179 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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

0 comments on commit a36e967

Please sign in to comment.