Last active
January 15, 2017 16:00
-
-
Save jroenf/decf4d211096ca293dda2436d7258511 to your computer and use it in GitHub Desktop.
Test protected private method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ | |
* | |
* Call protected/private method of a class. | |
* | |
* @param object &$object Instantiated object that we will run method on. | |
* @param string $methodName Method name to call | |
* @param array $parameters Array of parameters to pass into method. | |
* | |
* @return mixed Method return. | |
*/ | |
public function invokeMethod(&$object, $methodName, array $parameters = array()) | |
{ | |
$reflection = new \ReflectionClass(get_class($object)); | |
$method = $reflection->getMethod($methodName); | |
$method->setAccessible(true); | |
return $method->invokeArgs($object, $parameters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment