Skip to content

Instantly share code, notes, and snippets.

@jroenf
Last active January 15, 2017 16:00
Show Gist options
  • Save jroenf/decf4d211096ca293dda2436d7258511 to your computer and use it in GitHub Desktop.
Save jroenf/decf4d211096ca293dda2436d7258511 to your computer and use it in GitHub Desktop.
Test protected private method
/**
* 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