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.
  • Loading branch information
jrfnl committed Jan 8, 2019
1 parent beedda0 commit a1849aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 124 deletions.
3 changes: 2 additions & 1 deletion tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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
154 changes: 31 additions & 123 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testPassByReference()

Expand All @@ -66,19 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testArrayHint()

Expand Down Expand Up @@ -109,21 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testTypeHint()

Expand All @@ -145,19 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testSelfTypeHint()

Expand Down Expand Up @@ -188,21 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testNullableTypeHint()

Expand All @@ -224,19 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testVariable()

Expand All @@ -259,19 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testSingleDefaultValue()

Expand Down Expand Up @@ -303,21 +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']);
$this->assertSame($expected, $found);
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testDefaultValues()

Expand All @@ -340,21 +238,31 @@ 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']);
$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);

foreach ($found as $key => $value) {
unset($found[$key]['token'], $found[$key]['type_hint_token']);
}

$this->assertSame($expected, $found);

}//end getMethodParametersTestHelper()


}//end class

0 comments on commit a1849aa

Please sign in to comment.