public static function PHPUnit_Util_Class::getObjectAttribute

Returns the value of an object's attribute. This also works for attributes that are declared protected or private.

@since Method available since Release 3.4.0

Parameters

object $object:

string $attributeName:

Return value

mixed

Throws

PHPUnit_Framework_Exception

2 calls to PHPUnit_Util_Class::getObjectAttribute()
PHPUnit_Framework_Assert::readAttribute in drupal/core/vendor/phpunit/phpunit/PHPUnit/Framework/Assert.php
Returns the value of an attribute of a class or an object. This also works for attributes that are declared protected or private.
Util_ClassTest::testGetObjectAttributeCanHandleDynamicVariables in drupal/core/vendor/phpunit/phpunit/Tests/Util/ClassTest.php
Test that if a dynamic variable is defined on a class then the $attribute variable will be NULL, but the variable defined will be a public one so we are safe to return it

File

drupal/core/vendor/phpunit/phpunit/PHPUnit/Util/Class.php, line 296

Class

PHPUnit_Util_Class
Class helpers.

Code

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 || $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));
}