Created
October 21, 2015 15:32
-
-
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
This file contains 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
<?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