PHPUnit_Util_Class::getObjectAttribute PHP Method

getObjectAttribute() public static method

This also works for attributes that are declared protected or private.
public static getObjectAttribute ( object $object, string $attributeName ) : mixed
$object object
$attributeName string
return mixed
    public static function getObjectAttribute($object, $attributeName)
    {
        if (!is_object($object)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
        }
        if (!is_string($attributeName)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
        }
        try {
            $attribute = new ReflectionProperty($object, $attributeName);
        } catch (ReflectionException $e) {
            $reflector = new ReflectionObject($object);
            while ($reflector = $reflector->getParentClass()) {
                try {
                    $attribute = $reflector->getProperty($attributeName);
                    break;
                } catch (ReflectionException $e) {
                }
            }
        }
        if (isset($attribute)) {
            if ($attribute == NULL || $attribute->isPublic()) {
                return $object->{$attributeName};
            } else {
                $array = (array) $object;
                $protectedName = "*" . $attributeName;
                if (array_key_exists($protectedName, $array)) {
                    return $array[$protectedName];
                } else {
                    $classes = self::getHierarchy(get_class($object));
                    foreach ($classes as $class) {
                        $privateName = sprintf("%s%s", $class, $attributeName);
                        if (array_key_exists($privateName, $array)) {
                            return $array[$privateName];
                        }
                    }
                }
            }
        }
        throw new PHPUnit_Framework_Exception(sprintf('Attribute "%s" not found in object.', $attributeName));
    }

Usage Example

 /**
  * @dataProvider handlerProvider
  */
 public function testManufactureHandlers($types, $options)
 {
     $handler = HandlerFactory::createHandler($types, $options);
     if (count($types) > 1) {
         $handlerclass = $this->handlers['Composite'];
         $h = \PHPUnit_Util_Class::getObjectAttribute($handler, 'drivers');
         $handlers = array_combine($types, $h);
     } else {
         $handlerclass = $this->handlers[$types[0]];
         $handlers = array($types[0] => $handler);
     }
     $this->assertInstanceOf($handlerclass, $handler);
     foreach ($handlers as $subtype => $subhandler) {
         $subhandlerclass = $this->handlers[$subtype];
         $this->assertInstanceOf($subhandlerclass, $subhandler);
     }
     foreach ($types as $type) {
         $defaults = isset($this->defaultSettings[$type]) ? $this->defaultSettings[$type] : array();
         $options = array_merge($defaults, $options);
         /*            foreach($options as $optname => $optvalue) {
                         $this->assertAttributeEquals($optvalue, $optname, $handlers[$type]);
                     }
         */
     }
 }
All Usage Examples Of PHPUnit_Util_Class::getObjectAttribute