Merge pull request #4870 from kinglozzer/4821-viewabledata-failover

NEW: Add ViewableData::setFailover() to refresh detected methods when changing failover
This commit is contained in:
Damian Mooyman 2016-01-07 10:01:38 +13:00
commit c32017e3f3
2 changed files with 38 additions and 7 deletions

View File

@ -20,7 +20,8 @@ class ViewableDataTest extends SapphireTest {
public function testFailoverRequiresCasting() {
$caster = new ViewableDataTest_Castable();
$container = new ViewableDataTest_Container($caster);
$container = new ViewableDataTest_Container();
$container->setFailover($caster);
$this->assertTrue($container->obj('alwaysCasted') instanceof ViewableDataTest_RequiresCasting);
$this->assertTrue($caster->obj('alwaysCasted', null, false) instanceof ViewableDataTest_RequiresCasting);
@ -175,6 +176,19 @@ class ViewableDataTest extends SapphireTest {
$this->assertEquals('BBB', $objNotCached->obj('Test', null, true, true));
}
public function testSetFailover() {
$failover = new ViewableData();
$container = new ViewableDataTest_Container();
$container->setFailover($failover);
$this->assertSame($failover, $container->getFailover(), 'getFailover() returned a different object');
$this->assertFalse($container->hasMethod('testMethod'), 'testMethod() is already defined when it shouldnt be');
// Ensure that defined methods detected from the failover aren't cached when setting a new failover
$container->setFailover(new ViewableDataTest_Failover);
$this->assertTrue($container->hasMethod('testMethod'));
}
}
/**#@+
@ -252,10 +266,6 @@ class ViewableData_Caster extends ViewableData {
class ViewableDataTest_Container extends ViewableData {
public function __construct($failover) {
$this->failover = $failover;
parent::__construct();
}
}
class ViewableDataTest_CastingClass extends ViewableData {
@ -285,4 +295,6 @@ class ViewableDataTest_NotCached extends ViewableData {
}
}
/**#@-*/
class ViewableDataTest_Failover extends ViewableData {
public function testMethod() {}
}

View File

@ -127,6 +127,25 @@ class ViewableData extends Object implements IteratorAggregate {
$this->setField($property, $value);
}
}
/**
* Set a failover object to attempt to get data from if it is not present on this object.
*
* @param ViewableData $failover
*/
public function setFailover(ViewableData $failover) {
$this->failover = $failover;
$this->defineMethods();
}
/**
* Get the current failover object if set
*
* @return ViewableData|null
*/
public function getFailover() {
return $this->failover;
}
/**
* Check if a field exists on this object. This should be overloaded in child classes.
@ -137,7 +156,7 @@ class ViewableData extends Object implements IteratorAggregate {
public function hasField($field) {
return property_exists($this, $field);
}
/**
* Get the value of a field on this object. This should be overloaded in child classes.
*