2011-12-26 09:48:53 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\GroupedList;
|
2011-12-26 09:48:53 +01:00
|
|
|
/**
|
|
|
|
* Tests for the {@link GroupedList} list decorator.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-26 09:48:53 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class GroupedListTest extends SapphireTest {
|
|
|
|
|
|
|
|
public function testGroupBy() {
|
|
|
|
$list = new GroupedList(new ArrayList(array(
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'CCC'),
|
|
|
|
array('Name' => 'CCC')
|
|
|
|
)));
|
|
|
|
|
|
|
|
$grouped = $list->groupBy('Name');
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($grouped));
|
|
|
|
$this->assertEquals(3, count($grouped['AAA']));
|
|
|
|
$this->assertEquals(3, count($grouped['BBB']));
|
|
|
|
$this->assertEquals(2, count($grouped['CCC']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGroupedBy() {
|
|
|
|
$list = new GroupedList(new ArrayList(array(
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'AAA'),
|
|
|
|
array('Name' => 'BBB'),
|
|
|
|
array('Name' => 'CCC'),
|
|
|
|
array('Name' => 'CCC')
|
|
|
|
)));
|
|
|
|
|
|
|
|
$grouped = $list->GroupedBy('Name');
|
|
|
|
$first = $grouped->first();
|
|
|
|
$last = $grouped->last();
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($first->Children));
|
|
|
|
$this->assertEquals('AAA', $first->Name);
|
|
|
|
$this->assertEquals(2, count($last->Children));
|
|
|
|
$this->assertEquals('CCC', $last->Name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-16 14:02:03 +02:00
|
|
|
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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-16 14:02:03 +02:00
|
|
|
foreach($grouped as $group){
|
|
|
|
$children = $group->Children;
|
|
|
|
$childGroups = $children->GroupedBy('Number');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-16 14:02:03 +02:00
|
|
|
$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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-16 14:02:03 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-26 09:48:53 +01:00
|
|
|
|
2015-11-03 11:56:30 +01:00
|
|
|
public function testTotalItems() {
|
|
|
|
$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',
|
|
|
|
))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(4, $list->TotalItems());
|
|
|
|
}
|
|
|
|
|
2011-12-26 09:48:53 +01:00
|
|
|
}
|