PHPUnit_Framework_Assert::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');
        }
        if (!preg_match('/[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*/', $attributeName)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
        }
        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 || $attribute->isPublic()) {
                return $object->{$attributeName};
            }
            $attribute->setAccessible(true);
            $value = $attribute->getValue($object);
            $attribute->setAccessible(false);
            return $value;
        }
        throw new PHPUnit_Framework_Exception(sprintf('Attribute "%s" not found in object.', $attributeName));
    }

Usage Example

Esempio n. 1
0
 public function testAddExtension()
 {
     $factory = new Factory(M::mock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface'));
     $extension = M::mock('Knp\\Menu\\Factory\\ExtensionInterface');
     $factory->addExtension($extension);
     $this->assertCount(3, \PHPUnit_Framework_Assert::getObjectAttribute($factory, 'extensions'));
 }
All Usage Examples Of PHPUnit_Framework_Assert::getObjectAttribute
PHPUnit_Framework_Assert