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;
|
|
|
|
|
2019-03-19 23:46:35 +01:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
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;
|
2023-08-15 01:31:20 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
2016-10-14 03:30:05 +02:00
|
|
|
use stdClass;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ArrayListTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testPushOperator()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Num' => 1]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list[] = ['Num' => 2];
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 2], $list->last());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list[] = ['Num' => 3];
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 3], $list->last());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayAccessExists()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
$one = new DataObject(['Title' => 'one']),
|
|
|
|
$two = new DataObject(['Title' => 'two']),
|
|
|
|
$three = new DataObject(['Title' => 'three'])
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($list ?? []), 3);
|
2016-12-16 05:34:21 +01:00
|
|
|
$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(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
$one = new DataObject(['Title' => 'one']),
|
|
|
|
$two = new DataObject(['Title' => 'two']),
|
|
|
|
$three = new DataObject(['Title' => 'three'])
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
unset($list[0]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(count($list ?? []), 2);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayAccessSet()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(0, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$list['testing!'] = $test = new DataObject(['Title' => 'I\'m testing!']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($test, $list['testing!'], 'Set item is accessible by the key we set it as');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCount()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
|
|
|
$this->assertEquals(0, $list->count());
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = new ArrayList([1, 2, 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExists()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
|
|
|
$this->assertFalse($list->exists());
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = new ArrayList([1, 2, 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($list->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testToNestedArray()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['First' => 'FirstFirst', 'Second' => 'FirstSecond'],
|
|
|
|
(object) ['First' => 'SecondFirst', 'Second' => 'SecondSecond'],
|
2016-12-16 05:34:21 +01:00
|
|
|
new ArrayListTest\TestObject('ThirdFirst', 'ThirdSecond')
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->toNestedArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['First' => 'FirstFirst', 'Second' => 'FirstSecond'],
|
|
|
|
['First' => 'SecondFirst', 'Second' => 'SecondSecond'],
|
|
|
|
['First' => 'ThirdFirst', 'Second' => 'ThirdSecond']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEach()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = new ArrayList([1, 2, 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$test = $this;
|
|
|
|
|
|
|
|
$list->each(
|
|
|
|
function ($item) use (&$count, $test) {
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
$test->assertTrue(is_int($item));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($list->Count(), $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLimit()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->limit(2, 1)->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
public function testLimitNull()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->limit(null, 0)->toArray(),
|
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAddRemove()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->add(['Key' => 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$list->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->remove(['Key' => 2]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2022-04-14 03:12:59 +02:00
|
|
|
array_values($list->toArray() ?? []),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReplace()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1],
|
|
|
|
$two = (object) ['Key' => 2],
|
|
|
|
(object) ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Key' => 1], $list[0]);
|
|
|
|
$list->replace(['Key' => 1], ['Replaced' => 1]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Replaced' => 1], $list[0]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($two, $list[1]);
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->replace($two, ['Replaced' => 2]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Replaced' => 2], $list[1]);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMerge()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Num' => 1], ['Num' => 2]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list->merge(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Num' => 3], ['Num' => 4]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(4, count($list ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$list->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Num' => 1], ['Num' => 2], ['Num' => 3], ['Num' => 4]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveDuplicates()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['ID' => 1, 'Field' => 1],
|
|
|
|
['ID' => 2, 'Field' => 2],
|
|
|
|
['ID' => 3, 'Field' => 3],
|
|
|
|
['ID' => 4, 'Field' => 1],
|
|
|
|
(object) ['ID' => 5, 'Field' => 2]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(5, count($list ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->removeDuplicates();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(5, count($list ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$list->removeDuplicates('Field');
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([1, 2, 3], $list->column('Field'));
|
|
|
|
$this->assertEquals([1, 2, 3], $list->column('ID'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPushPop()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = new ArrayList(['Num' => 1]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(1, count($list ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->push(['Num' => 2]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 2], $list->last());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->push(['Num' => 3]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 3], $list->last());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 3], $list->pop());
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 2], $list->last());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShiftUnshift()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = new ArrayList(['Num' => 1]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(1, count($list ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->unshift(['Num' => 2]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 2], $list->first());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->unshift(['Num' => 3]);
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 3], $list->first());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 3], $list->shift());
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($list ?? []));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Num' => 2], $list->first());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFirstLast()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals($list->first(), ['Key' => 1]);
|
|
|
|
$this->assertEquals($list->last(), ['Key' => 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMap()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['ID' => 1, 'Name' => 'Steve',],
|
|
|
|
(object) ['ID' => 3, 'Name' => 'Bob'],
|
|
|
|
['ID' => 5, 'Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$map = $list->map('ID', 'Name');
|
|
|
|
// Items added after calling map should not be included retroactively
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->add(['ID' => 7, 'Name' => 'Andrew']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Map', $map);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
1 => 'Steve',
|
|
|
|
3 => 'Bob',
|
|
|
|
5 => 'John'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$map->toArray()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFind()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->find('Name', 'Bob'),
|
2020-04-20 19:58:09 +02:00
|
|
|
(object) [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Bob'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testColumn()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->column('Name'),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Steve', 'Bob', 'John'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortSimpleDefaultIsSortedASC()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Unquoted name
|
|
|
|
$list1 = $list->sort('Name');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list1->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Quoted name name
|
|
|
|
$list2 = $list->sort('"Name"');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list2->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Array (non-associative)
|
2020-04-20 19:58:09 +02:00
|
|
|
$list3 = $list->sort(['"Name"']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list3->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Quoted name name with table
|
|
|
|
$list4 = $list->sort('"Record"."Name"');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list4->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Quoted name name with table (desc)
|
|
|
|
$list5 = $list->sort('"Record"."Name" DESC');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
(object) ['Name' => 'Bob']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list5->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Table without quotes
|
|
|
|
$list6 = $list->sort('Record.Name');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list6->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->toArray()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-04-20 19:58:09 +02:00
|
|
|
$original = [
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'bonny1'],
|
2020-04-20 09:07:53 +02:00
|
|
|
//['Name' => 'bonny10'],
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => 'bonny2'],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$list = new ArrayList($original);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'bonny'],
|
|
|
|
['Name' => 'bonny1'],
|
2020-04-20 09:07:53 +02:00
|
|
|
//['Name' => 'bonny10'],
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => 'bonny2'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
];
|
2016-10-04 20:14:16 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Unquoted name
|
|
|
|
$list1 = $list->sort('Name');
|
|
|
|
$this->assertEquals($expected, $list1->toArray());
|
|
|
|
|
|
|
|
// Quoted name name
|
|
|
|
$list2 = $list->sort('"Name"');
|
|
|
|
$this->assertEquals($expected, $list2->toArray());
|
|
|
|
|
|
|
|
// Array (non-associative)
|
2020-04-20 19:58:09 +02:00
|
|
|
$list3 = $list->sort(['"Name"']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expected, $list3->toArray());
|
|
|
|
|
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals($original, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortSimpleASCOrder()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort two arguments
|
|
|
|
$list1 = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list1->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort single string
|
|
|
|
$list2 = $list->sort('Name asc');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list2->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort quoted string
|
|
|
|
$list3 = $list->sort('"Name" ASCENDING');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list3->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort array specifier
|
2020-04-20 19:58:09 +02:00
|
|
|
$list4 = $list->sort(['Name' => 'ascending']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$list4->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortSimpleDESCOrder()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort two arguments
|
|
|
|
$list1 = $list->sort('Name', 'DESC');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list1->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
(object) ['Name' => 'Bob']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort single string
|
|
|
|
$list2 = $list->sort('Name desc');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list2->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
(object) ['Name' => 'Bob']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort quoted string
|
|
|
|
$list3 = $list->sort('"Name" DESCENDING');
|
|
|
|
$this->assertEquals(
|
|
|
|
$list3->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
(object) ['Name' => 'Bob']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort array specifier
|
2020-04-20 19:58:09 +02:00
|
|
|
$list4 = $list->sort(['Name' => 'descending']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$list4->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
(object) ['Name' => 'Bob']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check original list isn't altered
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortNumeric()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Sort' => 0],
|
|
|
|
['Sort' => -1],
|
|
|
|
['Sort' => 1],
|
|
|
|
['Sort' => -2],
|
|
|
|
['Sort' => 2],
|
|
|
|
['Sort' => -10],
|
|
|
|
['Sort' => 10]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sort descending
|
|
|
|
$list1 = $list->sort('Sort', 'DESC');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Sort' => 10],
|
|
|
|
['Sort' => 2],
|
|
|
|
['Sort' => 1],
|
|
|
|
['Sort' => 0],
|
|
|
|
['Sort' => -1],
|
|
|
|
['Sort' => -2],
|
|
|
|
['Sort' => -10]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list1->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Sort ascending
|
|
|
|
$list1 = $list->sort('Sort', 'ASC');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Sort' => -10],
|
|
|
|
['Sort' => -2],
|
|
|
|
['Sort' => -1],
|
|
|
|
['Sort' => 0],
|
|
|
|
['Sort' => 1],
|
|
|
|
['Sort' => 2],
|
|
|
|
['Sort' => 10]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list1->toArray()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReverse()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'Steve']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$list = $list->reverse();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->toArray(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John'],
|
|
|
|
['Name' => 'Bob']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleMultiSort()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['Name'=>'Object1', 'F1'=>1, 'F2'=>2, 'F3'=>3],
|
|
|
|
(object) ['Name'=>'Object2', 'F1'=>2, 'F2'=>1, 'F3'=>4],
|
|
|
|
(object) ['Name'=>'Object3', 'F1'=>5, 'F2'=>2, 'F3'=>2],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->sort('F3', 'ASC');
|
|
|
|
$this->assertEquals($list->first()->Name, 'Object3', 'Object3 should be first in the list');
|
|
|
|
$this->assertEquals($list->last()->Name, 'Object2', 'Object2 should be last in the list');
|
|
|
|
|
|
|
|
$list = $list->sort('F3', 'DESC');
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultiSort()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
(object) ['ID'=>3, 'Name'=>'Bert', 'Importance'=>1],
|
|
|
|
(object) ['ID'=>1, 'Name'=>'Aron', 'Importance'=>2],
|
|
|
|
(object) ['ID'=>2, 'Name'=>'Aron', 'Importance'=>1],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['Name'=>'ASC', 'Importance'=>'ASC']);
|
2016-12-16 05:34:21 +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');
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['Name'=>'ASC', 'Importance'=>'DESC']);
|
2016-12-16 05:34:21 +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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertTrue(true, 'Sort with circular dependencies does not trigger an error.');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2019-03-19 23:46:35 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilter()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->filter('Name', 'Bob');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([(object)['Name'=>'Bob']], $list->toArray(), 'List should only contain Bob');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter('Name', ['Steve', 'John']; // Steve and John in list
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultiple()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
(object) ['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John']
|
|
|
|
];
|
|
|
|
$list = $list->filter('Name', ['Steve','John']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and John');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter('Name', ['Steve', 'John']; // negative version
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultipleNoMatch()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve', 'ID' => 1],
|
|
|
|
(object) ['Name' => 'Steve', 'ID' => 2],
|
|
|
|
['Name' => 'John', 'ID' => 2]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$list = $list->filter(['Name'=>'Clair']);
|
|
|
|
$this->assertEquals([], $list->toArray(), 'List should be empty');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter(['Name'=>'bob, 'Age'=>21]); // bob with the Age 21 in list
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleFilter()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve', 'ID' => 1],
|
|
|
|
(object) ['Name' => 'Steve', 'ID' => 2],
|
|
|
|
['Name' => 'John', 'ID' => 2]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>'Steve', 'ID'=>2]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[(object)['Name'=>'Steve', 'ID'=>2]],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->toArray(),
|
|
|
|
'List should only contain object Steve'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter(['Name'=>'bob, 'Age'=>21]); // negative version
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleFilterNoMatch()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve', 'ID' => 1],
|
|
|
|
(object) ['Name' => 'Steve', 'ID' => 2],
|
|
|
|
['Name' => 'John', 'ID' => 2]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$list = $list->filter(['Name'=>'Steve', 'ID'=>4]);
|
|
|
|
$this->assertEquals([], $list->toArray(), 'List should be empty');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter(['Name'=>'Steve', 'Age'=>[21, 43]]); // Steve with the Age 21 or 43
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleWithArrayFilter()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve', 'ID' => 1, 'Age'=>21],
|
|
|
|
['Name' => 'Steve', 'ID' => 2, 'Age'=>18],
|
|
|
|
['Name' => 'Clair', 'ID' => 2, 'Age'=>21],
|
|
|
|
['Name' => 'Steve', 'ID' => 3, 'Age'=>43]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>'Steve','Age'=>[21, 43]]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
['Name' => 'Steve', 'ID' => 1, 'Age'=>21],
|
|
|
|
['Name' => 'Steve', 'ID' => 3, 'Age'=>43]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and Steve');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter(['Name'=>['aziz','bob'], 'Age'=>[21, 43]]);
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleWithArrayFilterAdvanced()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve', 'ID' => 1, 'Age'=>21],
|
|
|
|
['Name' => 'Steve', 'ID' => 2, 'Age'=>18],
|
|
|
|
['Name' => 'Clair', 'ID' => 2, 'Age'=>21],
|
|
|
|
['Name' => 'Clair', 'ID' => 2, 'Age'=>52],
|
|
|
|
['Name' => 'Steve', 'ID' => 3, 'Age'=>43]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>['Steve','Clair'],'Age'=>[21, 43]]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
['Name' => 'Steve', 'ID' => 1, 'Age'=>21],
|
|
|
|
['Name' => 'Clair', 'ID' => 2, 'Age'=>21],
|
|
|
|
['Name' => 'Steve', 'ID' => 3, 'Age'=>43]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Steve and Steve and Clair');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAny()
|
|
|
|
{
|
|
|
|
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
$steve = ['Name' => 'Steve', 'ID' => 1, 'Age' => 21],
|
|
|
|
$bob = ['Name' => 'Bob', 'ID' => 2, 'Age' => 18],
|
|
|
|
$clair = ['Name' => 'Clair', 'ID' => 3, 'Age' => 21],
|
|
|
|
$phil = ['Name' => 'Phil', 'ID' => 4, 'Age' => 21],
|
|
|
|
$oscar = ['Name' => 'Oscar', 'ID' => 5, 'Age' => 52],
|
|
|
|
$mike = ['Name' => 'Mike', 'ID' => 6, 'Age' => 43],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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
|
2020-04-20 09:07:53 +02:00
|
|
|
//$list = $list->filterAny('Name', ['aziz', 'bob']);
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny('Name', ['Aziz', 'Bob'])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertCount(1, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny('Name', ['Steve', 'Bob'])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertCount(2, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
|
|
|
|
// bob or anyone aged 21 in the list
|
2020-04-20 09:07:53 +02:00
|
|
|
//$list = $list->filterAny(['Name'=>'bob, 'Age'=>21]);
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny(['Name' => 'Bob', 'Age' => 21])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$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
|
2020-04-20 09:07:53 +02:00
|
|
|
// $list = $list->filterAny(['Name'=>'bob, 'Age'=>[21, 43]]);
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny(['Name' => 'Bob', 'Age' => [21, 43]])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$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
|
2020-04-20 09:07:53 +02:00
|
|
|
//$list = $list->filterAny(['Name'=>['bob','phil'], 'Age'=>[21, 43]]);
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny(['Name' => ['Bob', 'Phil'], 'Age' => [21, 43]])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertCount(5, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($mike, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$filteredList = $list->filterAny(['Name' => ['Bob', 'Nobody'], 'Age' => [21, 43]])->toArray();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertCount(5, $filteredList);
|
|
|
|
$this->assertContains($bob, $filteredList);
|
|
|
|
$this->assertContains($steve, $filteredList);
|
|
|
|
$this->assertContains($clair, $filteredList);
|
|
|
|
$this->assertContains($mike, $filteredList);
|
|
|
|
$this->assertContains($phil, $filteredList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list = $list->filterByCallback(function($item, $list) { return $item->Age == 21; })
|
|
|
|
*/
|
|
|
|
public function testFilterByCallback()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
$steve = ['Name' => 'Steve', 'ID' => 1, 'Age' => 21],
|
|
|
|
['Name' => 'Bob', 'ID' => 2, 'Age' => 18],
|
|
|
|
$clair = ['Name' => 'Clair', 'ID' => 2, 'Age' => 21],
|
|
|
|
['Name' => 'Oscar', 'ID' => 2, 'Age' => 52],
|
|
|
|
['Name' => 'Mike', 'ID' => 3, 'Age' => 43]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->filterByCallback(
|
|
|
|
function ($item, $list) {
|
|
|
|
return $item->Age == 21;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
2017-08-22 01:22:08 +02:00
|
|
|
$this->assertEquals($steve, $list[0]->toMap(), 'List should only contain Steve and Clair');
|
|
|
|
$this->assertEquals($clair, $list[1]->toMap(), 'List should only contain Steve and Clair');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($list instanceof Filterable, 'The List should be of type SS_Filterable');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
*/
|
|
|
|
public function testSimpleExclude()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->exclude('Name', 'Bob');
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'John']
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should not contain Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude('Name', 'bob'); // No exclusion version
|
|
|
|
*/
|
|
|
|
public function testSimpleExcludeNoMatch()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->exclude('Name', 'Clair');
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should be unchanged');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude('Name', array('Steve','John'));
|
|
|
|
*/
|
|
|
|
public function testSimpleExcludeWithArray()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$list = $list->exclude('Name', ['Steve','John']);
|
|
|
|
$expected = [['Name' => 'Bob']];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude all Bob that has Age 21
|
|
|
|
*/
|
|
|
|
public function testExcludeWithTwoArrays()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Bob' , 'Age' => 21],
|
|
|
|
['Name' => 'Bob' , 'Age' => 32],
|
|
|
|
['Name' => 'John', 'Age' => 21]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude(['Name' => 'Bob', 'Age' => 21]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
|
|
|
['Name' => 'Bob', 'Age' => 32],
|
|
|
|
['Name' => 'John', 'Age' => 21]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $list->toArray(), 'List should only contain John and Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(10, 16)));
|
|
|
|
*/
|
|
|
|
public function testMultipleExclude()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'bob', 'Age' => 10],
|
|
|
|
['Name' => 'phil', 'Age' => 11],
|
|
|
|
['Name' => 'bob', 'Age' => 12],
|
|
|
|
['Name' => 'phil', 'Age' => 12],
|
|
|
|
['Name' => 'bob', 'Age' => 14],
|
|
|
|
['Name' => 'phil', 'Age' => 14],
|
|
|
|
['Name' => 'bob', 'Age' => 16],
|
|
|
|
['Name' => 'phil', 'Age' => 16]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->exclude(['Name'=>['bob','phil'],'Age'=>[10, 16]]);
|
|
|
|
$expected = [
|
|
|
|
['Name' => 'phil', 'Age' => 11],
|
|
|
|
['Name' => 'bob', 'Age' => 12],
|
|
|
|
['Name' => 'phil', 'Age' => 12],
|
|
|
|
['Name' => 'bob', 'Age' => 14],
|
|
|
|
['Name' => 'phil', 'Age' => 14],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(10, 16), 'Bananas'=>true));
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeNoMatch()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'bob', 'Age' => 10],
|
|
|
|
['Name' => 'phil', 'Age' => 11],
|
|
|
|
['Name' => 'bob', 'Age' => 12],
|
|
|
|
['Name' => 'phil', 'Age' => 12],
|
|
|
|
['Name' => 'bob', 'Age' => 14],
|
|
|
|
['Name' => 'phil', 'Age' => 14],
|
|
|
|
['Name' => 'bob', 'Age' => 16],
|
|
|
|
['Name' => 'phil', 'Age' => 16]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->exclude(['Name'=>['bob','phil'],'Age'=>[10, 16],'Bananas'=>true]);
|
|
|
|
$expected = [
|
|
|
|
['Name' => 'bob', 'Age' => 10],
|
|
|
|
['Name' => 'phil', 'Age' => 11],
|
|
|
|
['Name' => 'bob', 'Age' => 12],
|
|
|
|
['Name' => 'phil', 'Age' => 12],
|
|
|
|
['Name' => 'bob', 'Age' => 14],
|
|
|
|
['Name' => 'phil', 'Age' => 14],
|
|
|
|
['Name' => 'bob', 'Age' => 16],
|
|
|
|
['Name' => 'phil', 'Age' => 16]
|
|
|
|
];
|
2016-12-16 05:34:21 +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(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'bob', 'Age' => 10, 'HasBananas'=>false],
|
|
|
|
['Name' => 'phil','Age' => 11, 'HasBananas'=>true],
|
|
|
|
['Name' => 'bob', 'Age' => 12, 'HasBananas'=>true],
|
|
|
|
['Name' => 'phil','Age' => 12, 'HasBananas'=>true],
|
|
|
|
['Name' => 'bob', 'Age' => 14, 'HasBananas'=>false],
|
|
|
|
['Name' => 'ann', 'Age' => 14, 'HasBananas'=>true],
|
|
|
|
['Name' => 'phil','Age' => 14, 'HasBananas'=>false],
|
|
|
|
['Name' => 'bob', 'Age' => 16, 'HasBananas'=>false],
|
|
|
|
['Name' => 'phil','Age' => 16, 'HasBananas'=>true],
|
|
|
|
['Name' => 'clair','Age' => 16, 'HasBananas'=>true]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$list = $list->exclude(['Name'=>['bob','phil'],'Age'=>[10, 16],'HasBananas'=>true]);
|
|
|
|
$expected = [
|
|
|
|
['Name' => 'bob', 'Age' => 10, 'HasBananas'=>false],
|
|
|
|
['Name' => 'phil','Age' => 11, 'HasBananas'=>true],
|
|
|
|
['Name' => 'bob', 'Age' => 12, 'HasBananas'=>true],
|
|
|
|
['Name' => 'phil','Age' => 12, 'HasBananas'=>true],
|
|
|
|
['Name' => 'bob', 'Age' => 14, 'HasBananas'=>false],
|
|
|
|
['Name' => 'ann', 'Age' => 14, 'HasBananas'=>true],
|
|
|
|
['Name' => 'phil','Age' => 14, 'HasBananas'=>false],
|
|
|
|
['Name' => 'bob', 'Age' => 16, 'HasBananas'=>false],
|
|
|
|
['Name' => 'clair','Age' => 16, 'HasBananas'=>true]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanFilterBy()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'Steve'],
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($list->canFilterBy('Name'));
|
|
|
|
$this->assertFalse($list->canFilterBy('Age'));
|
|
|
|
}
|
|
|
|
|
2023-08-15 01:31:20 +02:00
|
|
|
public function testCanFilterByArrayData()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
|
|
|
[
|
|
|
|
new ArrayData(['Name' => 'Steve']),
|
|
|
|
new ArrayData(['Name' => 'Bob']),
|
|
|
|
new ArrayData(['Name' => 'John'])
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue($list->canFilterBy('Name'));
|
|
|
|
$this->assertFalse($list->canFilterBy('Age'));
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testCanFilterByEmpty()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
|
|
|
|
|
|
|
$this->assertFalse($list->canFilterBy('Name'));
|
|
|
|
$this->assertFalse($list->canFilterBy('Age'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByID()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['ID' => 1, 'Name' => 'Steve'],
|
|
|
|
['ID' => 2, 'Name' => 'Bob'],
|
|
|
|
['ID' => 3, 'Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByIDs()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['ID' => 1, 'Name' => 'Steve'],
|
|
|
|
['ID' => 2, 'Name' => 'Bob'],
|
|
|
|
['ID' => 3, 'Name' => 'John']
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByIDEmpty()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
|
|
|
|
|
|
|
$element = $list->byID(1);
|
|
|
|
$this->assertNull($element);
|
|
|
|
}
|
2019-03-19 23:46:35 +01:00
|
|
|
|
|
|
|
public function testDataClass()
|
|
|
|
{
|
|
|
|
$list = new ArrayList([
|
|
|
|
new DataObject(['Title' => 'one']),
|
|
|
|
]);
|
|
|
|
$this->assertEquals(DataObject::class, $list->dataClass());
|
|
|
|
$list->pop();
|
|
|
|
$this->assertNull($list->dataClass());
|
|
|
|
$list->setDataClass(DataObject::class);
|
|
|
|
$this->assertEquals(DataObject::class, $list->dataClass());
|
|
|
|
}
|
2019-05-15 23:26:11 +02:00
|
|
|
|
|
|
|
public function testShuffle()
|
|
|
|
{
|
|
|
|
$upperLimit = 50;
|
|
|
|
|
|
|
|
$list = new ArrayList(range(1, $upperLimit));
|
|
|
|
|
|
|
|
$list->shuffle();
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $upperLimit; $i++) {
|
|
|
|
$this->assertContains($i, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertNotEquals(range(1, $upperLimit), $list->toArray());
|
|
|
|
}
|
2020-12-15 02:50:10 +01:00
|
|
|
|
|
|
|
public function testOffsetSet()
|
|
|
|
{
|
|
|
|
$list = new ArrayList(['first value', 'second value']);
|
|
|
|
$this->assertSame(2, $list->count());
|
|
|
|
$list->offsetSet(0, 'new value');
|
|
|
|
$this->assertSame(2, $list->count());
|
|
|
|
$this->assertSame('new value', $list->offsetGet(0));
|
|
|
|
$this->assertSame('second value', $list->offsetGet(1));
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|