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\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);
|
|
|
|
}
|
|
|
|
|
2023-01-18 12:09:36 +01:00
|
|
|
public function limitDataProvider(): array
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2023-01-18 12:09:36 +01:00
|
|
|
$all = [ ['Key' => 1], ['Key' => 2], ['Key' => 3] ];
|
|
|
|
list($one, $two, $three) = $all;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'smaller limit' => [2, 0, [$one, $two]],
|
|
|
|
'limit equal to array' => [3, 0, $all],
|
|
|
|
'limit bigger than array' => [4, 0, $all],
|
|
|
|
'zero limit' => [0, 0, []],
|
|
|
|
'false limit' => [0, 0, []],
|
|
|
|
'null limit' => [null, 0, $all],
|
|
|
|
|
|
|
|
'smaller limit with offset' => [1, 1, [$two]],
|
|
|
|
'limit to end with offset' => [2, 1, [$two, $three]],
|
|
|
|
'bigger limit with offset' => [3, 1, [$two, $three]],
|
|
|
|
'offset beyond end of list' => [4, 3, []],
|
|
|
|
'zero limit with offset' => [0, 1, []],
|
|
|
|
'null limit with offset' => [null, 2, [$three]],
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider limitDataProvider
|
|
|
|
*/
|
|
|
|
public function testLimit($length, $offset, array $expected)
|
|
|
|
{
|
|
|
|
$data = [
|
2020-04-20 19:58:09 +02:00
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
2023-01-18 12:09:36 +01:00
|
|
|
];
|
|
|
|
$list = new ArrayList($data);
|
|
|
|
$this->assertEquals(
|
|
|
|
$list->limit($length, $offset)->toArray(),
|
|
|
|
$expected
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2023-01-18 12:09:36 +01:00
|
|
|
$list->toArray(),
|
|
|
|
$data,
|
|
|
|
'limit is immutable and does not affect the original list'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-18 12:09:36 +01:00
|
|
|
public function testLimitNegative()
|
2022-11-15 06:20:54 +01:00
|
|
|
{
|
2023-01-18 12:09:36 +01:00
|
|
|
$this->expectException(\InvalidArgumentException::class, 'Calling limit with a negative length throws exception');
|
2022-11-15 06:20:54 +01:00
|
|
|
$list = new ArrayList(
|
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
|
|
|
);
|
2023-01-18 12:09:36 +01:00
|
|
|
$list->limit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLimitNegativeOffset()
|
|
|
|
{
|
|
|
|
$this->expectException(\InvalidArgumentException::class, 'Calling limit with a negative offset throws exception');
|
|
|
|
$list = new ArrayList(
|
2022-11-15 06:20:54 +01:00
|
|
|
[
|
|
|
|
['Key' => 1], ['Key' => 2], ['Key' => 3]
|
|
|
|
]
|
|
|
|
);
|
2023-01-18 12:09:36 +01:00
|
|
|
$list->limit(1, -1);
|
2022-11-15 06:20:54 +01:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-29 05:40:19 +02:00
|
|
|
public function provideFindWithSearchfilters()
|
|
|
|
{
|
|
|
|
$objects = $this->getFilterWithSearchfiltersObjects();
|
|
|
|
return [
|
|
|
|
// test a couple of search filters
|
|
|
|
// don't need to be as explicit as the filter tests, just check the syntax works
|
|
|
|
'exact match not case sensitive' => [
|
|
|
|
'args' => ['NoCase:nocase', 'case sensitive'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects[0],
|
|
|
|
],
|
|
|
|
'startswith match' => [
|
|
|
|
'args' => ['StartsWithTest:StartsWith', 'test'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects[3],
|
|
|
|
],
|
|
|
|
'startswith match no case' => [
|
|
|
|
'args' => ['StartsWithTest:StartsWith:nocase', 'test'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects[0],
|
|
|
|
],
|
|
|
|
'startswith match negated' => [
|
|
|
|
'args' => ['StartsWithTest:StartsWith:not', 'Test'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects[1],
|
|
|
|
],
|
|
|
|
'lessthan match' => [
|
|
|
|
'args' => ['GreaterThan100:LessThan', '100'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects[2],
|
|
|
|
],
|
|
|
|
'nomatch greaterthan' => [
|
|
|
|
'args' => ['LessThan100:GreaterThan', 1000],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => null,
|
|
|
|
],
|
|
|
|
'nomatch lessthan' => [
|
|
|
|
'args' => ['LessThan100:LessThan:not', 1000],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFindWithSearchfilters
|
|
|
|
*/
|
|
|
|
public function testFindWithSearchfilters(array $args, array $objects, object|array|null $expected)
|
|
|
|
{
|
|
|
|
$list = new ArrayList($objects);
|
|
|
|
$this->assertEquals($expected, $list->find(...$args));
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2023-08-29 05:40:19 +02:00
|
|
|
private function getFilterWithSearchfiltersObjects()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'ID' => 1,
|
|
|
|
'Name' => 'Steve',
|
|
|
|
'Age' => 21,
|
|
|
|
'Title' => 'First Object',
|
|
|
|
'NoCase' => 'CaSe SeNsItIvE',
|
|
|
|
'CaseSensitive' => 'Case Sensitive',
|
|
|
|
'StartsWithTest' => 'Test Value',
|
|
|
|
'GreaterThan100' => 300,
|
|
|
|
'LessThan100' => 50,
|
|
|
|
'SomeField' => 'Some Value',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ID' => 2,
|
|
|
|
'Name' => 'Steve',
|
|
|
|
'Age' => 18,
|
|
|
|
'Title' => 'Second Object',
|
|
|
|
'NoCase' => 'case sensitive',
|
|
|
|
'CaseSensitive' => 'case sensitive',
|
|
|
|
'StartsWithTest' => 'Not Starts With Test',
|
|
|
|
'GreaterThan100' => 101,
|
|
|
|
'LessThan100' => 99,
|
|
|
|
'SomeField' => 'Another Value',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ID' => 3,
|
|
|
|
'Name' => 'Steve',
|
|
|
|
'Age' => 43,
|
|
|
|
'Title' => 'Third Object',
|
|
|
|
'NoCase' => null,
|
|
|
|
'CaseSensitive' => '',
|
|
|
|
'StartsWithTest' => 'Does not start with test',
|
|
|
|
'GreaterThan100' => 99,
|
|
|
|
'LessThan100' => 99,
|
|
|
|
'SomeField' => 'Some Value',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ID' => 4,
|
|
|
|
'Name' => 'Clair',
|
|
|
|
'Age' => 21,
|
|
|
|
'Title' => 'Fourth Object',
|
|
|
|
'StartsWithTest' => 'test value, but lower case',
|
|
|
|
'GreaterThan100' => 100,
|
|
|
|
'LessThan100' => 100,
|
|
|
|
'SomeField' => 'some value',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ID' => 5,
|
|
|
|
'Name' => 'Clair',
|
|
|
|
'Age' => 52,
|
|
|
|
'Title' => '',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFilterWithSearchfilters()
|
|
|
|
{
|
|
|
|
// Note that search filter tests here are to test syntax and to ensure all supported search filters
|
|
|
|
// work with arraylist - but we don't need to test every possible edge case here,
|
|
|
|
// we can rely on individual searchfilter unit tests for many edge cases
|
|
|
|
$objects = $this->getFilterWithSearchfiltersObjects();
|
|
|
|
return [
|
|
|
|
// exact match filter tests
|
|
|
|
'exact match - negate' => [
|
|
|
|
'args' => ['Title:not', 'First Object'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1], $objects[2], $objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
'exact match - negate two different ways' => [
|
|
|
|
'args' => [[
|
|
|
|
'Title:not' => 'First Object',
|
|
|
|
'Title:ExactMatch:not' => 'Third Object',
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1], $objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
'exact match negated - nothing gets filtered out' => [
|
|
|
|
'filter' => ['Title:not', 'No object has this title - we should have all objects'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => $objects,
|
|
|
|
],
|
|
|
|
'exact match negated against null - only last item gets filtered out' => [
|
|
|
|
'args' => ['SomeField:not', null],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[2], $objects[3]],
|
|
|
|
],
|
|
|
|
'exact match with a few items' => [
|
|
|
|
'args' => ['Title', ['First Object', 'Second Object', 'Third Object']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[2]],
|
|
|
|
],
|
|
|
|
'negate the above test' => [
|
|
|
|
'args' => ['Title:not', ['First Object', 'Second Object', 'Third Object']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
// case sensitivity checks
|
|
|
|
'exact match case sensitive' => [
|
|
|
|
'args' => [['NoCase' => 'case sensitive']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1]],
|
|
|
|
],
|
|
|
|
'exact match case insensitive' => [
|
|
|
|
'args' => ['NoCase:nocase', 'case sensitive'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1]],
|
|
|
|
],
|
|
|
|
'exact match mixed case filters' => [
|
|
|
|
'args' => [[
|
|
|
|
'NoCase:nocase' => 'case sensitive',
|
|
|
|
'CaseSensitive' => 'case sensitive',
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1]],
|
|
|
|
],
|
|
|
|
// explicit exact match
|
|
|
|
'exact match explicit' => [
|
|
|
|
'args' => ['Title:ExactMatch', 'Third Object'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2]],
|
|
|
|
],
|
|
|
|
'exact match explicit with modifier' => [
|
|
|
|
'args' => [['Title:ExactMatch:nocase' => 'third object']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2]],
|
|
|
|
],
|
|
|
|
// partialmatch filter
|
|
|
|
'partial match' => [
|
|
|
|
'args' => ['StartsWithTest:PartialMatch', 'start'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2]],
|
|
|
|
],
|
|
|
|
'partial match with modifier' => [
|
|
|
|
'args' => [['StartsWithTest:PartialMatch:nocase' => 'start']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1], $objects[2]],
|
|
|
|
],
|
|
|
|
// greaterthan filter
|
|
|
|
'greaterthan match' => [
|
|
|
|
'args' => ['GreaterThan100:GreaterThan', 100],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1]],
|
|
|
|
],
|
|
|
|
'greaterthan match with modifier' => [
|
|
|
|
'args' => [['GreaterThan100:GreaterThan:not' => 100]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2], $objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
// greaterthanorequal filter
|
|
|
|
'greaterthanorequal match' => [
|
|
|
|
'args' => ['GreaterThan100:GreaterThanOrEqual', 100],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[3]],
|
|
|
|
],
|
|
|
|
'greaterthanorequal match with modifier' => [
|
|
|
|
'args' => [['GreaterThan100:GreaterThanOrEqual:not' => 100]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2], $objects[4]],
|
|
|
|
],
|
|
|
|
// lessthan filter
|
|
|
|
'lessthan match' => [
|
|
|
|
'args' => ['LessThan100:LessThan', 100],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[2], $objects[4]],
|
|
|
|
],
|
|
|
|
'lessthan match with modifier' => [
|
|
|
|
'args' => [['LessThan100:LessThan:not' => 100]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[3]],
|
|
|
|
],
|
|
|
|
// lessthanorequal filter
|
|
|
|
'lessthanorequal match' => [
|
|
|
|
'args' => ['LessThan100:LessThanOrEqual', 99],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[2], $objects[4]],
|
|
|
|
],
|
|
|
|
'lessthanorequal match with modifier' => [
|
|
|
|
'args' => [['LessThan100:LessThanOrEqual:not' => 99]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[3]],
|
|
|
|
],
|
|
|
|
// various more complex filters/combinations and extra scenarios
|
|
|
|
'complex1' => [
|
|
|
|
'args' => [[
|
|
|
|
'NoCase:nocase' => 'CASE SENSITIVE',
|
|
|
|
'StartsWithTest:StartsWith' => 'Not',
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1]],
|
|
|
|
],
|
|
|
|
'complex2' => [
|
|
|
|
'args' => [[
|
|
|
|
'NoCase:case' => 'CASE SENSITIVE',
|
|
|
|
'StartsWithTest:StartsWith' => 'Not',
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [],
|
|
|
|
],
|
|
|
|
'complex3' => [
|
|
|
|
'args' => [[
|
|
|
|
'LessThan100:LessThan' => 100,
|
|
|
|
'GreaterThan100:GreaterThan:not' => 100,
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2], $objects[4]],
|
|
|
|
],
|
|
|
|
'complex4' => [
|
|
|
|
'args' => [[
|
|
|
|
'LessThan100:LessThan' => 1,
|
|
|
|
'GreaterThan100:GreaterThan' => 100,
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFilterWithSearchfilters
|
|
|
|
*/
|
|
|
|
public function testFilterWithSearchfilters(array $args, array $objects, array $expected)
|
|
|
|
{
|
|
|
|
$list = new ArrayList($objects);
|
|
|
|
$list = $list->filter(...$args);
|
|
|
|
$this->assertEquals(array_column($expected, 'ID'), $list->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFilterAnyWithSearchfilters()
|
|
|
|
{
|
|
|
|
$objects = $this->getFilterWithSearchfiltersObjects();
|
|
|
|
return [
|
|
|
|
// test a couple of search filters
|
|
|
|
// don't need to be as explicit as the filter tests, just check the syntax works
|
|
|
|
'partial match' => [
|
|
|
|
'args' => ['StartsWithTest:PartialMatch', 'start'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2]],
|
|
|
|
],
|
|
|
|
'partial match with modifier' => [
|
|
|
|
'args' => ['StartsWithTest:PartialMatch:nocase', 'start'],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1], $objects[2]],
|
|
|
|
],
|
|
|
|
'greaterthan match' => [
|
|
|
|
'args' => ['GreaterThan100:GreaterThan', 100],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1]],
|
|
|
|
],
|
|
|
|
'greaterthan match with modifier' => [
|
|
|
|
'args' => ['GreaterThan100:GreaterThan:not', 100],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[2], $objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
'multiple filters match' => [
|
|
|
|
'args' => [[
|
|
|
|
'StartsWithTest:PartialMatch:nocase' => 'start',
|
|
|
|
'Age:GreaterThanOrEqual' => 43,
|
|
|
|
]],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[1], $objects[2], $objects[4]],
|
|
|
|
],
|
|
|
|
'partial match with a few items' => [
|
|
|
|
'args' => ['Title:PartialMatch', ['First Object', 'Second Object', 'Third Object']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[0], $objects[1], $objects[2]],
|
|
|
|
],
|
|
|
|
'negate the above test' => [
|
|
|
|
'args' => ['Title:PartialMatch:not', ['First Object', 'Second Object', 'Third Object']],
|
|
|
|
'objects' => $objects,
|
|
|
|
'expected' => [$objects[3], $objects[4]],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFilterAnyWithSearchfilters
|
|
|
|
*/
|
|
|
|
public function testFilterAnyWithSearchfilters(array $args, array $objects, array $expected)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2023-08-29 05:40:19 +02:00
|
|
|
$list = new ArrayList($objects);
|
|
|
|
$list = $list->filterAny(...$args);
|
|
|
|
$this->assertEquals(array_column($expected, 'ID'), $list->column('ID'));
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2023-08-29 05:40:19 +02:00
|
|
|
public function provideFilterAny()
|
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = new ArrayList(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2023-08-29 05:40:19 +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],
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2023-08-29 05:40:19 +02:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => ['Name', 'Bob'],
|
|
|
|
'contains' => [$bob],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => ['Name', ['Aziz', 'Bob']],
|
|
|
|
'contains' => [$bob],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => ['Name', ['Steve', 'Bob']],
|
|
|
|
'contains' => [$steve, $bob],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => [['Name' => 'Bob', 'Age' => 21]],
|
|
|
|
'contains' => [$bob, $steve, $clair, $phil],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => [['Name' => 'Bob', 'Age' => [21, 43]]],
|
|
|
|
'contains' => [$bob, $steve, $clair, $mike, $phil],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => [['Name' => ['Bob', 'Phil'], 'Age' => [21, 43]]],
|
|
|
|
'contains' => [$bob, $steve, $clair, $mike, $phil],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'list' => $list,
|
|
|
|
'args' => [['Name' => ['Bob', 'Nobody'], 'Age' => [21, 43]]],
|
|
|
|
'contains' => [$bob, $steve, $clair, $mike, $phil],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2023-08-29 05:40:19 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideFilterAny
|
|
|
|
*/
|
|
|
|
public function testFilterAny(ArrayList $list, array $args, array $contains)
|
|
|
|
{
|
|
|
|
$filteredList = $list->filterAny(...$args)->toArray();
|
|
|
|
$this->assertCount(count($contains), $filteredList);
|
|
|
|
foreach ($contains as $item) {
|
|
|
|
$this->assertContains($item, $filteredList);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $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());
|
|
|
|
}
|
|
|
|
|
2023-08-29 05:40:19 +02:00
|
|
|
public function provideExcludeWithSearchfilters()
|
|
|
|
{
|
|
|
|
// If it's included in the filter test, then it's excluded in the exclude test,
|
|
|
|
// so we can just use the same scenarios and reverse the expected results.
|
|
|
|
$objects = $this->getFilterWithSearchfiltersObjects();
|
|
|
|
$scenarios = $this->provideFilterWithSearchfilters();
|
|
|
|
foreach ($scenarios as $name => $scenario) {
|
|
|
|
$kept = [];
|
|
|
|
$excluded = [];
|
|
|
|
foreach ($scenario['expected'] as $item) {
|
|
|
|
$kept[] = $item['ID'];
|
|
|
|
}
|
|
|
|
foreach ($objects as $item) {
|
|
|
|
if (!in_array($item['ID'], $kept)) {
|
|
|
|
$excluded[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scenarios[$name]['expected'] = $excluded;
|
|
|
|
}
|
|
|
|
return $scenarios;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideExcludeWithSearchfilters
|
|
|
|
*/
|
|
|
|
public function testExcludeWithSearchfilters(array $args, array $objects, array $expected)
|
|
|
|
{
|
|
|
|
$list = new ArrayList($objects);
|
|
|
|
$list = $list->exclude(...$args);
|
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExcludeAnyWithSearchfilters()
|
|
|
|
{
|
|
|
|
// If it's included in the filterAny test, then it's excluded in the excludeAny test,
|
|
|
|
// so we can just use the same scenarios and reverse the expected results.
|
|
|
|
$objects = $this->getFilterWithSearchfiltersObjects();
|
|
|
|
$scenarios = $this->provideFilterAnyWithSearchfilters();
|
|
|
|
foreach ($scenarios as $name => $scenario) {
|
|
|
|
$kept = [];
|
|
|
|
$excluded = [];
|
|
|
|
foreach ($scenario['expected'] as $item) {
|
|
|
|
$kept[] = $item['ID'];
|
|
|
|
}
|
|
|
|
foreach ($objects as $item) {
|
|
|
|
if (!in_array($item['ID'], $kept)) {
|
|
|
|
$excluded[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scenarios[$name]['expected'] = $excluded;
|
|
|
|
}
|
|
|
|
return $scenarios;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideExcludeAnyWithSearchfilters
|
|
|
|
*/
|
|
|
|
public function testExcludeAnyWithSearchfilters(array $args, array $objects, array $expected)
|
|
|
|
{
|
|
|
|
$list = new ArrayList($objects);
|
|
|
|
$list = $list->excludeAny(...$args);
|
|
|
|
$this->assertEquals($expected, $list->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExcludeAny()
|
|
|
|
{
|
|
|
|
// If it's included in the filterAny test, then it's excluded in the excludeAny test,
|
|
|
|
// so we can just use the same scenarios and reverse the expected results.
|
|
|
|
$scenarios = $this->provideFilterAny();
|
|
|
|
foreach ($scenarios as $name => $scenario) {
|
|
|
|
$kept = [];
|
|
|
|
$excluded = [];
|
|
|
|
/** @var array $item */
|
|
|
|
foreach ($scenario['contains'] as $item) {
|
|
|
|
$kept[] = $item['ID'];
|
|
|
|
}
|
|
|
|
/** @var ArrayData $item */
|
|
|
|
foreach ($scenario['list'] as $item) {
|
|
|
|
$itemAsArray = $item->toMap();
|
|
|
|
if (!in_array($itemAsArray['ID'], $kept)) {
|
|
|
|
$excluded[] = $itemAsArray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scenarios[$name]['contains'] = $excluded;
|
|
|
|
}
|
|
|
|
return $scenarios;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideExcludeAny
|
|
|
|
*/
|
|
|
|
public function testExcludeAny(ArrayList $list, array $args, array $contains)
|
|
|
|
{
|
|
|
|
$filteredList = $list->excludeAny(...$args)->toArray();
|
|
|
|
$this->assertCount(count($contains), $filteredList);
|
|
|
|
foreach ($contains as $item) {
|
|
|
|
$this->assertContains($item, $filteredList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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
|
|
|
}
|