BUGFIX #4929: Fixed Object::add_static_vars() for uninherited static.s (from r97586)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102510 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-04-12 22:00:25 +00:00
parent 6994e715ca
commit 69543bb575
2 changed files with 31 additions and 4 deletions

View File

@ -280,6 +280,19 @@ abstract class Object {
if($parentProp == $classProp) $classProp = null;
}
// Add data from extra_statics if it has been applied to this specific class (it
// wouldn't make sense to have them inherit in this method). This is kept separate
// from the equivalent get_static code because it's so much simpler
if(isset(self::$extra_statics[$class][$name])) {
$toMerge = self::$extra_statics[$class][$name];
if(is_array($toMerge) && is_array($classProp)) {
$classProp = array_merge($toMerge, $classProp);
} elseif(!$classProp) {
$classProp = $toMerge;
}
}
self::$cached_uninherited_statics[$class][$name] = true;
self::$uninherited_statics[$class][$name] = $classProp;
}
@ -355,8 +368,11 @@ abstract class Object {
if ($replace) {
self::set_static($class, $name, $value);
self::$replaced_statics[$class][$name] = true;
// Clear caches
} else {
self::$cached_statics[$class][$name] = null;
self::$cached_uninherited_statics[$class][$name] = null;
}
}

View File

@ -70,14 +70,25 @@ class ObjectStaticTest extends SapphireTest {
}
/**
* Confirms that Object::add_static_var() doesn't work for uninherited stats
* Checks that Object::add_static_var() also works for uninherited stats
*/
public function testAddStaticVarDoesntWorkFor() {
public function testAddStaticVarWorksForUninheritedStatics() {
Object::add_static_var('ObjectStaticTest_First', 'first', array('test_1b'));
Object::add_static_var('ObjectStaticTest_Second', 'first', array('test_2b'));
$this->assertNotContains('test_1b', Object::uninherited_static('ObjectStaticTest_First', 'first'));
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Second', 'first'));
// Check that it can be applied to parent and subclasses, and queried directly
$this->assertContains('test_1b', Object::uninherited_static('ObjectStaticTest_First', 'first'));
$this->assertContains('test_2b', Object::uninherited_static('ObjectStaticTest_Second', 'first'));
// But it won't affect subclasses - this is *uninherited* static
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Third', 'first'));
$this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
// Subclasses that don't have the static explicitly defined should allow definition, also
// This also checks that add_static_var can be called after the first uninherited_static()
// call (which can be buggy due to caching)
Object::add_static_var('ObjectStaticTest_Fourth', 'first', array('test_4b'));
$this->assertContains('test_4b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
}
}