* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/development:testing:unit_tests Wiki */ namespace VuFind\Marc\Test\Feature; /** * Reflection helper methods for unit tests. * * @category VuFind * @package Tests * @author David Maus * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/development:testing:unit_tests Wiki */ trait ReflectionTrait { /** * Call protected or private method for side-effect and result. * * Uses PHP's reflection API in order to modify method accessibility. * * @param object|string $object Object or class name * @param string $method Method name * @param array $arguments Method arguments * * @throws \ReflectionException Method does not exist * * @return mixed */ protected function callMethod($object, $method, array $arguments = []) { $reflectionMethod = new \ReflectionMethod($object, $method); $reflectionMethod->setAccessible(true); return $reflectionMethod->invokeArgs($object, $arguments); } /** * Return protected or private property. * * Uses PHP's reflection API in order to modify property accessibility. * * @param object|string $object Object or class name * @param string $property Property name * * @throws \ReflectionException Property does not exist * * @return mixed */ protected function getProperty($object, $property) { $reflectionProperty = new \ReflectionProperty($object, $property); $reflectionProperty->setAccessible(true); return $reflectionProperty->getValue($object); } /** * Set protected or private property. * * Uses PHP's reflection API in order to modify property accessibility. * * @param object|string $object Object or class name * @param string $property Property name * @param mixed $value Property value * * @throws \ReflectionException Property does not exist * * @return void */ protected function setProperty($object, $property, $value) { $reflectionProperty = new \ReflectionProperty($object, $property); $reflectionProperty->setAccessible(true); return $reflectionProperty->setValue($object, $value); } }