2011-05-03 16:06:42 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Filterable;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2016-10-14 03:30:05 +02:00
|
|
|
use stdClass;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
class ArrayListTest extends SapphireTest {
|
|
|
|
|
2014-01-12 06:55:40 +01:00
|
|
|
public function testPushOperator() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Num' => 1)
|
|
|
|
));
|
|
|
|
|
|
|
|
$list[] = array('Num' => 2);
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 2), $list->last());
|
|
|
|
|
|
|
|
$list[] = array('Num' => 3);
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 3), $list->last());
|
|
|
|
}
|
|
|
|
|
2011-05-05 16:52:50 +02:00
|
|
|
public function testArrayAccessExists() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
$one = new DataObject(array('Title' => 'one')),
|
|
|
|
$two = new DataObject(array('Title' => 'two')),
|
|
|
|
$three = new DataObject(array('Title' => 'three'))
|
|
|
|
));
|
|
|
|
$this->assertEquals(count($list), 3);
|
|
|
|
$this->assertTrue(isset($list[0]), 'First item in the set is set');
|
|
|
|
$this->assertEquals($one, $list[0], 'First item in the set is accessible by array notation');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayAccessUnset() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
$one = new DataObject(array('Title' => 'one')),
|
|
|
|
$two = new DataObject(array('Title' => 'two')),
|
|
|
|
$three = new DataObject(array('Title' => 'three'))
|
|
|
|
));
|
|
|
|
unset($list[0]);
|
|
|
|
$this->assertEquals(count($list), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayAccessSet() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
$this->assertEquals(0, count($list));
|
|
|
|
$list['testing!'] = $test = new DataObject(array('Title' => 'I\'m testing!'));
|
|
|
|
$this->assertEquals($test, $list['testing!'], 'Set item is accessible by the key we set it as');
|
|
|
|
}
|
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
public function testCount() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
$this->assertEquals(0, $list->count());
|
|
|
|
$list = new ArrayList(array(1, 2, 3));
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
|
|
|
|
2011-05-05 09:48:12 +02:00
|
|
|
public function testExists() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
$this->assertFalse($list->exists());
|
|
|
|
$list = new ArrayList(array(1, 2, 3));
|
|
|
|
$this->assertTrue($list->exists());
|
|
|
|
}
|
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
public function testToNestedArray() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('First' => 'FirstFirst', 'Second' => 'FirstSecond'),
|
|
|
|
(object) array('First' => 'SecondFirst', 'Second' => 'SecondSecond'),
|
2016-10-14 03:30:05 +02:00
|
|
|
new ArrayListTest\TestObject('ThirdFirst', 'ThirdSecond')
|
2011-05-03 16:06:42 +02:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
$this->assertEquals($list->toNestedArray(), array(
|
|
|
|
array('First' => 'FirstFirst', 'Second' => 'FirstSecond'),
|
|
|
|
array('First' => 'SecondFirst', 'Second' => 'SecondSecond'),
|
|
|
|
array('First' => 'ThirdFirst', 'Second' => 'ThirdSecond')
|
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
public function testEach() {
|
|
|
|
$list = new ArrayList(array(1, 2, 3));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$count = 0;
|
|
|
|
$test = $this;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$list->each(function($item) use (&$count, $test) {
|
|
|
|
$count++;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$test->assertTrue(is_int($item));
|
|
|
|
});
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$this->assertEquals($list->Count(), $count);
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2012-03-09 02:02:37 +01:00
|
|
|
public function testLimit() {
|
2011-05-03 16:06:42 +02:00
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Key' => 1), array('Key' => 2), array('Key' => 3)
|
|
|
|
));
|
2012-03-09 02:02:37 +01:00
|
|
|
$this->assertEquals($list->limit(2,1)->toArray(), array(
|
2011-05-03 16:06:42 +02:00
|
|
|
array('Key' => 2), array('Key' => 3)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddRemove() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Key' => 1), array('Key' => 2)
|
|
|
|
));
|
|
|
|
|
|
|
|
$list->add(array('Key' => 3));
|
|
|
|
$this->assertEquals($list->toArray(), array(
|
|
|
|
array('Key' => 1), array('Key' => 2), array('Key' => 3)
|
|
|
|
));
|
|
|
|
|
|
|
|
$list->remove(array('Key' => 2));
|
|
|
|
$this->assertEquals(array_values($list->toArray()), array(
|
|
|
|
array('Key' => 1), array('Key' => 3)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-05-05 09:48:12 +02:00
|
|
|
public function testReplace() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Key' => 1),
|
|
|
|
$two = (object) array('Key' => 2),
|
|
|
|
(object) array('Key' => 3)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(array('Key' => 1), $list[0]);
|
|
|
|
$list->replace(array('Key' => 1), array('Replaced' => 1));
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array('Replaced' => 1), $list[0]);
|
|
|
|
|
|
|
|
$this->assertEquals($two, $list[1]);
|
|
|
|
$list->replace($two, array('Replaced' => 2));
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array('Replaced' => 2), $list[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMerge() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Num' => 1), array('Num' => 2)
|
|
|
|
));
|
|
|
|
$list->merge(array(
|
|
|
|
array('Num' => 3), array('Num' => 4)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(4, count($list));
|
|
|
|
$this->assertEquals($list->toArray(), array(
|
|
|
|
array('Num' => 1), array('Num' => 2), array('Num' => 3), array('Num' => 4)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-05-05 12:53:07 +02:00
|
|
|
public function testRemoveDuplicates() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('ID' => 1, 'Field' => 1),
|
|
|
|
array('ID' => 2, 'Field' => 2),
|
|
|
|
array('ID' => 3, 'Field' => 3),
|
|
|
|
array('ID' => 4, 'Field' => 1),
|
|
|
|
(object) array('ID' => 5, 'Field' => 2)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(5, count($list));
|
|
|
|
$list->removeDuplicates();
|
|
|
|
$this->assertEquals(5, count($list));
|
|
|
|
|
|
|
|
$list->removeDuplicates('Field');
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array(1, 2, 3), $list->column('Field'));
|
|
|
|
$this->assertEquals(array(1, 2, 3), $list->column('ID'));
|
|
|
|
}
|
|
|
|
|
2011-05-05 09:48:12 +02:00
|
|
|
public function testPushPop() {
|
|
|
|
$list = new ArrayList(array('Num' => 1));
|
|
|
|
$this->assertEquals(1, count($list));
|
|
|
|
|
|
|
|
$list->push(array('Num' => 2));
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 2), $list->last());
|
|
|
|
|
|
|
|
$list->push(array('Num' => 3));
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 3), $list->last());
|
|
|
|
|
|
|
|
$this->assertEquals(array('Num' => 3), $list->pop());
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 2), $list->last());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShiftUnshift() {
|
|
|
|
$list = new ArrayList(array('Num' => 1));
|
|
|
|
$this->assertEquals(1, count($list));
|
|
|
|
|
|
|
|
$list->unshift(array('Num' => 2));
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 2), $list->first());
|
|
|
|
|
|
|
|
$list->unshift(array('Num' => 3));
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 3), $list->first());
|
|
|
|
|
|
|
|
$this->assertEquals(array('Num' => 3), $list->shift());
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
$this->assertEquals(array('Num' => 2), $list->first());
|
|
|
|
}
|
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
public function testFirstLast() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Key' => 1), array('Key' => 2), array('Key' => 3)
|
|
|
|
));
|
|
|
|
$this->assertEquals($list->first(), array('Key' => 1));
|
|
|
|
$this->assertEquals($list->last(), array('Key' => 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMap() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('ID' => 1, 'Name' => 'Steve',),
|
|
|
|
(object) array('ID' => 3, 'Name' => 'Bob'),
|
|
|
|
array('ID' => 5, 'Name' => 'John')
|
|
|
|
));
|
2016-01-21 21:29:53 +01:00
|
|
|
$map = $list->map('ID', 'Name');
|
|
|
|
// Items added after calling map should not be included retroactively
|
|
|
|
$list->add(array('ID' => 7, 'Name' => 'Andrew'));
|
2016-09-09 08:43:05 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Map', $map);
|
2016-01-21 21:29:53 +01:00
|
|
|
$this->assertEquals(array(
|
2011-05-03 16:06:42 +02:00
|
|
|
1 => 'Steve',
|
|
|
|
3 => 'Bob',
|
|
|
|
5 => 'John'
|
2016-01-21 21:29:53 +01:00
|
|
|
), $map->toArray());
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFind() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
|
|
|
$this->assertEquals($list->find('Name', 'Bob'), (object) array(
|
|
|
|
'Name' => 'Bob'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testColumn() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
|
|
|
$this->assertEquals($list->column('Name'), array(
|
|
|
|
'Steve', 'Bob', 'John'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
public function testSortSimpleDefaultIsSortedASC() {
|
2011-05-03 16:06:42 +02:00
|
|
|
$list = new ArrayList(array(
|
2016-11-09 23:54:33 +01:00
|
|
|
array('Name' => 'Steve'),
|
2011-05-03 16:06:42 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'bonny'),
|
2011-05-03 16:06:42 +02:00
|
|
|
));
|
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Unquoted name
|
|
|
|
$list1 = $list->sort('Name');
|
2016-08-22 12:21:50 +02:00
|
|
|
$this->assertEquals(array(
|
2014-05-15 04:25:23 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'bonny'),
|
2014-05-15 04:25:23 +02:00
|
|
|
array('Name' => 'John'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
), $list1->toArray());
|
2014-05-15 04:25:23 +02:00
|
|
|
|
|
|
|
// Quoted name name
|
|
|
|
$list2 = $list->sort('"Name"');
|
2016-08-22 12:21:50 +02:00
|
|
|
$this->assertEquals(array(
|
2014-05-15 04:25:23 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'bonny'),
|
2014-05-15 04:25:23 +02:00
|
|
|
array('Name' => 'John'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
), $list2->toArray());
|
2014-05-15 04:25:23 +02:00
|
|
|
|
|
|
|
// Array (non-associative)
|
|
|
|
$list3 = $list->sort(array('"Name"'));
|
2016-08-22 12:21:50 +02:00
|
|
|
$this->assertEquals(array(
|
2011-05-03 16:06:42 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'bonny'),
|
2011-05-03 16:06:42 +02:00
|
|
|
array('Name' => 'John'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
), $list3->toArray());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-12 07:00:17 +02:00
|
|
|
// Quoted name name with table
|
|
|
|
$list4 = $list->sort('"Record"."Name"');
|
2016-08-22 16:30:40 +02:00
|
|
|
$this->assertEquals(array(
|
2016-07-12 07:00:17 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 16:30:40 +02:00
|
|
|
array('Name' => 'bonny'),
|
2016-07-12 07:00:17 +02:00
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
2016-08-22 16:30:40 +02:00
|
|
|
), $list4->toArray());
|
2016-07-12 07:00:17 +02:00
|
|
|
|
|
|
|
// Quoted name name with table (desc)
|
|
|
|
$list5 = $list->sort('"Record"."Name" DESC');
|
2016-08-22 16:30:40 +02:00
|
|
|
$this->assertEquals(array(
|
2016-07-12 07:00:17 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
2016-08-22 16:30:40 +02:00
|
|
|
array('Name' => 'bonny'),
|
2016-07-12 07:00:17 +02:00
|
|
|
(object) array('Name' => 'Bob')
|
2016-08-22 16:30:40 +02:00
|
|
|
), $list5->toArray());
|
2016-07-12 07:00:17 +02:00
|
|
|
|
|
|
|
// Table without quotes
|
|
|
|
$list6 = $list->sort('Record.Name');
|
2016-08-22 16:30:40 +02:00
|
|
|
$this->assertEquals(array(
|
2016-07-12 07:00:17 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 16:30:40 +02:00
|
|
|
array('Name' => 'bonny'),
|
2016-07-12 07:00:17 +02:00
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
2016-08-22 16:30:40 +02:00
|
|
|
), $list6->toArray());
|
2016-07-12 07:00:17 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Check original list isn't altered
|
2016-08-22 12:21:50 +02:00
|
|
|
$this->assertEquals(array(
|
2014-05-15 04:25:23 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'bonny'),
|
|
|
|
), $list->toArray());
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:14:16 +02:00
|
|
|
public function testMixedCaseSort() {
|
|
|
|
// Note: Natural sorting is not expected, so if 'bonny10' were included
|
|
|
|
// below we would expect it to appear between bonny1 and bonny2. That's
|
|
|
|
// undesirable though so we're not enforcing it in tests.
|
|
|
|
$original = array(
|
|
|
|
array('Name' => 'Steve'),
|
2016-08-22 12:21:50 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'bonny'),
|
|
|
|
array('Name' => 'bonny1'),
|
2016-10-04 20:14:16 +02:00
|
|
|
//array('Name' => 'bonny10'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'bonny2'),
|
2016-10-04 20:14:16 +02:00
|
|
|
);
|
2016-08-22 12:21:50 +02:00
|
|
|
|
2016-10-04 20:14:16 +02:00
|
|
|
$list = new ArrayList($original);
|
|
|
|
|
|
|
|
$expected = array(
|
2016-08-22 12:21:50 +02:00
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'bonny'),
|
|
|
|
array('Name' => 'bonny1'),
|
2016-10-04 20:14:16 +02:00
|
|
|
//array('Name' => 'bonny10'),
|
2016-08-22 12:21:50 +02:00
|
|
|
array('Name' => 'bonny2'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve'),
|
2016-10-04 20:14:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Unquoted name
|
|
|
|
$list1 = $list->sort('Name');
|
|
|
|
$this->assertEquals($expected, $list1->toArray());
|
2016-08-22 12:21:50 +02:00
|
|
|
|
|
|
|
// Quoted name name
|
|
|
|
$list2 = $list->sort('"Name"');
|
2016-10-04 20:14:16 +02:00
|
|
|
$this->assertEquals($expected, $list2->toArray());
|
2016-08-22 12:21:50 +02:00
|
|
|
|
|
|
|
// Array (non-associative)
|
|
|
|
$list3 = $list->sort(array('"Name"'));
|
2016-10-04 20:14:16 +02:00
|
|
|
$this->assertEquals($expected, $list3->toArray());
|
2016-08-22 12:21:50 +02:00
|
|
|
|
|
|
|
// Check original list isn't altered
|
2016-10-04 20:14:16 +02:00
|
|
|
$this->assertEquals($original, $list->toArray());
|
2016-08-22 12:21:50 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortSimpleASCOrder() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Sort two arguments
|
|
|
|
$list1 = $list->sort('Name','ASC');
|
|
|
|
$this->assertEquals($list1->toArray(), array(
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Sort single string
|
|
|
|
$list2 = $list->sort('Name asc');
|
|
|
|
$this->assertEquals($list2->toArray(), array(
|
2011-12-08 22:08:46 +01:00
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Sort quoted string
|
|
|
|
$list3 = $list->sort('"Name" ASCENDING');
|
|
|
|
$this->assertEquals($list3->toArray(), array(
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Sort array specifier
|
|
|
|
$list4 = $list->sort(array('Name' => 'ascending'));
|
|
|
|
$this->assertEquals($list4->toArray(), array(
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Steve')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals($list->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortSimpleDESCOrder() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Sort two arguments
|
|
|
|
$list1 = $list->sort('Name', 'DESC');
|
|
|
|
$this->assertEquals($list1->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
(object) array('Name' => 'Bob')
|
|
|
|
));
|
|
|
|
|
|
|
|
// Sort single string
|
|
|
|
$list2 = $list->sort('Name desc');
|
|
|
|
$this->assertEquals($list2->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
(object) array('Name' => 'Bob')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Sort quoted string
|
|
|
|
$list3 = $list->sort('"Name" DESCENDING');
|
|
|
|
$this->assertEquals($list3->toArray(), array(
|
2011-05-03 16:06:42 +02:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
(object) array('Name' => 'Bob')
|
|
|
|
));
|
2014-05-15 04:25:23 +02:00
|
|
|
|
|
|
|
// Sort array specifier
|
|
|
|
$list4 = $list->sort(array('Name' => 'descending'));
|
|
|
|
$this->assertEquals($list4->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
(object) array('Name' => 'Bob')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals($list->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-04 20:14:16 +02:00
|
|
|
public function testSortNumeric() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Sort' => 0),
|
|
|
|
array('Sort' => -1),
|
|
|
|
array('Sort' => 1),
|
|
|
|
array('Sort' => -2),
|
|
|
|
array('Sort' => 2),
|
|
|
|
array('Sort' => -10),
|
|
|
|
array('Sort' => 10)
|
|
|
|
));
|
|
|
|
|
|
|
|
// Sort descending
|
|
|
|
$list1 = $list->sort('Sort', 'DESC');
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array('Sort' => 10),
|
|
|
|
array('Sort' => 2),
|
|
|
|
array('Sort' => 1),
|
|
|
|
array('Sort' => 0),
|
|
|
|
array('Sort' => -1),
|
|
|
|
array('Sort' => -2),
|
|
|
|
array('Sort' => -10)
|
|
|
|
), $list1->toArray());
|
|
|
|
|
|
|
|
// Sort ascending
|
|
|
|
$list1 = $list->sort('Sort', 'ASC');
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array('Sort' => -10),
|
|
|
|
array('Sort' => -2),
|
|
|
|
array('Sort' => -1),
|
|
|
|
array('Sort' => 0),
|
|
|
|
array('Sort' => 1),
|
|
|
|
array('Sort' => 2),
|
|
|
|
array('Sort' => 10)
|
|
|
|
), $list1->toArray());
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
public function testReverse() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'Steve')
|
|
|
|
));
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$list = $list->reverse();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
$this->assertEquals($list->toArray(), array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John'),
|
|
|
|
array('Name' => 'Bob')
|
|
|
|
));
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleMultiSort() {
|
2011-05-05 16:24:33 +02:00
|
|
|
$list = new ArrayList(array(
|
|
|
|
(object) array('Name'=>'Object1', 'F1'=>1, 'F2'=>2, 'F3'=>3),
|
|
|
|
(object) array('Name'=>'Object2', 'F1'=>2, 'F2'=>1, 'F3'=>4),
|
|
|
|
(object) array('Name'=>'Object3', 'F1'=>5, 'F2'=>2, 'F3'=>2),
|
|
|
|
));
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->sort('F3', 'ASC');
|
2011-05-05 16:24:33 +02:00
|
|
|
$this->assertEquals($list->first()->Name, 'Object3', 'Object3 should be first in the list');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals($list->last()->Name, 'Object2', 'Object2 should be last in the list');
|
2011-05-05 16:24:33 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->sort('F3', 'DESC');
|
2011-05-05 16:24:33 +02:00
|
|
|
$this->assertEquals($list->first()->Name, 'Object2', 'Object2 should be first in the list');
|
|
|
|
$this->assertEquals($list->last()->Name, 'Object3', 'Object3 should be last in the list');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testMultiSort() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
(object) array('ID'=>3, 'Name'=>'Bert', 'Importance'=>1),
|
|
|
|
(object) array('ID'=>1, 'Name'=>'Aron', 'Importance'=>2),
|
|
|
|
(object) array('ID'=>2, 'Name'=>'Aron', 'Importance'=>1),
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->sort(array('Name'=>'ASC', 'Importance'=>'ASC'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals($list->first()->ID, 2, 'Aron.2 should be first in the list');
|
|
|
|
$this->assertEquals($list->last()->ID, 3, 'Bert.3 should be last in the list');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->sort(array('Name'=>'ASC', 'Importance'=>'DESC'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals($list->first()->ID, 1, 'Aron.2 should be first in the list');
|
|
|
|
$this->assertEquals($list->last()->ID, 3, 'Bert.3 should be last in the list');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-26 11:29:37 +02:00
|
|
|
/**
|
|
|
|
* Check that we don't cause recursion errors with array_multisort() and circular dependencies
|
|
|
|
*/
|
|
|
|
public function testSortWithCircularDependencies() {
|
|
|
|
$itemA = new stdClass;
|
|
|
|
$childA = new stdClass;
|
|
|
|
$itemA->child = $childA;
|
|
|
|
$childA->parent = $itemA;
|
|
|
|
$itemA->Sort = 1;
|
|
|
|
|
|
|
|
$itemB = new stdClass;
|
|
|
|
$childB = new stdClass;
|
|
|
|
$itemB->child = $childB;
|
|
|
|
$childB->parent = $itemB;
|
|
|
|
$itemB->Sort = 1;
|
|
|
|
|
|
|
|
$items = new ArrayList;
|
|
|
|
$items->add($itemA);
|
|
|
|
$items->add($itemB);
|
|
|
|
|
|
|
|
// This call will trigger a fatal error if there are issues with circular dependencies
|
|
|
|
$items->sort('Sort');
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilter() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter('Name','Bob');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(array((object)array('Name'=>'Bob')), $list->toArray(), 'List should only contain Bob');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter('Name', array('Steve', 'John'); // Steve and John in list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultiple() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
(object) array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
);
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter('Name',array('Steve','John'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and John');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter('Name', array('Steve', 'John'); // negative version
|
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultipleNoMatch() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1),
|
|
|
|
(object) array('Name' => 'Steve', 'ID' => 2),
|
|
|
|
array('Name' => 'John', 'ID' => 2)
|
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter(array('Name'=>'Clair'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(array(), $list->toArray(), 'List should be empty');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>'bob, 'Age'=>21)); // bob with the Age 21 in list
|
|
|
|
*/
|
|
|
|
public function testMultipleFilter() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1),
|
|
|
|
(object) array('Name' => 'Steve', 'ID' => 2),
|
|
|
|
array('Name' => 'John', 'ID' => 2)
|
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter(array('Name'=>'Steve', 'ID'=>2));
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(array((object)array('Name'=>'Steve', 'ID'=>2)), $list->toArray(),
|
|
|
|
'List should only contain object Steve');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>'bob, 'Age'=>21)); // negative version
|
|
|
|
*/
|
|
|
|
public function testMultipleFilterNoMatch() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1),
|
|
|
|
(object) array('Name' => 'Steve', 'ID' => 2),
|
|
|
|
array('Name' => 'John', 'ID' => 2)
|
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter(array('Name'=>'Steve', 'ID'=>4));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(array(), $list->toArray(), 'List should be empty');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>'Steve', 'Age'=>array(21, 43))); // Steve with the Age 21 or 43
|
|
|
|
*/
|
|
|
|
public function testMultipleWithArrayFilter() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1, 'Age'=>21),
|
|
|
|
array('Name' => 'Steve', 'ID' => 2, 'Age'=>18),
|
|
|
|
array('Name' => 'Clair', 'ID' => 2, 'Age'=>21),
|
|
|
|
array('Name' => 'Steve', 'ID' => 3, 'Age'=>43)
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter(array('Name'=>'Steve','Age'=>array(21, 43)));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1, 'Age'=>21),
|
|
|
|
array('Name' => 'Steve', 'ID' => 3, 'Age'=>43)
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and Steve');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>array('aziz','bob'), 'Age'=>array(21, 43)));
|
|
|
|
*/
|
|
|
|
public function testMultipleWithArrayFilterAdvanced() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1, 'Age'=>21),
|
|
|
|
array('Name' => 'Steve', 'ID' => 2, 'Age'=>18),
|
|
|
|
array('Name' => 'Clair', 'ID' => 2, 'Age'=>21),
|
|
|
|
array('Name' => 'Clair', 'ID' => 2, 'Age'=>52),
|
|
|
|
array('Name' => 'Steve', 'ID' => 3, 'Age'=>43)
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->filter(array('Name'=>array('Steve','Clair'),'Age'=>array(21, 43)));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1, 'Age'=>21),
|
|
|
|
array('Name' => 'Clair', 'ID' => 2, 'Age'=>21),
|
|
|
|
array('Name' => 'Steve', 'ID' => 3, 'Age'=>43)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and Steve and Clair');
|
|
|
|
}
|
2013-10-30 14:08:55 +01:00
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
public function testFilterAny() {
|
|
|
|
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
$steve = array('Name' => 'Steve', 'ID' => 1, 'Age' => 21),
|
|
|
|
$bob = array('Name' => 'Bob', 'ID' => 2, 'Age' => 18),
|
|
|
|
$clair = array('Name' => 'Clair', 'ID' => 3, 'Age' => 21),
|
|
|
|
$phil = array('Name' => 'Phil', 'ID' => 4, 'Age' => 21),
|
|
|
|
$oscar = array('Name' => 'Oscar', 'ID' => 5, 'Age' => 52),
|
|
|
|
$mike = array('Name' => 'Mike', 'ID' => 6, 'Age' => 43),
|
|
|
|
));
|
|
|
|
|
|
|
|
// only bob in the list
|
|
|
|
//$list = $list->filterAny('Name', 'bob');
|
|
|
|
$filteredList = $list->filterAny('Name', 'Bob')->toArray();
|
|
|
|
$this->assertCount(1, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
|
|
|
|
// azis or bob in the list
|
|
|
|
//$list = $list->filterAny('Name', array('aziz', 'bob');
|
|
|
|
$filteredList = $list->filterAny('Name', array('Aziz', 'Bob'))->toArray();
|
|
|
|
$this->assertCount(1, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
|
|
|
|
$filteredList = $list->filterAny('Name', array('Steve', 'Bob'))->toArray();
|
|
|
|
$this->assertCount(2, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
|
|
|
|
// bob or anyone aged 21 in the list
|
|
|
|
//$list = $list->filterAny(array('Name'=>'bob, 'Age'=>21));
|
|
|
|
$filteredList = $list->filterAny(array('Name' => 'Bob', 'Age' => 21))->toArray();
|
|
|
|
$this->assertCount(4, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
|
|
|
|
// bob or anyone aged 21 or 43 in the list
|
|
|
|
// $list = $list->filterAny(array('Name'=>'bob, 'Age'=>array(21, 43)));
|
|
|
|
$filteredList = $list->filterAny(array('Name' => 'Bob', 'Age' => array(21, 43)))->toArray();
|
|
|
|
$this->assertCount(5, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($mike, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
|
|
|
|
// all bobs, phils or anyone aged 21 or 43 in the list
|
|
|
|
//$list = $list->filterAny(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
$filteredList = $list->filterAny(array('Name' => array('Bob', 'Phil'), 'Age' => array(21, 43)))->toArray();
|
|
|
|
$this->assertCount(5, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($mike, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
|
|
|
|
$filteredList = $list->filterAny(array('Name' => array('Bob', 'Nobody'), 'Age' => array(21, 43)))->toArray();
|
|
|
|
$this->assertCount(5, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($mike, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
}
|
|
|
|
|
2013-10-30 14:08:55 +01:00
|
|
|
/**
|
|
|
|
* $list = $list->filterByCallback(function($item, $list) { return $item->Age == 21; })
|
|
|
|
*/
|
|
|
|
public function testFilterByCallback() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve', 'ID' => 1, 'Age' => 21),
|
|
|
|
array('Name' => 'Bob', 'ID' => 2, 'Age' => 18),
|
|
|
|
array('Name' => 'Clair', 'ID' => 2, 'Age' => 21),
|
|
|
|
array('Name' => 'Oscar', 'ID' => 2, 'Age' => 52),
|
|
|
|
array('Name' => 'Mike', 'ID' => 3, 'Age' => 43)
|
|
|
|
));
|
|
|
|
|
|
|
|
$list = $list->filterByCallback(function ($item, $list) {
|
|
|
|
return $item->Age == 21;
|
|
|
|
});
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
new ArrayData(array('Name' => 'Steve', 'ID' => 1, 'Age' => 21)),
|
|
|
|
new ArrayData(array('Name' => 'Clair', 'ID' => 2, 'Age' => 21)),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and Clair');
|
2016-09-09 08:43:05 +02:00
|
|
|
$this->assertTrue($list instanceof Filterable, 'The List should be of type SS_Filterable');
|
2013-10-30 14:08:55 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
*/
|
|
|
|
public function testSimpleExclude() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude('Name', 'Bob');
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'John')
|
2011-12-08 22:08:46 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should not contain Bob');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude('Name', 'bob'); // No exclusion version
|
|
|
|
*/
|
|
|
|
public function testSimpleExcludeNoMatch() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude('Name', 'Clair');
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should be unchanged');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude('Name', array('Steve','John'));
|
|
|
|
*/
|
|
|
|
public function testSimpleExcludeWithArray() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude('Name', array('Steve','John'));
|
2012-11-12 04:25:55 +01:00
|
|
|
$expected = array(array('Name' => 'Bob'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Bob');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude all Bob that has Age 21
|
|
|
|
*/
|
|
|
|
public function testExcludeWithTwoArrays() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'Bob' , 'Age' => 21),
|
|
|
|
array('Name' => 'Bob' , 'Age' => 32),
|
|
|
|
array('Name' => 'John', 'Age' => 21)
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude(array('Name' => 'Bob', 'Age' => 21));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'Bob', 'Age' => 32),
|
|
|
|
array('Name' => 'John', 'Age' => 21)
|
2011-12-08 22:08:46 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain John and Bob');
|
|
|
|
}
|
2011-05-05 16:24:33 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(10, 16)));
|
|
|
|
*/
|
|
|
|
public function testMultipleExclude() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'bob', 'Age' => 10),
|
|
|
|
array('Name' => 'phil', 'Age' => 11),
|
|
|
|
array('Name' => 'bob', 'Age' => 12),
|
|
|
|
array('Name' => 'phil', 'Age' => 12),
|
|
|
|
array('Name' => 'bob', 'Age' => 14),
|
|
|
|
array('Name' => 'phil', 'Age' => 14),
|
|
|
|
array('Name' => 'bob', 'Age' => 16),
|
|
|
|
array('Name' => 'phil', 'Age' => 16)
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude(array('Name'=>array('bob','phil'),'Age'=>array(10, 16)));
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'phil', 'Age' => 11),
|
|
|
|
array('Name' => 'bob', 'Age' => 12),
|
|
|
|
array('Name' => 'phil', 'Age' => 12),
|
|
|
|
array('Name' => 'bob', 'Age' => 14),
|
|
|
|
array('Name' => 'phil', 'Age' => 14),
|
2011-12-08 22:08:46 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $list->toArray());
|
2011-05-05 16:24:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(10, 16), 'Bananas'=>true));
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeNoMatch() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'bob', 'Age' => 10),
|
|
|
|
array('Name' => 'phil', 'Age' => 11),
|
|
|
|
array('Name' => 'bob', 'Age' => 12),
|
|
|
|
array('Name' => 'phil', 'Age' => 12),
|
|
|
|
array('Name' => 'bob', 'Age' => 14),
|
|
|
|
array('Name' => 'phil', 'Age' => 14),
|
|
|
|
array('Name' => 'bob', 'Age' => 16),
|
|
|
|
array('Name' => 'phil', 'Age' => 16)
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
2011-05-05 16:24:33 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude(array('Name'=>array('bob','phil'),'Age'=>array(10, 16),'Bananas'=>true));
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'bob', 'Age' => 10),
|
|
|
|
array('Name' => 'phil', 'Age' => 11),
|
|
|
|
array('Name' => 'bob', 'Age' => 12),
|
|
|
|
array('Name' => 'phil', 'Age' => 12),
|
|
|
|
array('Name' => 'bob', 'Age' => 14),
|
|
|
|
array('Name' => 'phil', 'Age' => 14),
|
|
|
|
array('Name' => 'bob', 'Age' => 16),
|
|
|
|
array('Name' => 'phil', 'Age' => 16)
|
2011-12-08 22:08:46 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(10, 16), 'HasBananas'=>true));
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeThreeArguments() {
|
|
|
|
$list = new ArrayList(array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'bob', 'Age' => 10, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'phil','Age' => 11, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'bob', 'Age' => 12, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'phil','Age' => 12, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'bob', 'Age' => 14, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'ann', 'Age' => 14, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'phil','Age' => 14, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'bob', 'Age' => 16, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'phil','Age' => 16, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'clair','Age' => 16, 'HasBananas'=>true)
|
2011-12-08 22:08:46 +01:00
|
|
|
));
|
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->exclude(array('Name'=>array('bob','phil'),'Age'=>array(10, 16),'HasBananas'=>true));
|
2011-12-08 22:08:46 +01:00
|
|
|
$expected = array(
|
2012-11-12 04:25:55 +01:00
|
|
|
array('Name' => 'bob', 'Age' => 10, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'phil','Age' => 11, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'bob', 'Age' => 12, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'phil','Age' => 12, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'bob', 'Age' => 14, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'ann', 'Age' => 14, 'HasBananas'=>true),
|
|
|
|
array('Name' => 'phil','Age' => 14, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'bob', 'Age' => 16, 'HasBananas'=>false),
|
|
|
|
array('Name' => 'clair','Age' => 16, 'HasBananas'=>true)
|
2011-12-08 22:08:46 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
2013-08-05 05:43:35 +02:00
|
|
|
|
|
|
|
public function testCanFilterBy() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('Name' => 'Steve'),
|
|
|
|
array('Name' => 'Bob'),
|
|
|
|
array('Name' => 'John')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertTrue($list->canFilterBy('Name'));
|
|
|
|
$this->assertFalse($list->canFilterBy('Age'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanFilterByEmpty() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
|
|
|
|
$this->assertFalse($list->canFilterBy('Name'));
|
|
|
|
$this->assertFalse($list->canFilterBy('Age'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByID() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('ID' => 1, 'Name' => 'Steve'),
|
|
|
|
array('ID' => 2, 'Name' => 'Bob'),
|
|
|
|
array('ID' => 3, 'Name' => 'John')
|
|
|
|
));
|
|
|
|
|
|
|
|
$element = $list->byID(1);
|
|
|
|
$this->assertEquals($element['Name'], 'Steve');
|
|
|
|
|
|
|
|
$element = $list->byID(2);
|
|
|
|
$this->assertEquals($element['Name'], 'Bob');
|
|
|
|
|
|
|
|
$element = $list->byID(4);
|
|
|
|
$this->assertNull($element);
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
public function testByIDs() {
|
|
|
|
$list = new ArrayList(array(
|
|
|
|
array('ID' => 1, 'Name' => 'Steve'),
|
|
|
|
array('ID' => 2, 'Name' => 'Bob'),
|
|
|
|
array('ID' => 3, 'Name' => 'John')
|
|
|
|
));
|
|
|
|
$knownIDs = $list->column('ID');
|
|
|
|
$removedID = array_pop($knownIDs);
|
|
|
|
$filteredItems = $list->byIDs($knownIDs);
|
|
|
|
foreach ($filteredItems as $item) {
|
|
|
|
$this->assertContains($item->ID, $knownIDs);
|
|
|
|
$this->assertNotEquals($removedID, $item->ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 05:43:35 +02:00
|
|
|
public function testByIDEmpty() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
|
|
|
|
$element = $list->byID(1);
|
|
|
|
$this->assertNull($element);
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|