You are here

public static function PHPUnit_Framework_Assert::getObjectAttribute in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpunit/phpunit/src/Framework/Assert.php \PHPUnit_Framework_Assert::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 4.0.0

Parameters

object $object:

string $attributeName:

Return value

mixed

Throws

PHPUnit_Framework_Exception

6 calls to PHPUnit_Framework_Assert::getObjectAttribute()
Framework_AssertTest::testGetObjectAttributeRaisesExceptionForInvalidFirstArgument in vendor/phpunit/phpunit/tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::getObjectAttribute @expectedException PHPUnit_Framework_Exception
Framework_AssertTest::testGetObjectAttributeRaisesExceptionForInvalidSecondArgument in vendor/phpunit/phpunit/tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::getObjectAttribute @expectedException PHPUnit_Framework_Exception
Framework_AssertTest::testGetObjectAttributeRaisesExceptionForInvalidSecondArgument2 in vendor/phpunit/phpunit/tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::getObjectAttribute @expectedException PHPUnit_Framework_Exception
Framework_AssertTest::testGetObjectAttributeRaisesExceptionForInvalidSecondArgument3 in vendor/phpunit/phpunit/tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::getObjectAttribute @expectedException PHPUnit_Framework_Exception
Framework_AssertTest::testGetObjectAttributeWorksForInheritedAttributes in vendor/phpunit/phpunit/tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::getObjectAttribute

... See full list

File

vendor/phpunit/phpunit/src/Framework/Assert.php, line 2929

Class

PHPUnit_Framework_Assert
A set of assert methods.

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