Permalink
Browse files

- Allow testing inherited private attributes. Closes #108.

  • Loading branch information...
sebastianbergmann committed Mar 18, 2007
1 parent e0c3d07 commit 25299f9420239712ac0abeb8c1410dbe35ddf8ac
@@ -1180,8 +1180,6 @@ public static function getStaticAttribute($className, $attributeName)
throw new InvalidArgumentException;
}
- self::assertClassHasStaticAttribute($attributeName, $className);
-
$class = new ReflectionClass($className);
$attributes = $class->getStaticProperties();
@@ -1199,9 +1197,28 @@ public static function getStaticAttribute($className, $attributeName)
return $attributes[$protectedName];
}
- $privateName = sprintf("\0%s\0%s", $className, $attributeName);
+ $classes = PHPUnit_Util_Class::getHierarchy($className);
+
+ foreach ($classes as $class) {
+ $privateName = sprintf(
+ "\0%s\0%s",
+
+ $class,
+ $attributeName
+ );
- return $attributes[$privateName];
+ if (isset($attributes[$privateName])) {
+ return $attributes[$privateName];
+ }
+ }
+
+ throw new RuntimeException(
+ sprintf(
+ 'Attribute "%s" not found in class.',
+
+ $attributeName
+ )
+ );
}
/**
@@ -1224,27 +1241,39 @@ public static function getObjectAttribute($object, $attributeName)
self::assertObjectHasAttribute($attributeName, $object);
- $class = new ReflectionObject($object);
- $attribute = $class->getProperty($attributeName);
-
- if ($attribute->isPublic()) {
+ if (isset($object->$attributeName)) {
return $object->$attributeName;
} else {
- if ($attribute->isProtected()) {
- $attributeName = "\0*\0" . $attributeName;
+ $array = (array) $object;
+ $protectedName = "\0*\0" . $attributeName;
+
+ if (isset($array[$protectedName])) {
+ return $array[$protectedName];
} else {
- $attributeName = sprintf(
- "\0%s\0%s",
+ $classes = PHPUnit_Util_Class::getHierarchy(get_class($object));
- get_class($object),
- $attributeName
- );
- }
+ foreach ($classes as $class) {
+ $privateName = sprintf(
+ "\0%s\0%s",
- $tmp = (array) $object;
+ $class,
+ $attributeName
+ );
- return $tmp[$attributeName];
+ if (isset($array[$privateName])) {
+ return $array[$privateName];
+ }
+ }
+ }
}
+
+ throw new RuntimeException(
+ sprintf(
+ 'Attribute "%s" not found in object.',
+
+ $attributeName
+ )
+ );
}
/**
@@ -1037,13 +1037,15 @@ public function testGetObjectAttribute()
$this->assertEquals('foo', $this->getAttribute($obj, 'publicAttribute'));
$this->assertEquals('bar', $this->getAttribute($obj, 'protectedAttribute'));
$this->assertEquals('baz', $this->getAttribute($obj, 'privateAttribute'));
+ $this->assertEquals('parent', $this->getAttribute($obj, 'privateParentAttribute'));
}
public function testGetStaticAttribute()
{
$this->assertEquals('foo', $this->getAttribute('ClassWithNonPublicAttributes', 'publicStaticAttribute'));
$this->assertEquals('bar', $this->getAttribute('ClassWithNonPublicAttributes', 'protectedStaticAttribute'));
$this->assertEquals('baz', $this->getAttribute('ClassWithNonPublicAttributes', 'privateStaticAttribute'));
+ $this->assertEquals('parent', $this->getAttribute('ClassWithNonPublicAttributes', 'privateStaticParentAttribute'));
}
public function testAssertPublicAttributeContains()
@@ -60,7 +60,13 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class ClassWithNonPublicAttributes
+class ParentClassWithPrivateAttributes
+{
+ private static $privateStaticParentAttribute = 'parent';
+ private $privateParentAttribute = 'parent';
+}
+
+class ClassWithNonPublicAttributes extends ParentClassWithPrivateAttributes
{
public static $publicStaticAttribute = 'foo';
protected static $protectedStaticAttribute = 'bar';
View
@@ -134,5 +134,24 @@ public static function getClassesInFile($filename, $commonPath = '', $clearCache
return isset(self::$fileClassMap[$filename]) ? self::$fileClassMap[$filename] : array();
}
+
+ public static function getHierarchy($className)
+ {
+ $classes = array($className);
+ $done = FALSE;
+
+ while (!$done) {
+ $class = new ReflectionClass($classes[count($classes)-1]);
+ $parent = $class->getParentClass();
+
+ if ($parent !== FALSE) {
+ $classes[] = $parent->getName();
+ } else {
+ $done = TRUE;
+ }
+ }
+
+ return $classes;
+ }
}
?>

0 comments on commit 25299f9

Please sign in to comment.