From 6931b5e6ec96c63026b8f977c8171433a0340bfe Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 19 Jul 2017 11:22:31 +0200 Subject: [PATCH] Create test utils --- Utils/TestUtils.php | 76 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Utils/TestUtils.php diff --git a/Utils/TestUtils.php b/Utils/TestUtils.php new file mode 100644 index 000000000..37b1ea2f1 --- /dev/null +++ b/Utils/TestUtils.php @@ -0,0 +1,76 @@ + + * @author Dennis Eichhorn + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +declare(strict_types=1); +namespace phpOMS\Utils; +/** + * Array utils. + * + * @category Framework + * @package phpOMS\Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class TestUtils +{ + public static function setMember($obj, $name, $value) : bool + { + $reflectionClass = new \ReflectionClass(get_class($obj)); + + if(!$reflectionClass->hasProperty($name)) { + return false; + } + + $reflectionProperty = $reflectionClass->getProperty($name); + + if (!($accessible = $reflectionProperty->isPublic())) { + $reflectionProperty->setAccessible(true); + } + + $reflectionProperty->setValue($obj, $value); + + if (!$accessible) { + $reflectionProperty->setAccessible(false); + } + + return true; + } + + public static function getMember($obj, $name) + { + $reflectionClass = new \ReflectionClass(get_class($obj)); + + if(!$reflectionClass->hasProperty($name)) { + return null; + } + + $reflectionProperty = $reflectionClass->getProperty($name); + + if (!($accessible = $reflectionProperty->isPublic())) { + $reflectionProperty->setAccessible(true); + } + + $value = $reflectionProperty->getValue($obj); + + if (!$accessible) { + $reflectionProperty->setAccessible(false); + } + + return $value; + } +}