2011-05-01 07:12:50 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
use InvalidArgumentException;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2020-09-05 01:24:37 +02:00
|
|
|
use SilverStripe\Core\Injector\InjectorNotFoundException;
|
2020-01-28 02:03:12 +01:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-05-19 04:07:45 +02:00
|
|
|
use SilverStripe\ORM\DataQuery;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Filterable;
|
2017-02-02 21:03:19 +01:00
|
|
|
use SilverStripe\ORM\Filters\ExactMatchFilter;
|
2017-05-19 04:07:45 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Bracket;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\EquipmentCompany;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Fan;
|
2020-01-28 02:03:12 +01:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Fixture;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Player;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Sortable;
|
2020-01-28 02:03:12 +01:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Staff;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\SubTeam;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\TeamComment;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\ValidatedObject;
|
2020-01-28 02:03:12 +01:00
|
|
|
use SilverStripe\ORM\Tests\ManyManyListTest\Category;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2017-07-03 02:21:27 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class DataListTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
// Borrow the model from DataObjectTest
|
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getExtraDataObjects()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
DataObjectTest::$extra_data_objects,
|
|
|
|
ManyManyListTest::$extra_data_objects
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-02 21:03:19 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFilterDataObjectByCreatedDate()
|
|
|
|
{
|
|
|
|
// create an object to test with
|
|
|
|
$obj1 = new ValidatedObject();
|
|
|
|
$obj1->Name = 'test obj 1';
|
|
|
|
$obj1->write();
|
|
|
|
$this->assertTrue($obj1->isInDB());
|
|
|
|
|
|
|
|
// reload the object from the database and reset its Created timestamp to a known value
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj1 = ValidatedObject::get()->filter(['Name' => 'test obj 1'])->first();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(is_object($obj1));
|
|
|
|
$this->assertEquals('test obj 1', $obj1->Name);
|
|
|
|
$obj1->Created = '2013-01-01 00:00:00';
|
|
|
|
$obj1->write();
|
|
|
|
|
|
|
|
// reload the object again and make sure that our Created date was properly persisted
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj1 = ValidatedObject::get()->filter(['Name' => 'test obj 1'])->first();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(is_object($obj1));
|
|
|
|
$this->assertEquals('test obj 1', $obj1->Name);
|
|
|
|
$this->assertEquals('2013-01-01 00:00:00', $obj1->Created);
|
|
|
|
|
|
|
|
// now save a second object to the DB with an automatically-set Created value
|
|
|
|
$obj2 = new ValidatedObject();
|
|
|
|
$obj2->Name = 'test obj 2';
|
|
|
|
$obj2->write();
|
|
|
|
$this->assertTrue($obj2->isInDB());
|
|
|
|
|
|
|
|
// and a third object
|
|
|
|
$obj3 = new ValidatedObject();
|
|
|
|
$obj3->Name = 'test obj 3';
|
|
|
|
$obj3->write();
|
|
|
|
$this->assertTrue($obj3->isInDB());
|
|
|
|
|
|
|
|
// now test the filtering based on Created timestamp
|
|
|
|
$list = ValidatedObject::get()
|
2020-04-20 19:58:09 +02:00
|
|
|
->filter(['Created:GreaterThan' => '2013-02-01 00:00:00'])
|
2016-12-16 05:34:21 +01:00
|
|
|
->toArray();
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubtract()
|
|
|
|
{
|
|
|
|
$comment1 = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1');
|
|
|
|
$subtractList = TeamComment::get()->filter('ID', $comment1->ID);
|
|
|
|
$fullList = TeamComment::get();
|
|
|
|
$newList = $fullList->subtract($subtractList);
|
|
|
|
$this->assertEquals(2, $newList->Count(), 'List should only contain two objects after subtraction');
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSubtractBadDataclassThrowsException()
|
|
|
|
{
|
|
|
|
$teamsComments = TeamComment::get();
|
|
|
|
$teams = Team::get();
|
|
|
|
$teamsComments->subtract($teams);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testListCreationSortAndLimit()
|
|
|
|
{
|
|
|
|
// By default, a DataList will contain all items of that class
|
|
|
|
$list = TeamComment::get()->sort('ID');
|
|
|
|
|
|
|
|
// We can iterate on the DataList
|
2020-04-20 19:58:09 +02:00
|
|
|
$names = [];
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($list as $item) {
|
|
|
|
$names[] = $item->Name;
|
|
|
|
}
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Joe', 'Bob', 'Phil'], $names);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// If we don't want to iterate, we can extract a single column from the list with column()
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Joe', 'Bob', 'Phil'], $list->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// We can sort a list
|
|
|
|
$list = $list->sort('Name');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Bob', 'Joe', 'Phil'], $list->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// We can also restrict the output to a range
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Joe', 'Phil'], $list->limit(2, 1)->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLimitAndOffset()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$check = $list->limit(3);
|
|
|
|
|
|
|
|
$this->assertEquals(3, $check->count());
|
|
|
|
|
|
|
|
$check = $list->limit(1);
|
|
|
|
$this->assertEquals(1, $check->count());
|
|
|
|
|
|
|
|
$check = $list->limit(1, 1);
|
|
|
|
$this->assertEquals(1, $check->count());
|
|
|
|
|
|
|
|
$check = $list->limit(false);
|
|
|
|
$this->assertEquals(3, $check->count());
|
|
|
|
|
|
|
|
$check = $list->limit(null);
|
|
|
|
$this->assertEquals(3, $check->count());
|
|
|
|
|
|
|
|
$check = $list->limit(null, 2);
|
|
|
|
$this->assertEquals(1, $check->count());
|
|
|
|
|
|
|
|
// count()/first()/last() methods may alter limit/offset, so run the query and manually check the count
|
|
|
|
$check = $list->limit(null, 1)->toArray();
|
|
|
|
$this->assertEquals(2, count($check));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDistinct()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query is set as distinct by default');
|
|
|
|
|
|
|
|
$list = $list->distinct(false);
|
|
|
|
$this->assertNotContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query does not contain distinct');
|
|
|
|
|
|
|
|
$list = $list->distinct(true);
|
|
|
|
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query contains distinct');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataClass()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$this->assertEquals(DataObjectTest\TeamComment::class, $list->dataClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataClassCaseInsensitive()
|
|
|
|
{
|
|
|
|
$list = DataList::create(strtolower(DataObjectTest\TeamComment::class));
|
|
|
|
$this->assertTrue($list->exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClone()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$this->assertEquals($list, clone($list));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSql()
|
|
|
|
{
|
|
|
|
$db = DB::get_conn();
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
|
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL '
|
|
|
|
. 'THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
|
|
|
|
. $db->quoteString(DataObjectTest\TeamComment::class)
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment"'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInnerJoin()
|
|
|
|
{
|
|
|
|
$db = DB::get_conn();
|
|
|
|
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->innerJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
|
|
|
|
'Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
|
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
|
|
|
|
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
|
|
|
|
. $db->quoteString(DataObjectTest\TeamComment::class)
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID"'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
$this->assertEmpty($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInnerJoinParameterised()
|
|
|
|
{
|
|
|
|
$db = DB::get_conn();
|
|
|
|
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->innerJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?',
|
|
|
|
'Team',
|
|
|
|
20,
|
2020-04-20 19:58:09 +02:00
|
|
|
['Team%']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
|
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
|
|
|
|
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
|
|
|
|
. $db->quoteString(DataObjectTest\TeamComment::class)
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Team%'], $parameters);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLeftJoin()
|
|
|
|
{
|
|
|
|
$db = DB::get_conn();
|
|
|
|
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->leftJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
|
|
|
|
'Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
|
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL '
|
|
|
|
. 'THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
|
|
|
|
. $db->quoteString(DataObjectTest\TeamComment::class)
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN "DataObjectTest_Team" '
|
|
|
|
. 'AS "Team" ON "DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
$this->assertEmpty($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLeftJoinParameterised()
|
|
|
|
{
|
|
|
|
$db = DB::get_conn();
|
|
|
|
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->leftJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?',
|
|
|
|
'Team',
|
|
|
|
20,
|
2020-04-20 19:58:09 +02:00
|
|
|
['Team%']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
|
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
|
|
|
|
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
|
|
|
|
. $db->quoteString(DataObjectTest\TeamComment::class)
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Team%'], $parameters);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testToNestedArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get()->sort('ID');
|
|
|
|
$nestedArray = $list->toNestedArray();
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
0=>
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ClassName'=>DataObjectTest\TeamComment::class,
|
|
|
|
'Name'=>'Joe',
|
|
|
|
'Comment'=>'This is a team comment by Joe',
|
|
|
|
'TeamID'=> $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1')->TeamID,
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
1=>
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ClassName'=>DataObjectTest\TeamComment::class,
|
|
|
|
'Name'=>'Bob',
|
|
|
|
'Comment'=>'This is a team comment by Bob',
|
|
|
|
'TeamID'=> $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment2')->TeamID,
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
2=>
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ClassName'=>DataObjectTest\TeamComment::class,
|
|
|
|
'Name'=>'Phil',
|
|
|
|
'Comment'=>'Phil is a unique guy, and comments on team2',
|
|
|
|
'TeamID'=> $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3')->TeamID,
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(3, count($nestedArray));
|
|
|
|
$this->assertEquals($expected[0]['Name'], $nestedArray[0]['Name']);
|
|
|
|
$this->assertEquals($expected[1]['Comment'], $nestedArray[1]['Comment']);
|
|
|
|
$this->assertEquals($expected[2]['TeamID'], $nestedArray[2]['TeamID']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMap()
|
|
|
|
{
|
|
|
|
$map = TeamComment::get()->map()->toArray();
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->idFromFixture(DataObjectTest\TeamComment::class, 'comment1') => 'Joe',
|
|
|
|
$this->idFromFixture(DataObjectTest\TeamComment::class, 'comment2') => 'Bob',
|
|
|
|
$this->idFromFixture(DataObjectTest\TeamComment::class, 'comment3') => 'Phil'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $map);
|
|
|
|
$otherMap = TeamComment::get()->map('Name', 'TeamID')->toArray();
|
2020-04-20 19:58:09 +02:00
|
|
|
$otherExpected = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Joe' => $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1')->TeamID,
|
|
|
|
'Bob' => $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment2')->TeamID,
|
|
|
|
'Phil' => $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3')->TeamID
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($otherExpected, $otherMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAmbiguousAggregate()
|
|
|
|
{
|
|
|
|
// Test that we avoid ambiguity error when a field exists on two joined tables
|
|
|
|
// Fetch the sponsors in a round-about way to simulate this
|
|
|
|
$teamID = $this->idFromFixture(DataObjectTest\Team::class, 'team2');
|
|
|
|
$sponsors = EquipmentCompany::get()->filter('SponsoredTeams.ID', $teamID);
|
|
|
|
$this->assertNotNull($sponsors->Max('ID'));
|
|
|
|
$this->assertNotNull($sponsors->Min('ID'));
|
|
|
|
$this->assertNotNull($sponsors->Avg('ID'));
|
|
|
|
$this->assertNotNull($sponsors->Sum('ID'));
|
|
|
|
|
|
|
|
// Test non-orm many_many_extraFields
|
|
|
|
$company = $this->objFromFixture(EquipmentCompany::class, 'equipmentcompany1');
|
|
|
|
$this->assertNotNull($company->SponsoredTeams()->Max('SponsorFee'));
|
|
|
|
$this->assertNotNull($company->SponsoredTeams()->Min('SponsorFee'));
|
|
|
|
$this->assertNotNull($company->SponsoredTeams()->Avg('SponsorFee'));
|
|
|
|
$this->assertNotNull($company->SponsoredTeams()->Sum('SponsorFee'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEach()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$list->each(
|
|
|
|
function ($item) use (&$count) {
|
|
|
|
$count++;
|
|
|
|
$this->assertInstanceOf(TeamComment::class, $item);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($count, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhere()
|
|
|
|
{
|
|
|
|
// We can use raw SQL queries with where. This is only recommended for advanced uses;
|
|
|
|
// if you can, you should use filter().
|
|
|
|
$list = TeamComment::get();
|
|
|
|
|
|
|
|
// where() returns a new DataList, like all the other modifiers, so it can be chained.
|
|
|
|
$list2 = $list->where('"Name" = \'Joe\'');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['This is a team comment by Joe'], $list2->column('Comment'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// The where() clauses are chained together with AND
|
|
|
|
$list3 = $list2->where('"Name" = \'Bob\'');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([], $list3->column('Comment'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test DataList->byID()
|
|
|
|
*/
|
|
|
|
public function testByID()
|
|
|
|
{
|
|
|
|
// We can get a single item by ID.
|
|
|
|
$id = $this->idFromFixture(DataObjectTest\Team::class, 'team2');
|
|
|
|
$team = Team::get()->byID($id);
|
|
|
|
|
|
|
|
// byID() returns a DataObject, rather than a DataList
|
|
|
|
$this->assertInstanceOf(DataObjectTest\Team::class, $team);
|
|
|
|
$this->assertEquals('Team 2', $team->Title);
|
|
|
|
|
|
|
|
// Assert that filtering on ID searches by the base table, not the child table field
|
|
|
|
$query = SubTeam::get()->filter('ID', 4)->sql($parameters);
|
|
|
|
$this->assertContains('WHERE ("DataObjectTest_Team"."ID" = ?)', $query);
|
|
|
|
$this->assertNotContains('WHERE ("DataObjectTest_SubTeam"."ID" = ?)', $query);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByIDs()
|
|
|
|
{
|
|
|
|
$knownIDs = $this->allFixtureIDs(DataObjectTest\Player::class);
|
|
|
|
$removedID = array_pop($knownIDs);
|
|
|
|
$filteredPlayers = Player::get()->byIDs($knownIDs);
|
|
|
|
foreach ($filteredPlayers as $player) {
|
|
|
|
$this->assertContains($player->ID, $knownIDs);
|
|
|
|
$this->assertNotEquals($removedID, $player->ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test DataList->removeByID()
|
|
|
|
*/
|
|
|
|
public function testRemoveByID()
|
|
|
|
{
|
|
|
|
$list = Team::get();
|
|
|
|
$id = $this->idFromFixture(DataObjectTest\Team::class, 'team2');
|
|
|
|
|
|
|
|
$this->assertNotNull($list->byID($id));
|
|
|
|
$list->removeByID($id);
|
|
|
|
$this->assertNull($list->byID($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test DataList->canSortBy()
|
|
|
|
*/
|
|
|
|
public function testCanSortBy()
|
|
|
|
{
|
|
|
|
// Basic check
|
|
|
|
$team = Team::get();
|
|
|
|
$this->assertTrue($team->canSortBy("Title"));
|
|
|
|
$this->assertFalse($team->canSortBy("SomethingElse"));
|
|
|
|
|
|
|
|
// Subclasses
|
|
|
|
$subteam = SubTeam::get();
|
|
|
|
$this->assertTrue($subteam->canSortBy("Title"));
|
|
|
|
$this->assertTrue($subteam->canSortBy("SubclassDatabaseField"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataListArrayAccess()
|
|
|
|
{
|
|
|
|
$list = Team::get()->sort('Title');
|
|
|
|
|
|
|
|
// We can use array access to refer to single items in the DataList, as if it were an array
|
|
|
|
$this->assertEquals("Subteam 1", $list[0]->Title);
|
|
|
|
$this->assertEquals("Subteam 3", $list[2]->Title);
|
|
|
|
$this->assertEquals("Team 2", $list[4]->Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFind()
|
|
|
|
{
|
|
|
|
$list = Team::get();
|
|
|
|
$record = $list->find('Title', 'Team 1');
|
|
|
|
$this->assertEquals($this->idFromFixture(DataObjectTest\Team::class, 'team1'), $record->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindById()
|
|
|
|
{
|
|
|
|
$list = Team::get();
|
|
|
|
$record = $list->find('ID', $this->idFromFixture(DataObjectTest\Team::class, 'team1'));
|
|
|
|
$this->assertEquals('Team 1', $record->Title);
|
|
|
|
// Test that you can call it twice on the same list
|
|
|
|
$record = $list->find('ID', $this->idFromFixture(DataObjectTest\Team::class, 'team2'));
|
|
|
|
$this->assertEquals('Team 2', $record->Title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSort()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSortOneArgumentASC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name ASC');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSortOneArgumentDESC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name DESC');
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'Last comment should be from Phil');
|
|
|
|
$this->assertEquals('Bob', $list->last()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortOneArgumentMultipleColumns()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('TeamID ASC, Name DESC');
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSortASC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name', 'asc');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSortDESC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name', 'desc');
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'Last comment should be from Phil');
|
|
|
|
$this->assertEquals('Bob', $list->last()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortWithArraySyntaxSortASC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['Name'=>'asc']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortWithArraySyntaxSortDESC()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['Name'=>'desc']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'Last comment should be from Phil');
|
|
|
|
$this->assertEquals('Bob', $list->last()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortWithMultipleArraySyntaxSort()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['TeamID'=>'asc','Name'=>'desc']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortWithCompositeSyntax()
|
|
|
|
{
|
|
|
|
// Phil commented on team with founder surname "Aaron"
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Team.Founder.Surname', 'asc');
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name);
|
|
|
|
$list = $list->sort('Team.Founder.Surname', 'desc');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name);
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Fans is not a linear relation on model SilverStripe\ORM\Tests\DataObjectTest\Player
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSortInvalidParameters()
|
|
|
|
{
|
|
|
|
$list = Team::get();
|
|
|
|
$list->sort('Founder.Fans.Surname'); // Can't sort on has_many
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortNumeric()
|
|
|
|
{
|
|
|
|
$list = Sortable::get();
|
|
|
|
$list1 = $list->sort('Sort', 'ASC');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
-10,
|
|
|
|
-2,
|
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
10
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list1->column('Sort')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortMixedCase()
|
|
|
|
{
|
|
|
|
$list = Sortable::get();
|
|
|
|
$list1 = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-10-04 20:14:16 +02:00
|
|
|
'Bob',
|
|
|
|
'bonny',
|
|
|
|
'jane',
|
|
|
|
'John',
|
|
|
|
'sam',
|
|
|
|
'Steve',
|
|
|
|
'steven'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list1->column('Name')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test DataList->canFilterBy()
|
|
|
|
*/
|
|
|
|
public function testCanFilterBy()
|
|
|
|
{
|
|
|
|
// Basic check
|
|
|
|
$team = Team::get();
|
|
|
|
$this->assertTrue($team->canFilterBy("Title"));
|
|
|
|
$this->assertFalse($team->canFilterBy("SomethingElse"));
|
|
|
|
|
|
|
|
// Has one
|
|
|
|
$this->assertTrue($team->canFilterBy("CaptainID"));
|
|
|
|
$this->assertTrue($team->canFilterBy("Captain.ShirtNumber"));
|
|
|
|
$this->assertFalse($team->canFilterBy("SomethingElse.ShirtNumber"));
|
|
|
|
$this->assertFalse($team->canFilterBy("Captain.SomethingElse"));
|
|
|
|
$this->assertTrue($team->canFilterBy("Captain.FavouriteTeam.Captain.ShirtNumber"));
|
|
|
|
|
|
|
|
// Has many
|
|
|
|
$this->assertTrue($team->canFilterBy("Fans.Name"));
|
|
|
|
$this->assertFalse($team->canFilterBy("SomethingElse.Name"));
|
|
|
|
$this->assertFalse($team->canFilterBy("Fans.SomethingElse"));
|
|
|
|
|
|
|
|
// Many many
|
|
|
|
$this->assertTrue($team->canFilterBy("Players.FirstName"));
|
|
|
|
$this->assertFalse($team->canFilterBy("SomethingElse.FirstName"));
|
|
|
|
$this->assertFalse($team->canFilterBy("Players.SomethingElse"));
|
|
|
|
|
|
|
|
// Subclasses
|
|
|
|
$subteam = SubTeam::get();
|
|
|
|
$this->assertTrue($subteam->canFilterBy("Title"));
|
|
|
|
$this->assertTrue($subteam->canFilterBy("SubclassDatabaseField"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilter()
|
|
|
|
{
|
|
|
|
$list = Team::get();
|
|
|
|
$list = $list->filter('Title', 'Team 2');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Team 2', $list->first()->Title, 'List should only contain Team 2');
|
|
|
|
$this->assertEquals('Team 2', $list->last()->Title, 'Last should only contain Team 2');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterEndsWith()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('Name:EndsWith', 'b');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterExactMatchFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('Name:ExactMatch', 'Bob');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterGreaterThanFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('TeamID:GreaterThan', $this->idFromFixture(DataObjectTest\Team::class, 'team1'));
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'First comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterGreaterThanOrEqualFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
|
|
|
'TeamID:GreaterThanOrEqual',
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1')
|
|
|
|
)->sort("ID");
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterLessThanFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
|
|
|
'TeamID:LessThan',
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team2')
|
|
|
|
)->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Joe', $list->Last()->Name, 'Last comment should be from Joe');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterLessThanOrEqualFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
|
|
|
'TeamID:LessThanOrEqual',
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1')
|
|
|
|
)->sort('ID');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
|
|
|
$this->assertEquals('Bob', $list->Last()->Name, 'Last comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimplePartialMatchFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('Name:PartialMatch', 'o')->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Joe', $list->last()->Name, 'First comment should be from Joe');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterStartsWith()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('Name:StartsWith', 'B');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterWithNonExistingComparisator()
|
|
|
|
{
|
2020-09-05 01:24:37 +02:00
|
|
|
$this->expectException(InjectorNotFoundException::class);
|
|
|
|
$this->expectExceptionMessageRegExp('/Class "?DataListFilter.Bogus"? does not exist/');
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = TeamComment::get();
|
|
|
|
$list->filter('Comment:Bogus', 'team comment');
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* Invalid modifiers are treated as failed filter construction
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testInvalidModifier()
|
|
|
|
{
|
2020-09-05 01:24:37 +02:00
|
|
|
$this->expectException(InjectorNotFoundException::class);
|
|
|
|
$this->expectExceptionMessageRegExp('/Class "?DataListFilter.invalidmodifier"? does not exist/');
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = TeamComment::get();
|
|
|
|
$list->filter('Comment:invalidmodifier', 'team comment');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter('Name', ['aziz', 'bob']); // aziz and bob in list
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultiple()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter('Name', ['Bob','Phil']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleFilterWithNoMatch()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>'Bob', 'Comment'=>'Phil is a unique guy, and comments on team2']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(0, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->filter(['Name'=>'bob, 'Age'=>21]); // bob with the age 21
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testFilterMultipleArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>'Bob', 'Comment'=>'This is a team comment by Bob']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterMultipleWithTwoMatches()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['TeamID'=>$this->idFromFixture(DataObjectTest\Team::class, 'team1')]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterMultipleWithArrayFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name'=>['Bob','Phil']]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(2, $list->count(), 'There should be two comments');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterMultipleWithArrayFilterAndModifiers()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name:StartsWith'=>['Bo', 'Jo']]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name);
|
|
|
|
$this->assertEquals('Joe', $list->last()->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 testFilterArrayInArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name'=>['Bob','Phil'],
|
|
|
|
'TeamID'=>[$this->idFromFixture(DataObjectTest\Team::class, 'team1')]]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(1, $list->count(), 'There should be one comment');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterWithModifiers()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$nocaseList = $list->filter('Name:nocase', 'bob');
|
|
|
|
$this->assertEquals(1, $nocaseList->count(), 'There should be one comment');
|
|
|
|
$caseList = $list->filter('Name:case', 'bob');
|
|
|
|
$this->assertEquals(0, $caseList->count(), 'There should be no comments');
|
|
|
|
$gtList = $list->filter(
|
|
|
|
'TeamID:GreaterThan:not',
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1')
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, $gtList->count());
|
|
|
|
}
|
|
|
|
|
2017-05-19 04:07:45 +02:00
|
|
|
/**
|
|
|
|
* Test that a filter correctly aliases relationships that share common classes
|
|
|
|
*/
|
|
|
|
public function testFilterSharedRelationalClasses()
|
|
|
|
{
|
|
|
|
/** @var Bracket $final1 */
|
|
|
|
$final1 = $this->objFromFixture(Bracket::class, 'final');
|
|
|
|
$prefinal1 = $this->objFromFixture(Bracket::class, 'prefinal1');
|
|
|
|
$prefinal2 = $this->objFromFixture(Bracket::class, 'prefinal2');
|
|
|
|
$semifinal1 = $this->objFromFixture(Bracket::class, 'semifinal1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
|
|
|
|
// grand child can be found from parent
|
|
|
|
$found = Bracket::get()->filter('Next.Next.Title', $final1->Title);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2017-05-19 04:07:45 +02:00
|
|
|
[['Title' => $semifinal1->Title]],
|
|
|
|
$found
|
|
|
|
);
|
|
|
|
|
|
|
|
// grand child can be found from child
|
|
|
|
$found = Bracket::get()->filter('Next.Title', $prefinal1->Title);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2017-05-19 04:07:45 +02:00
|
|
|
[['Title' => $semifinal1->Title]],
|
|
|
|
$found
|
|
|
|
);
|
|
|
|
|
|
|
|
// child can be found from parent
|
|
|
|
$found = Bracket::get()->filter('Next.Title', $final1->Title);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2017-05-19 04:07:45 +02:00
|
|
|
[
|
|
|
|
['Title' => $prefinal1->Title],
|
|
|
|
['Title' => $prefinal2->Title]
|
|
|
|
],
|
|
|
|
$found
|
|
|
|
);
|
|
|
|
|
|
|
|
// Complex filter, get brackets where the following bracket was won by team 1
|
|
|
|
// Note: Includes results from multiple levels
|
|
|
|
$found = Bracket::get()->filter('Next.Winner.Title', $team2->Title);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2017-05-19 04:07:45 +02:00
|
|
|
[
|
|
|
|
['Title' => $prefinal1->Title],
|
|
|
|
['Title' => $prefinal2->Title],
|
|
|
|
['Title' => $semifinal1->Title]
|
|
|
|
],
|
|
|
|
$found
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterOnImplicitJoinWithSharedInheritance()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = DataObjectTest\RelationChildFirst::get()->filter([
|
|
|
|
'ManyNext.ID' => [
|
2017-05-19 04:07:45 +02:00
|
|
|
$this->idFromFixture(DataObjectTest\RelationChildSecond::class, 'test1'),
|
|
|
|
$this->idFromFixture(DataObjectTest\RelationChildSecond::class, 'test2'),
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
]);
|
2017-05-19 04:07:45 +02:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$ids = $list->column('ID');
|
|
|
|
$this->assertContains($this->idFromFixture(DataObjectTest\RelationChildFirst::class, 'test1'), $ids);
|
|
|
|
$this->assertContains($this->idFromFixture(DataObjectTest\RelationChildFirst::class, 'test2'), $ids);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFilterAny()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filterAny('Name', 'Bob');
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
}
|
|
|
|
|
2017-11-25 17:56:50 +01:00
|
|
|
public function testFilterAnyWithRelation()
|
|
|
|
{
|
|
|
|
$list = Player::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filterAny([
|
2017-11-25 17:56:50 +01:00
|
|
|
'Teams.Title:StartsWith' => 'Team',
|
|
|
|
'ID:GreaterThan' => 0,
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2017-11-25 17:56:50 +01:00
|
|
|
$this->assertCount(4, $list);
|
|
|
|
}
|
|
|
|
|
2018-11-10 00:42:35 +01:00
|
|
|
public function testFilterAnyWithTwoGreaterThanFilters()
|
|
|
|
{
|
|
|
|
|
|
|
|
for ($i=1; $i<=3; $i++) {
|
|
|
|
$f = new Fixture();
|
|
|
|
$f->MyDecimal = $i;
|
|
|
|
$f->write();
|
|
|
|
|
|
|
|
$f = new Fixture();
|
|
|
|
$f->MyInt = $i;
|
|
|
|
$f->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = Fixture::get()->filterAny([
|
|
|
|
'MyDecimal:GreaterThan' => 1, // 2 records
|
|
|
|
'MyInt:GreaterThan' => 2, // 1 record
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertCount(3, $list);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFilterAnyMultipleArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filterAny(['Name'=>'Bob', 'Comment'=>'This is a team comment by Bob']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAnyOnFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'TeamID'=>$this->idFromFixture(DataObjectTest\Team::class, 'team1')
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->filterAny(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name'=>['Phil', 'Joe'],
|
2016-12-16 05:34:21 +01:00
|
|
|
'Comment'=>'This is a team comment by Bob'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
|
|
|
'Results should include comments from Bob, matched by comment and team'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Joe',
|
|
|
|
$list->offsetGet(1)->Name,
|
|
|
|
'Results should include comments by Joe, matched by name and team (not by comment)'
|
|
|
|
);
|
|
|
|
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'TeamID'=>$this->idFromFixture(DataObjectTest\Team::class, 'team1')
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->filterAny(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name'=>['Phil', 'Joe'],
|
2016-12-16 05:34:21 +01:00
|
|
|
'Comment'=>'This is a team comment by Bob'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->sort('Name');
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter(['Name' => 'Bob']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
|
|
|
'Results should include comments from Bob, matched by name and team'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAnyMultipleWithArrayFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filterAny(['Name'=>['Bob','Phil']]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $list->count(), 'There should be two comments');
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAnyArrayInArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filterAny(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name'=>['Bob','Phil'],
|
|
|
|
'TeamID'=>[$this->idFromFixture(DataObjectTest\Team::class, 'team1')]]
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
|
|
|
->sort('Name');
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
|
|
|
'Results should include comments from Bob, matched by name and team'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Joe',
|
|
|
|
$list->offsetGet(1)->Name,
|
|
|
|
'Results should include comments by Joe, matched by team (not by name)'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Phil',
|
|
|
|
$list->offsetGet(2)->Name,
|
|
|
|
'Results should include comments from Phil, matched by name (even if he\'s not in Team1)'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterOnJoin()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get()
|
|
|
|
->leftJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"'
|
2019-10-18 04:31:39 +02:00
|
|
|
)->filter([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title' => 'Team 1'
|
2019-10-18 04:31:39 +02:00
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$values = $list->column('Name');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(array_intersect($values, ['Joe', 'Bob']), $values);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterOnImplicitJoin()
|
|
|
|
{
|
|
|
|
// Many to many
|
|
|
|
$list = Team::get()
|
2020-04-20 19:58:09 +02:00
|
|
|
->filter('Players.FirstName', ['Captain', 'Captain 2']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
|
|
|
|
// Has many
|
|
|
|
$list = Team::get()
|
2020-04-20 19:58:09 +02:00
|
|
|
->filter('Comments.Name', ['Joe', 'Phil']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
|
|
|
|
// Has one
|
|
|
|
$list = Player::get()
|
|
|
|
->filter('FavouriteTeam.Title', 'Team 1');
|
|
|
|
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('007', $list->first()->ShirtNumber);
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage MascotAnimal is not a relation on model SilverStripe\ORM\Tests\DataObjectTest\Team
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFilterOnInvalidRelation()
|
|
|
|
{
|
|
|
|
// Filter on missing relation 'MascotAnimal'
|
|
|
|
Team::get()
|
|
|
|
->filter('MascotAnimal.Name', 'Richard')
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAndExcludeById()
|
|
|
|
{
|
|
|
|
$id = $this->idFromFixture(SubTeam::class, 'subteam1');
|
|
|
|
$list = SubTeam::get()->filter('ID', $id);
|
|
|
|
$this->assertEquals($id, $list->first()->ID);
|
|
|
|
|
|
|
|
$list = SubTeam::get();
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(2, count($list->exclude('ID', $id)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
public function testFilterByNull()
|
|
|
|
{
|
|
|
|
$list = Fan::get();
|
|
|
|
// Force DataObjectTest_Fan/fan5::Email to empty string
|
|
|
|
$fan5id = $this->idFromFixture(Fan::class, 'fan5');
|
2020-04-20 19:58:09 +02:00
|
|
|
DB::prepared_query("UPDATE \"DataObjectTest_Fan\" SET \"Email\" = '' WHERE \"ID\" = ?", [$fan5id]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Filter by null email
|
|
|
|
$nullEmails = $list->filter('Email', null);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$nullEmails
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by non-null
|
|
|
|
$nonNullEmails = $list->filter('Email:not', null);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Hamish',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$nonNullEmails
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by empty only
|
|
|
|
$emptyOnly = $list->filter('Email', '');
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Hamish',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$emptyOnly
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-empty only. This should include null values, since ExactMatchFilter works around
|
|
|
|
// the caveat that != '' also excludes null values in ANSI SQL-92 behaviour.
|
|
|
|
$nonEmptyOnly = $list->filter('Email:not', '');
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$nonEmptyOnly
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by many including null, empty string, and non-empty
|
2020-04-20 19:58:09 +02:00
|
|
|
$items1 = $list->filter('Email', [null, '', 'damian@thefans.com']);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Hamish',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter exclusion of above list
|
2020-04-20 19:58:09 +02:00
|
|
|
$items2 = $list->filter('Email:not', [null, '', 'damian@thefans.com']);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items2
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by many including empty string and non-empty
|
2020-04-20 19:58:09 +02:00
|
|
|
$items3 = $list->filter('Email', ['', 'damian@thefans.com']);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Hamish',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items3
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by many including empty string and non-empty
|
|
|
|
// This also relies no the workaround for null comparison as in the $nonEmptyOnly test
|
2020-04-20 19:58:09 +02:00
|
|
|
$items4 = $list->filter('Email:not', ['', 'damian@thefans.com']);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items4
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by many including empty string and non-empty
|
|
|
|
// The extra null check isn't necessary, but check that this doesn't fail
|
|
|
|
$items5 = $list->filterAny(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Email:not' => ['', 'damian@thefans.com'],
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => null
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items5
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter by null or empty values
|
2020-04-20 19:58:09 +02:00
|
|
|
$items6 = $list->filter('Email', [null, '']);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Stephen',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Mitch',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Hamish',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$items6
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test null checks with case modifiers
|
|
|
|
*/
|
|
|
|
public function testFilterByNullCase()
|
|
|
|
{
|
|
|
|
// Test with case (case/nocase both use same code path)
|
|
|
|
// Test with and without null, and with inclusion/exclusion permutations
|
|
|
|
$list = Fan::get();
|
|
|
|
|
|
|
|
// Only an explicit NOT NULL should include null values
|
2020-04-20 19:58:09 +02:00
|
|
|
$items6 = $list->filter('Email:not:case', [null, '', 'damian@thefans.com']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertSQLContains(' AND "DataObjectTest_Fan"."Email" IS NOT NULL', $items6->sql());
|
|
|
|
|
|
|
|
// These should all include values where Email IS NULL
|
2020-04-20 19:58:09 +02:00
|
|
|
$items7 = $list->filter('Email:nocase', [null, '', 'damian@thefans.com']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items7->sql());
|
2020-04-20 19:58:09 +02:00
|
|
|
$items8 = $list->filter('Email:not:case', ['', 'damian@thefans.com']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items8->sql());
|
|
|
|
|
|
|
|
// These should not contain any null checks at all
|
2020-04-20 19:58:09 +02:00
|
|
|
$items9 = $list->filter('Email:nocase', ['', 'damian@thefans.com']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NULL', $items9->sql());
|
|
|
|
$this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NOT NULL', $items9->sql());
|
|
|
|
}
|
|
|
|
|
2017-02-02 21:03:19 +01:00
|
|
|
public function testAggregateDBName()
|
|
|
|
{
|
|
|
|
$filter = new ExactMatchFilter(
|
|
|
|
'Comments.Count()'
|
|
|
|
);
|
2017-05-19 04:07:45 +02:00
|
|
|
$filter->apply(new DataQuery(DataObjectTest\Team::class));
|
|
|
|
$this->assertEquals('COUNT("comments_DataObjectTest_TeamComment"."ID")', $filter->getDBName());
|
2017-02-02 21:03:19 +01:00
|
|
|
|
|
|
|
foreach (['Comments.Max(ID)', 'Comments.Max( ID )', 'Comments.Max( ID)'] as $name) {
|
|
|
|
$filter = new ExactMatchFilter($name);
|
2017-05-19 04:07:45 +02:00
|
|
|
$filter->apply(new DataQuery(DataObjectTest\Team::class));
|
|
|
|
$this->assertEquals('MAX("comments_DataObjectTest_TeamComment"."ID")', $filter->getDBName());
|
2017-02-02 21:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAggregateFilterExceptions()
|
|
|
|
{
|
|
|
|
$ex = null;
|
|
|
|
try {
|
|
|
|
$filter = new ExactMatchFilter('Comments.Max( This will not parse! )');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$ex = $e;
|
|
|
|
}
|
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $ex);
|
|
|
|
$this->assertRegExp('/Malformed/', $ex->getMessage());
|
|
|
|
|
|
|
|
|
|
|
|
$filter = new ExactMatchFilter('Comments.Max(NonExistentColumn)');
|
|
|
|
$filter->setModel(new DataObjectTest\Team());
|
|
|
|
$ex = null;
|
|
|
|
try {
|
|
|
|
$name = $filter->getDBName();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$ex = $e;
|
|
|
|
}
|
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $ex);
|
|
|
|
$this->assertRegExp('/Invalid column/', $ex->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAggregateFilters()
|
|
|
|
{
|
|
|
|
$teams = Team::get()->filter('Comments.Count()', 2);
|
2017-03-24 12:17:26 +01:00
|
|
|
|
2017-02-02 21:03:19 +01:00
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
$team3 = $this->objFromFixture(Team::class, 'team3');
|
|
|
|
$team4 = $this->objFromFixture(SubTeam::class, 'subteam1');
|
|
|
|
$team5 = $this->objFromFixture(SubTeam::class, 'subteam2_with_player_relation');
|
|
|
|
$team6 = $this->objFromFixture(SubTeam::class, 'subteam3_with_empty_fields');
|
|
|
|
|
|
|
|
$company1 = $this->objFromFixture(EquipmentCompany::class, 'equipmentcompany1');
|
|
|
|
$company2 = $this->objFromFixture(EquipmentCompany::class, 'equipmentcompany2');
|
|
|
|
|
|
|
|
$company1->CurrentStaff()->add(Staff::create(['Salary' => 3])->write());
|
|
|
|
$company1->CurrentStaff()->add(Staff::create(['Salary' => 5])->write());
|
|
|
|
$company2->CurrentStaff()->add(Staff::create(['Salary' => 4])->write());
|
|
|
|
|
|
|
|
$this->assertCount(1, $teams);
|
|
|
|
$this->assertEquals($team1->ID, $teams->first()->ID);
|
|
|
|
|
|
|
|
$teams = Team::get()->filter('Comments.Count()', [1,2]);
|
2017-03-24 12:17:26 +01:00
|
|
|
|
2017-02-02 21:03:19 +01:00
|
|
|
$this->assertCount(2, $teams);
|
|
|
|
foreach ([$team1, $team2] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $teams->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$teams = Team::get()->filter('Comments.Count():GreaterThan', 1);
|
|
|
|
|
|
|
|
$this->assertCount(1, $teams);
|
|
|
|
$this->assertContains(
|
|
|
|
$this->objFromFixture(Team::class, 'team1')->ID,
|
|
|
|
$teams->column('ID')
|
|
|
|
);
|
|
|
|
|
|
|
|
$teams = Team::get()->filter('Comments.Count():LessThan', 2);
|
|
|
|
|
|
|
|
$this->assertCount(5, $teams);
|
|
|
|
foreach ([$team2, $team3, $team4, $team5, $team6] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $teams->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$teams = Team::get()->filter('Comments.Count():GreaterThanOrEqual', 1);
|
|
|
|
|
|
|
|
$this->assertCount(2, $teams);
|
|
|
|
foreach ([$team1, $team2] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $teams->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$teams = Team::get()->filter('Comments.Count():LessThanOrEqual', 1);
|
|
|
|
|
|
|
|
$this->assertCount(5, $teams);
|
|
|
|
foreach ([$team2, $team3, $team4, $team5, $team6] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $teams->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Max(Salary)', 5);
|
|
|
|
$this->assertCount(1, $companies);
|
|
|
|
$this->assertEquals($company1->ID, $companies->first()->ID);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Min(Salary)', 3);
|
|
|
|
$this->assertCount(1, $companies);
|
|
|
|
$this->assertEquals($company1->ID, $companies->first()->ID);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Max(Salary):GreaterThan', 3);
|
|
|
|
$this->assertCount(2, $companies);
|
|
|
|
foreach ([$company1, $company2] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $companies->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Sum(Salary)', 8);
|
|
|
|
$this->assertCount(1, $companies);
|
|
|
|
$this->assertEquals($company1->ID, $companies->first()->ID);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Sum(Salary):LessThan', 7);
|
|
|
|
$this->assertCount(1, $companies);
|
|
|
|
$this->assertEquals($company2->ID, $companies->first()->ID);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Sum(Salary):GreaterThan', 100);
|
|
|
|
$this->assertCount(0, $companies);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Sum(Salary):GreaterThan', 7);
|
|
|
|
$this->assertCount(1, $companies);
|
|
|
|
$this->assertEquals($company1->ID, $companies->first()->ID);
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Avg(Salary)', 4);
|
|
|
|
$this->assertCount(2, $companies);
|
|
|
|
foreach ([$company1, $company2] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $companies->column('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$companies = EquipmentCompany::get()->filter('CurrentStaff.Avg(Salary):LessThan', 10);
|
|
|
|
$this->assertCount(2, $companies);
|
|
|
|
foreach ([$company1, $company2] as $expectedTeam) {
|
|
|
|
$this->assertContains($expectedTeam->ID, $companies->column('ID'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* $list = $list->filterByCallback(function($item, $list) { return $item->Age == 21; })
|
|
|
|
*/
|
|
|
|
public function testFilterByCallback()
|
|
|
|
{
|
|
|
|
$team1ID = $this->idFromFixture(DataObjectTest\Team::class, 'team1');
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filterByCallback(
|
|
|
|
function ($item, $list) use ($team1ID) {
|
|
|
|
return $item->TeamID == $team1ID;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $list->column('Name');
|
2020-04-20 19:58:09 +02:00
|
|
|
$expected = array_intersect($result, ['Joe', 'Bob']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $result, 'List should only contain comments from Team 1 (Joe and Bob)');
|
|
|
|
$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 = TeamComment::get();
|
|
|
|
$list = $list->exclude('Name', 'Bob');
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
//
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude('Name', ['aziz', 'bob']); // exclude aziz and bob from list
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testSimpleExcludeWithMultiple()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude('Name', ['Joe','Phil']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>'bob, 'Age'=>21]); // negative version
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMiss()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude(['Name'=>'Bob', 'Comment'=>'Does not match any comments']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>'bob, 'Age'=>21]); // exclude bob that has Age 21
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExclude()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude(['Name'=>'Bob', 'Comment'=>'This is a team comment by Bob']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:20:17 +01:00
|
|
|
/**
|
|
|
|
* Test doesn't exclude if only matches one
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>'bob, 'Age'=>21]); // exclude bob that has Age 21
|
2018-02-07 21:20:17 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeMultipleMatches()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude(['Name'=>'Bob', 'Comment'=>'Phil is a unique guy, and comments on team2']);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertCount(3, $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* // exclude only those that match both
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeArraysMultipleMatches()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude([
|
|
|
|
'Name'=> ['Bob', 'Phil'],
|
|
|
|
'Comment'=> [
|
2018-02-07 21:20:17 +01:00
|
|
|
'This is a team comment by Bob',
|
|
|
|
'Phil is a unique guy, and comments on team2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Joe']], $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exclude only which matches both params
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeArraysMultipleMatchesOneMiss()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude([
|
|
|
|
'Name' => ['Bob', 'Phil'],
|
|
|
|
'Comment' => [
|
2018-02-07 21:20:17 +01:00
|
|
|
'Does not match any comments',
|
|
|
|
'Phil is a unique guy, and comments on team2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertListEquals(
|
|
|
|
[
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'Joe'],
|
|
|
|
],
|
|
|
|
$list
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that if an exclude() is applied to a filter(), the filter() is still preserved.
|
|
|
|
*/
|
|
|
|
public function testExcludeOnFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->filter('Comment', 'Phil is a unique guy, and comments on team2');
|
|
|
|
$list = $list->exclude('Name', 'Bob');
|
|
|
|
|
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains(
|
|
|
|
'WHERE ("DataObjectTest_TeamComment"."Comment" = ?) AND (("DataObjectTest_TeamComment"."Name" != ? '
|
|
|
|
. 'OR "DataObjectTest_TeamComment"."Name" IS NULL))',
|
|
|
|
$sql
|
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Phil is a unique guy, and comments on team2', 'Bob'], $parameters);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Phil']], $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that if a complicated exclude() is applied to a filter(), the filter() is still preserved.
|
|
|
|
*/
|
|
|
|
public function testComplicatedExcludeOnFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter('Name', ['Phil', 'Bob']);
|
|
|
|
$list = $list->exclude('Name', ['Bob', 'Joe']);
|
2018-02-07 21:20:17 +01:00
|
|
|
|
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains(
|
|
|
|
'WHERE ("DataObjectTest_TeamComment"."Name" IN (?, ?)) AND (("DataObjectTest_TeamComment"."Name" NOT IN (?, ?) '
|
|
|
|
. 'OR "DataObjectTest_TeamComment"."Name" IS NULL))',
|
|
|
|
$sql
|
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Phil', 'Bob', 'Bob', 'Joe'], $parameters);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Phil']], $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that if a very complicated exclude() is applied to a filter(), the filter() is still preserved.
|
|
|
|
*/
|
|
|
|
public function testVeryComplicatedExcludeOnFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->filter('Name', ['Phil', 'Bob']);
|
|
|
|
$list = $list->exclude([
|
|
|
|
'Name' => ['Joe', 'Phil'],
|
|
|
|
'Comment' => ['Matches no comments', 'Not a matching comment']
|
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
|
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains(
|
|
|
|
'WHERE ("DataObjectTest_TeamComment"."Name" IN (?, ?)) '
|
|
|
|
. 'AND (("DataObjectTest_TeamComment"."Name" NOT IN (?, ?) '
|
|
|
|
. 'OR "DataObjectTest_TeamComment"."Name" IS NULL) '
|
|
|
|
. 'OR ("DataObjectTest_TeamComment"."Comment" NOT IN (?, ?) '
|
|
|
|
. 'OR "DataObjectTest_TeamComment"."Comment" IS NULL))',
|
|
|
|
$sql
|
|
|
|
);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Phil', 'Bob', 'Joe', 'Phil', 'Matches no comments', 'Not a matching comment'], $parameters);
|
2018-02-07 21:20:17 +01:00
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertListEquals(
|
|
|
|
[
|
|
|
|
['Name' => 'Bob'],
|
|
|
|
['Name' => 'Phil'],
|
|
|
|
],
|
|
|
|
$list
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExcludeWithSearchFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->exclude('Name:LessThan', 'Bob');
|
|
|
|
|
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains('WHERE (("DataObjectTest_TeamComment"."Name" >= ?))', $sql);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Bob'], $parameters);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 21:20:17 +01:00
|
|
|
/**
|
|
|
|
* Test that Bob and Phil are excluded (one match each)
|
|
|
|
*/
|
|
|
|
public function testExcludeAny()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->excludeAny([
|
2018-02-07 21:20:17 +01:00
|
|
|
'Name' => 'Bob',
|
|
|
|
'Comment' => 'Phil is a unique guy, and comments on team2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Joe']], $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that Bob and Phil are excluded by Name
|
|
|
|
*/
|
|
|
|
public function testExcludeAnyArrays()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->excludeAny([
|
|
|
|
'Name' => ['Bob', 'Phil'],
|
2018-02-07 21:20:17 +01:00
|
|
|
'Comment' => 'No matching comments'
|
2020-04-20 19:58:09 +02:00
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Joe']], $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that Bob is excluded by Name, Phil by comment
|
|
|
|
*/
|
|
|
|
public function testExcludeAnyMultiArrays()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->excludeAny([
|
|
|
|
'Name' => ['Bob', 'Fred'],
|
|
|
|
'Comment' => ['No matching comments', 'Phil is a unique guy, and comments on team2']
|
|
|
|
]);
|
2018-02-07 21:20:17 +01:00
|
|
|
$this->assertListEquals([['Name' => 'Joe']], $list);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test exact match filter with empty array items
|
2017-09-27 09:25:37 +02:00
|
|
|
*
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Cannot filter "DataObjectTest_TeamComment"."Name" against an empty set
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testEmptyFilter()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list->exclude('Name', []);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>'bob, 'Age'=>[21, 43]]); // exclude bob with Age 21 or 43
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMultipleThatCheersEitherTeam()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->exclude(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name'=>'Bob', 'TeamID'=>[
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1'),
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team2')
|
2020-04-20 19:58:09 +02:00
|
|
|
]]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Phil');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'First comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>'bob, 'Age'=>[21, 43]]); // negative version
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMultipleThatCheersOnNonExistingTeam()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->exclude(['Name'=>'Bob', 'TeamID'=>[3]]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-20 09:07:53 +02:00
|
|
|
* $list->exclude(['Name'=>['bob','phil'], 'Age'=>[21, 43]]); //negative version
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithNoExclusion()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->exclude(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name'=>['Bob','Joe'],
|
|
|
|
'Comment' => 'Phil is a unique guy, and comments on team2']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithTwoArray()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->exclude(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => ['Bob','Joe'], 'TeamID' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1'),
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team2')
|
2020-04-20 19:58:09 +02:00
|
|
|
]]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Only comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithTwoArrayOneTeam()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->exclude(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
'Name' => ['Bob', 'Phil'],
|
|
|
|
'TeamID' => [$this->idFromFixture(DataObjectTest\Team::class, 'team1')]]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testSortByRelation()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
2020-04-20 19:58:09 +02:00
|
|
|
$list = $list->sort(['Team.Title' => 'DESC']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team2'),
|
|
|
|
$list->first()->TeamID,
|
|
|
|
'First comment should be for Team 2'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->idFromFixture(DataObjectTest\Team::class, 'team1'),
|
|
|
|
$list->last()->TeamID,
|
|
|
|
'Last comment should be for Team 1'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReverse()
|
|
|
|
{
|
|
|
|
$list = TeamComment::get();
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$list = $list->reverse();
|
|
|
|
|
|
|
|
$this->assertEquals('Bob', $list->last()->Name, 'Last comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'First comment should be from Phil');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortByComplexExpression()
|
|
|
|
{
|
|
|
|
// Test an expression with both spaces and commas. This test also tests that column() can be called
|
|
|
|
// with a complex sort expression, so keep using column() below
|
|
|
|
$teamClass = Convert::raw2sql(SubTeam::class);
|
|
|
|
$list = Team::get()->sort(
|
2018-01-16 19:39:30 +01:00
|
|
|
'CASE WHEN "DataObjectTest_Team"."ClassName" = \'' . $teamClass . '\' THEN 0 ELSE 1 END, "Title" DESC'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Subteam 3',
|
|
|
|
'Subteam 2',
|
|
|
|
'Subteam 1',
|
|
|
|
'Team 3',
|
|
|
|
'Team 2',
|
|
|
|
'Team 1',
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->column("Title")
|
|
|
|
);
|
|
|
|
}
|
2019-05-15 23:26:11 +02:00
|
|
|
|
|
|
|
public function testShuffle()
|
|
|
|
{
|
|
|
|
$list = Team::get()->shuffle();
|
|
|
|
|
|
|
|
$this->assertSQLContains(DB::get_conn()->random() . ' AS "_SortColumn', $list->dataQuery()->sql());
|
|
|
|
}
|
2020-02-11 04:45:15 +01:00
|
|
|
|
2020-01-28 02:03:12 +01:00
|
|
|
public function testColumnFailureInvalidColumn()
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
Category::get()->column('ObviouslyInvalidColumn');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testColumnFailureInvalidTable()
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$columnName = null;
|
|
|
|
Category::get()
|
|
|
|
->applyRelation('Products.ID', $columnName)
|
|
|
|
->column('"ObviouslyInvalidTable"."ID"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testColumnFromRelatedTable()
|
|
|
|
{
|
|
|
|
$columnName = null;
|
|
|
|
$productTitles = Category::get()
|
|
|
|
->applyRelation('Products.Title', $columnName)
|
|
|
|
->column($columnName);
|
|
|
|
|
|
|
|
$productTitles = array_diff($productTitles, [null]);
|
|
|
|
sort($productTitles);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'Product A',
|
|
|
|
'Product B',
|
|
|
|
], $productTitles);
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|