BUG Fixed handing of false values in GridState_Data

API Added ability to unset values
This commit is contained in:
Damian Mooyman 2014-02-04 10:18:26 +13:00
parent 6df276c843
commit cd213ab488
2 changed files with 14 additions and 10 deletions

View File

@ -153,7 +153,7 @@ class GridState_Data {
* @return mixed The value associated with this key, or the value specified by $default if not set
*/
public function getData($name, $default = null) {
if(empty($this->data[$name])) {
if(!isset($this->data[$name])) {
$this->data[$name] = $default;
} else if(is_array($this->data[$name])) {
$this->data[$name] = new GridState_Data($this->data[$name]);
@ -170,6 +170,10 @@ class GridState_Data {
return isset($this->data[$name]);
}
public function __unset($name) {
unset($this->data[$name]);
}
public function __toString() {
if(!$this->data) {
return "";

View File

@ -124,21 +124,21 @@ class GridFieldTest extends SapphireTest {
$this->assertEquals(10, $obj->State->NoValue);
$this->assertEquals(10, $obj->State->NoValue(20));
//Show that values returned by the assignment function don't
// actually return the value stored
// Test that values can be set, unset, and inspected
$this->assertFalse(isset($obj->State->NotSet));
$obj->State->NotSet = false;
$this->assertTrue(isset($obj->State->NotSet));
unset($obj->State->NotSet);
$this->assertFalse(isset($obj->State->NotSet));
// Test that false evaluating values are storable
$this->assertEquals(0, $obj->State->Falsey0(0)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0(10)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
$obj->State->Falsey0 = 0; //manually assign 0
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
//repeat for other falsey values
$this->assertEquals(array(), $obj->State->Falsey1(array()));
$this->assertEquals(array(), $obj->State->Falsey1(array('test')));
$this->assertEquals(array(), $obj->State->Falsey1);
$obj->State->Falsey1 = array(); //manually assign 0
$this->assertEquals(array(), $obj->State->Falsey1); //expect 0 back
// Test that false is storable
$this->assertFalse($obj->State->Falsey2(false));
$this->assertFalse($obj->State->Falsey2(true));
$this->assertFalse($obj->State->Falsey2);