Skip to content

Instantly share code, notes, and snippets.

@Gisleburt
Created October 21, 2015 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gisleburt/958c44cc63c5955c1ef3 to your computer and use it in GitHub Desktop.
Save Gisleburt/958c44cc63c5955c1ef3 to your computer and use it in GitHub Desktop.
Adding a method to PhpUnits TestCase to get any method from another class and return a closure that will call it with the provided arguments
<?php
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Gets returns a proxy for any method of an object, regardless of scope
* @param object $object Any object
* @param string $methodName The name of the method you want to proxy
* @return \Closure
*/
protected function getObjectMethod($object, $methodName)
{
if (!is_object($object)) {
throw new \InvalidArgumentException('Can not get method of non object');
}
$reflectionMethod = new \ReflectionMethod($object, $methodName);
$reflectionMethod->setAccessible(true);
return function () use ($object, $reflectionMethod) {
return $reflectionMethod->invokeArgs($object, func_get_args());
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment