From 6676787176d43d2f149ff46f364763981aa6204b Mon Sep 17 00:00:00 2001 From: Simon Erkelens Date: Sun, 16 Jun 2013 15:02:03 +0300 Subject: [PATCH] ENHANCEMENT: Allow nested grouping of GroupedList --- model/GroupedList.php | 1 + tests/model/GroupedListTest.php | 74 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/model/GroupedList.php b/model/GroupedList.php index 9e9a693bb..50fe7723a 100644 --- a/model/GroupedList.php +++ b/model/GroupedList.php @@ -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 diff --git a/tests/model/GroupedListTest.php b/tests/model/GroupedListTest.php index 7bdad0949..1c83d90b8 100644 --- a/tests/model/GroupedListTest.php +++ b/tests/model/GroupedListTest.php @@ -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); + } + } + } }