ENHANCEMENT: Allow nested grouping of GroupedList

This commit is contained in:
Simon Erkelens 2013-06-16 15:02:03 +03:00 committed by Simon Welsh
parent dbb6ba82f8
commit 6676787176
2 changed files with 75 additions and 0 deletions

View File

@ -43,6 +43,7 @@ class GroupedList extends SS_ListDecorator {
$result = new ArrayList();
foreach ($grouped as $indVal => $list) {
$list = GroupedList::create($list);
$result->push(new ArrayData(array(
$index => $indVal,
$children => $list

View File

@ -48,5 +48,79 @@ class GroupedListTest extends SapphireTest {
$this->assertEquals(2, count($last->Children));
$this->assertEquals('CCC', $last->Name);
}
public function testGroupedByChildren(){
$list = GroupedList::create(
ArrayList::create(
array(
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '111',
)),
ArrayData::create(array(
'Name' => 'BBB',
'Number' => '111',
)),
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '222',
)),
ArrayData::create(array(
'Name' => 'BBB',
'Number' => '111',
)),
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '111',
)),
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '333',
)),
ArrayData::create(array(
'Name' => 'BBB',
'Number' => '222',
)),
ArrayData::create(array(
'Name' => 'BBB',
'Number' => '333',
)),
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '111',
)),
ArrayData::create(array(
'Name' => 'AAA',
'Number' => '333',
))
)
)
);
$grouped = $list->GroupedBy('Name');
foreach($grouped as $group){
$children = $group->Children;
$childGroups = $children->GroupedBy('Number');
$this->assertEquals(3, count($childGroups));
$first = $childGroups->first();
$last = $childGroups->last();
if($group->Name == 'AAA'){
$this->assertEquals(3, count($first->Children));
$this->assertEquals('111', $first->Number);
$this->assertEquals(2, count($last->Children));
$this->assertEquals('333', $last->Number);
}
if($group->Name == 'BBB'){
$this->assertEquals(2, count($first->Children));
$this->assertEquals('111', $first->Number);
$this->assertEquals(1, count($last->Children));
$this->assertEquals('333', $last->Number);
}
}
}
}