Skip to content

Commit

Permalink
GetMethodPropertiesTest: 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 Dec 4, 2018
1 parent b024214 commit 0b6c300
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 179 deletions.
3 changes: 2 additions & 1 deletion tests/Core/File/GetMethodPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
209 changes: 31 additions & 178 deletions tests/Core/File/GetMethodPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 0b6c300

Please sign in to comment.