ENHANCEMENT #3049 Array notation on DataObjectSet is now possible

MINOR Added unit tests for array notation to DataObjectSetTest
MINOR Removed commented out tests from DataObjectSet



git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@95876 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-12-18 03:35:26 +00:00
parent ba2066b8dd
commit f2d264ce3a
2 changed files with 65 additions and 15 deletions

View File

@ -5,7 +5,7 @@
* @package sapphire
* @subpackage model
*/
class DataObjectSet extends ViewableData implements IteratorAggregate, Countable {
class DataObjectSet extends ViewableData implements IteratorAggregate, Countable, ArrayAccess {
/**
* The DataObjects in this set.
* @var array
@ -83,12 +83,45 @@ class DataObjectSet extends ViewableData implements IteratorAggregate, Countable
$this->items[$i] = $item;
}
}
}
parent::__construct();
}
/**
* Necessary for interface ArrayAccess. Returns whether an item with $key exists
* @param mixed $key
* @return bool
*/
public function offsetExists($key) {
return isset($this->items[$key]);
}
/**
* Necessary for interface ArrayAccess. Returns item stored in array with index $key
* @param mixed $key
* @return DataObject
*/
public function offsetGet($key) {
return $this->items[$key];
}
/**
* Necessary for interface ArrayAccess. Set an item with the key in $key
* @param mixed $key
* @param mixed $value
*/
public function offsetSet($key, $value) {
$this->items[$key] = $value;
}
/**
* Necessary for interface ArrayAccess. Unset an item with the key in $key
* @param mixed $key
*/
public function offsetUnset($key) {
unset($this->items[$key]);
}
/**
* Destory all of the DataObjects in this set.
*/
@ -97,7 +130,7 @@ class DataObjectSet extends ViewableData implements IteratorAggregate, Countable
$item->destroy();
}
}
/**
* Removes all the items in this set.
*/

View File

@ -9,6 +9,34 @@ class DataObjectSetTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/DataObjectTest.yml';
function testArrayAccessExists() {
$set = new DataObjectSet(array(
$one = new DataObject(array('Title' => 'one')),
$two = new DataObject(array('Title' => 'two')),
$three = new DataObject(array('Title' => 'three'))
));
$this->assertEquals(count($set), 3);
$this->assertTrue(isset($set[0]), 'First item in the set is set');
$this->assertEquals($one, $set[0], 'First item in the set is accessible by array notation');
}
function testArrayAccessUnset() {
$set = new DataObjectSet(array(
$one = new DataObject(array('Title' => 'one')),
$two = new DataObject(array('Title' => 'two')),
$three = new DataObject(array('Title' => 'three'))
));
unset($set[0]);
$this->assertEquals(count($set), 2);
}
function testArrayAccessSet() {
$set = new DataObjectSet();
$this->assertEquals(0, count($set));
$set['testing!'] = $test = new DataObject(array('Title' => 'I\'m testing!'));
$this->assertEquals($test, $set['testing!'], 'Set item is accessible by the key we set it as');
}
function testIterator() {
$set = new DataObjectSet(array(
$one = new DataObject(array('Title'=>'one')),
@ -112,8 +140,6 @@ class DataObjectSetTest extends SapphireTest {
$comments = DataObject::get('PageComment', '', "\"ID\" ASC");
/* The first object is Joe's comment */
//Disabled due to Postgres not liking the ID values to be dictated
//$this->assertEquals($comments->First()->ID, 1, 'The first object has an ID of "1"');
$this->assertEquals($comments->First()->Name, 'Joe', 'The first object has a Name field value of "Joe"');
}
@ -124,8 +150,6 @@ class DataObjectSetTest extends SapphireTest {
$comments = DataObject::get('PageComment', '', "\"ID\" ASC");
/* The last object is Dean's comment */
//Disabled due to Postgres not liking the ID values to be dictated
//$this->assertEquals($comments->Last()->ID, 8, 'The last object has an ID of "8"');
$this->assertEquals($comments->Last()->Name, 'Dean', 'The last object has a Name field value of "Dean"');
}
@ -154,19 +178,12 @@ class DataObjectSetTest extends SapphireTest {
$this->assertEquals(count($map), 9, 'There are 9 items in the map. 8 are records. 1 is the empty value');
/* We have the same map as our expected map, asserted above */
//Disabled due to Postgres not liking the ID values to be dictated
//$this->assertSame($expectedMap, $map, 'The map we generated is exactly the same as the asserted one');
/* toDropDownMap() is an alias of map() - let's make a map from that */
$map2 = $comments->toDropDownMap('ID', 'Title', '(Select one)');
/* There are 9 items in the map. 8 are records. 1 is the empty value */
$this->assertEquals(count($map), 9, 'There are 9 items in the map. 8 are records. 1 is the empty value.');
/* We have the same map as our expected map, asserted above */
//Disabled due to Postgres not liking the ID values to be dictated
//We could possibly fix this problem by changing 'assertSame' to not check the keys
//$this->assertSame($expectedMap, $map2, 'The map we generated is exactly the same as the asserted one');
}
}