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; + } +}