2011-05-01 07:12:50 +02:00
|
|
|
<?php
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2011-05-01 07:12:50 +02:00
|
|
|
class DataListTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// Borrow the model from DataObjectTest
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
2011-05-01 07:12:50 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-01-27 01:46:43 +01:00
|
|
|
// From DataObjectTest
|
2011-05-01 07:12:50 +02:00
|
|
|
'DataObjectTest_Team',
|
|
|
|
'DataObjectTest_Fixture',
|
|
|
|
'DataObjectTest_SubTeam',
|
|
|
|
'OtherSubclassWithSameField',
|
|
|
|
'DataObjectTest_FieldlessTable',
|
|
|
|
'DataObjectTest_FieldlessSubTable',
|
|
|
|
'DataObjectTest_ValidatedObject',
|
|
|
|
'DataObjectTest_Player',
|
2012-06-25 00:43:37 +02:00
|
|
|
'DataObjectTest_TeamComment',
|
2015-01-24 00:13:17 +01:00
|
|
|
'DataObjectTest_EquipmentCompany',
|
|
|
|
'DataObjectTest_SubEquipmentCompany',
|
2012-06-25 00:43:37 +02:00
|
|
|
'DataObjectTest\NamespacedClass',
|
2016-01-27 01:46:43 +01:00
|
|
|
'DataObjectTest\RelationClass',
|
|
|
|
'DataObjectTest_ExtendedTeamComment',
|
2015-02-12 14:15:32 +01:00
|
|
|
'DataObjectTest_Company',
|
2016-01-27 01:46:43 +01:00
|
|
|
'DataObjectTest_Staff',
|
|
|
|
'DataObjectTest_CEO',
|
2015-02-12 14:15:32 +01:00
|
|
|
'DataObjectTest_Fan',
|
2016-01-27 01:46:43 +01:00
|
|
|
'DataObjectTest_Play',
|
|
|
|
'DataObjectTest_Ploy',
|
|
|
|
'DataObjectTest_Bogey',
|
2016-05-10 16:56:15 +02:00
|
|
|
'ManyManyListTest_Product',
|
|
|
|
'ManyManyListTest_Category',
|
2011-05-01 07:12:50 +02:00
|
|
|
);
|
2013-07-02 15:09:05 +02:00
|
|
|
|
|
|
|
public function testFilterDataObjectByCreatedDate() {
|
|
|
|
// create an object to test with
|
|
|
|
$obj1 = new DataObjectTest_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
|
|
|
|
$obj1 = DataObjectTest_ValidatedObject::get()->filter(array('Name' => 'test obj 1'))->first();
|
|
|
|
$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
|
|
|
|
$obj1 = DataObjectTest_ValidatedObject::get()->filter(array('Name' => 'test obj 1'))->first();
|
|
|
|
$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 DataObjectTest_ValidatedObject();
|
|
|
|
$obj2->Name = 'test obj 2';
|
|
|
|
$obj2->write();
|
|
|
|
$this->assertTrue($obj2->isInDB());
|
|
|
|
|
|
|
|
// and a third object
|
|
|
|
$obj3 = new DataObjectTest_ValidatedObject();
|
|
|
|
$obj3->Name = 'test obj 3';
|
|
|
|
$obj3->write();
|
|
|
|
$this->assertTrue($obj3->isInDB());
|
|
|
|
|
|
|
|
// now test the filtering based on Created timestamp
|
|
|
|
$list = DataObjectTest_ValidatedObject::get()
|
|
|
|
->filter(array('Created:GreaterThan' => '2013-02-01 00:00:00'))
|
|
|
|
->toArray();
|
|
|
|
$this->assertEquals(2, count($list));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-25 23:53:12 +01:00
|
|
|
public function testSubtract(){
|
2012-09-12 10:51:40 +02:00
|
|
|
$comment1 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1');
|
|
|
|
$subtractList = DataObjectTest_TeamComment::get()->filter('ID', $comment1->ID);
|
2012-05-28 11:13:42 +02:00
|
|
|
$fullList = DataObjectTest_TeamComment::get();
|
2012-01-25 23:53:12 +01:00
|
|
|
$newList = $fullList->subtract($subtractList);
|
|
|
|
$this->assertEquals(2, $newList->Count(), 'List should only contain two objects after subtraction');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 23:53:12 +01:00
|
|
|
public function testSubtractBadDataclassThrowsException(){
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
2012-05-28 11:13:42 +02:00
|
|
|
$teamsComments = DataObjectTest_TeamComment::get();
|
|
|
|
$teams = DataObjectTest_Team::get();
|
2012-01-25 23:53:12 +01:00
|
|
|
$teamsComments->subtract($teams);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testListCreationSortAndLimit() {
|
2011-05-01 07:12:50 +02:00
|
|
|
// By default, a DataList will contain all items of that class
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get()->sort('ID');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can iterate on the DataList
|
|
|
|
$names = array();
|
|
|
|
foreach($list as $item) {
|
|
|
|
$names[] = $item->Name;
|
|
|
|
}
|
|
|
|
$this->assertEquals(array('Joe', 'Bob', 'Phil'), $names);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// If we don't want to iterate, we can extract a single column from the list with column()
|
|
|
|
$this->assertEquals(array('Joe', 'Bob', 'Phil'), $list->column('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can sort a list
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(array('Bob', 'Joe', 'Phil'), $list->column('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can also restrict the output to a range
|
2012-03-09 02:02:37 +01:00
|
|
|
$this->assertEquals(array('Joe', 'Phil'), $list->limit(2, 1)->column('Name'));
|
2011-05-01 07:12:50 +02:00
|
|
|
}
|
2013-08-05 09:59:12 +02:00
|
|
|
|
|
|
|
public function testLimitAndOffset() {
|
|
|
|
$list = DataObjectTest_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());
|
|
|
|
|
2014-10-03 10:33:18 +02:00
|
|
|
$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));
|
2013-08-05 09:59:12 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-09-03 08:53:47 +02:00
|
|
|
public function testDistinct() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query is set as distinct by default');
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
$list = $list->distinct(false);
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertNotContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query does not contain distinct');
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
$list = $list->distinct(true);
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql($params), 'Query contains distinct');
|
2014-09-03 08:53:47 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataClass() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2011-10-26 23:43:38 +02:00
|
|
|
$this->assertEquals('DataObjectTest_TeamComment',$list->dataClass());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-27 01:10:32 +01:00
|
|
|
public function testDataClassCaseInsensitive() {
|
|
|
|
$list = DataList::create('dataobjecttest_teamcomment');
|
|
|
|
$this->assertTrue($list->exists());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testClone() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2011-10-26 23:43:38 +02:00
|
|
|
$this->assertEquals($list, clone($list));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSql() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2013-06-21 00:32:08 +02: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')
|
2015-09-07 03:44:16 +02:00
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment"'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
2011-10-26 23:43:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testInnerJoin() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->innerJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
|
|
|
|
'Team'
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02: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')
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
2015-09-15 03:18:47 +02:00
|
|
|
. '"DataObjectTest_TeamComment"."TeamID"'
|
2015-09-07 03:44:16 +02:00
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
2014-09-15 02:27:55 +02:00
|
|
|
$this->assertEmpty($parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInnerJoinParameterised() {
|
|
|
|
$db = DB::get_conn();
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-09-15 02:27:55 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->innerJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?',
|
|
|
|
'Team',
|
|
|
|
20,
|
|
|
|
array('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')
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID" '
|
2015-09-15 03:18:47 +02:00
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2014-09-15 02:27:55 +02:00
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
$this->assertEquals(array('Team%'), $parameters);
|
2011-10-26 23:43:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLeftJoin() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->leftJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
|
|
|
|
'Team'
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02: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')
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN "DataObjectTest_Team" '
|
2015-09-15 03:18:47 +02:00
|
|
|
. 'AS "Team" ON "DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"'
|
2015-09-07 03:44:16 +02:00
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2012-12-12 05:22:45 +01:00
|
|
|
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
2014-09-15 02:27:55 +02:00
|
|
|
$this->assertEmpty($parameters);
|
2012-06-30 12:37:58 +02:00
|
|
|
|
|
|
|
// Test with namespaces (with non-sensical join, but good enough for testing)
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = $list->leftJoin(
|
2014-08-15 08:53:05 +02:00
|
|
|
'DataObjectTest\NamespacedClass',
|
2012-10-03 18:08:34 +02:00
|
|
|
'"DataObjectTest\NamespacedClass"."ID" = "DataObjectTest_TeamComment"."ID"'
|
|
|
|
);
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2012-10-03 18:08:34 +02:00
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
|
|
|
|
. '"DataObjectTest_TeamComment"."LastEdited", '
|
2013-06-21 00:32:08 +02:00
|
|
|
. '"DataObjectTest_TeamComment"."Created", '
|
2012-10-03 18:08:34 +02:00
|
|
|
. '"DataObjectTest_TeamComment"."Name", '
|
|
|
|
. '"DataObjectTest_TeamComment"."Comment", '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID", '
|
2014-08-15 08:53:05 +02:00
|
|
|
. '"DataObjectTest_TeamComment"."ID", '
|
2012-10-03 18:08:34 +02:00
|
|
|
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL '
|
2014-08-15 08:53:05 +02:00
|
|
|
. 'THEN "DataObjectTest_TeamComment"."ClassName" '
|
2013-06-21 00:32:08 +02:00
|
|
|
. 'ELSE ' . $db->quoteString('DataObjectTest_TeamComment') . ' END AS "RecordClassName" '
|
2012-10-03 18:08:34 +02:00
|
|
|
. 'FROM "DataObjectTest_TeamComment" '
|
|
|
|
. 'LEFT JOIN "DataObjectTest\NamespacedClass" ON '
|
2015-09-07 03:44:16 +02:00
|
|
|
. '"DataObjectTest\NamespacedClass"."ID" = "DataObjectTest_TeamComment"."ID"'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters), 'Retains backslashes in namespaced classes');
|
2014-09-15 02:27:55 +02:00
|
|
|
$this->assertEmpty($parameters);
|
2012-12-12 05:22:45 +01:00
|
|
|
|
2011-10-26 23:43:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-09-15 02:27:55 +02:00
|
|
|
public function testLeftJoinParameterised() {
|
|
|
|
$db = DB::get_conn();
|
|
|
|
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->leftJoin(
|
|
|
|
'DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
|
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?',
|
|
|
|
'Team',
|
|
|
|
20,
|
|
|
|
array('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')
|
|
|
|
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN '
|
|
|
|
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
|
|
|
|
. '"DataObjectTest_TeamComment"."TeamID" '
|
2015-09-15 03:18:47 +02:00
|
|
|
. 'AND "DataObjectTest_Team"."Title" LIKE ?'
|
|
|
|
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
|
2014-09-15 02:27:55 +02:00
|
|
|
|
|
|
|
$this->assertSQLEquals($expected, $list->sql($parameters));
|
|
|
|
$this->assertEquals(array('Team%'), $parameters);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testToNestedArray() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get()->sort('ID');
|
2011-10-26 23:43:38 +02:00
|
|
|
$nestedArray = $list->toNestedArray();
|
|
|
|
$expected = array(
|
|
|
|
0=>
|
|
|
|
array(
|
|
|
|
'ClassName'=>'DataObjectTest_TeamComment',
|
|
|
|
'Name'=>'Joe',
|
|
|
|
'Comment'=>'This is a team comment by Joe',
|
2011-10-29 07:18:50 +02:00
|
|
|
'TeamID'=> $this->objFromFixture('DataObjectTest_TeamComment', 'comment1')->TeamID,
|
2011-10-26 23:43:38 +02:00
|
|
|
),
|
|
|
|
1=>
|
|
|
|
array(
|
|
|
|
'ClassName'=>'DataObjectTest_TeamComment',
|
|
|
|
'Name'=>'Bob',
|
|
|
|
'Comment'=>'This is a team comment by Bob',
|
2011-10-29 07:18:50 +02:00
|
|
|
'TeamID'=> $this->objFromFixture('DataObjectTest_TeamComment', 'comment2')->TeamID,
|
2011-10-26 23:43:38 +02:00
|
|
|
),
|
|
|
|
2=>
|
|
|
|
array(
|
|
|
|
'ClassName'=>'DataObjectTest_TeamComment',
|
|
|
|
'Name'=>'Phil',
|
|
|
|
'Comment'=>'Phil is a unique guy, and comments on team2',
|
2011-10-29 07:18:50 +02:00
|
|
|
'TeamID'=> $this->objFromFixture('DataObjectTest_TeamComment', 'comment3')->TeamID,
|
2011-10-26 23:43:38 +02: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']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMap() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$map = DataObjectTest_TeamComment::get()->map()->toArray();
|
2011-10-29 07:18:50 +02:00
|
|
|
$expected = array(
|
|
|
|
$this->idFromFixture('DataObjectTest_TeamComment', 'comment1') => 'Joe',
|
|
|
|
$this->idFromFixture('DataObjectTest_TeamComment', 'comment2') => 'Bob',
|
|
|
|
$this->idFromFixture('DataObjectTest_TeamComment', 'comment3') => 'Phil'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-26 23:43:38 +02:00
|
|
|
$this->assertEquals($expected, $map);
|
2012-05-28 11:13:42 +02:00
|
|
|
$otherMap = DataObjectTest_TeamComment::get()->map('Name', 'TeamID')->toArray();
|
2011-10-29 07:18:50 +02:00
|
|
|
$otherExpected = array(
|
|
|
|
'Joe' => $this->objFromFixture('DataObjectTest_TeamComment', 'comment1')->TeamID,
|
|
|
|
'Bob' => $this->objFromFixture('DataObjectTest_TeamComment', 'comment2')->TeamID,
|
|
|
|
'Phil' => $this->objFromFixture('DataObjectTest_TeamComment', 'comment3')->TeamID
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-26 23:43:38 +02:00
|
|
|
$this->assertEquals($otherExpected, $otherMap);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
public function testEach() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$count = 0;
|
|
|
|
$test = $this;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$list->each(function($item) use (&$count, $test) {
|
|
|
|
$count++;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$test->assertTrue(is_a($item, "DataObjectTest_TeamComment"));
|
|
|
|
});
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
$this->assertEquals($list->Count(), $count);
|
|
|
|
}
|
2013-06-08 10:29:36 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWhere() {
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can use raw SQL queries with where. This is only recommended for advanced uses;
|
|
|
|
// if you can, you should use filter().
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// where() returns a new DataList, like all the other modifiers, so it can be chained.
|
|
|
|
$list2 = $list->where('"Name" = \'Joe\'');
|
|
|
|
$this->assertEquals(array('This is a team comment by Joe'), $list2->column('Comment'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// The where() clauses are chained together with AND
|
|
|
|
$list3 = $list2->where('"Name" = \'Bob\'');
|
|
|
|
$this->assertEquals(array(), $list3->column('Comment'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
/**
|
|
|
|
* Test DataList->byID()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testByID() {
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can get a single item by ID.
|
2011-11-15 16:51:17 +01:00
|
|
|
$id = $this->idFromFixture('DataObjectTest_Team','team2');
|
2012-05-28 11:13:42 +02:00
|
|
|
$team = DataObjectTest_Team::get()->byID($id);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// byID() returns a DataObject, rather than a DataList
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataObjectTest_Team', $team);
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertEquals('Team 2', $team->Title);
|
2015-10-30 04:26:14 +01:00
|
|
|
|
|
|
|
// Assert that filtering on ID searches by the base table, not the child table field
|
|
|
|
$query = DataObjectTest_SubTeam::get()->filter('ID', 4)->sql($parameters);
|
|
|
|
$this->assertContains('WHERE ("DataObjectTest_Team"."ID" = ?)', $query);
|
|
|
|
$this->assertNotContains('WHERE ("DataObjectTest_SubTeam"."ID" = ?)', $query);
|
2011-05-01 07:12:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
public function testByIDs() {
|
|
|
|
$knownIDs = $this->allFixtureIDs('DataObjectTest_Player');
|
|
|
|
$removedID = array_pop($knownIDs);
|
|
|
|
$filteredPlayers = DataObjectTest_Player::get()->byIDs($knownIDs);
|
|
|
|
foreach ($filteredPlayers as $player) {
|
|
|
|
$this->assertContains($player->ID, $knownIDs);
|
|
|
|
$this->assertNotEquals($removedID, $player->ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
/**
|
|
|
|
* Test DataList->removeByID()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveByID() {
|
2012-05-28 11:17:40 +02:00
|
|
|
$list = DataObjectTest_Team::get();
|
2011-11-15 16:51:17 +01:00
|
|
|
$id = $this->idFromFixture('DataObjectTest_Team','team2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertNotNull($list->byID($id));
|
2011-05-01 07:12:50 +02:00
|
|
|
$list->removeByID($id);
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertNull($list->byID($id));
|
2011-05-01 07:12:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
/**
|
|
|
|
* Test DataList->canSortBy()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCanSortBy() {
|
2011-11-15 16:51:17 +01:00
|
|
|
// Basic check
|
2012-05-28 11:13:42 +02:00
|
|
|
$team = DataObjectTest_Team::get();
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertTrue($team->canSortBy("Title"));
|
|
|
|
$this->assertFalse($team->canSortBy("SomethingElse"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-11-15 16:51:17 +01:00
|
|
|
// Subclasses
|
2012-05-28 11:13:42 +02:00
|
|
|
$subteam = DataObjectTest_SubTeam::get();
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertTrue($subteam->canSortBy("Title"));
|
|
|
|
$this->assertTrue($subteam->canSortBy("SubclassDatabaseField"));
|
2011-05-01 07:12:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataListArrayAccess() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_Team::get()->sort('Title');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-01 07:12:50 +02:00
|
|
|
// We can use array access to refer to single items in the DataList, as if it were an array
|
2011-11-15 16:51:17 +01:00
|
|
|
$this->assertEquals("Subteam 1", $list[0]->Title);
|
|
|
|
$this->assertEquals("Subteam 3", $list[2]->Title);
|
|
|
|
$this->assertEquals("Team 2", $list[4]->Title);
|
2011-05-01 07:12:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFind() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_Team::get();
|
2011-10-29 06:12:02 +02:00
|
|
|
$record = $list->find('Title', 'Team 1');
|
|
|
|
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $record->ID);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFindById() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_Team::get();
|
2011-10-29 06:12:02 +02:00
|
|
|
$record = $list->find('ID', $this->idFromFixture('DataObjectTest_Team', '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', 'team2'));
|
|
|
|
$this->assertEquals('Team 2', $record->Title);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleSort() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleSortOneArgumentASC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name ASC');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleSortOneArgumentDESC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name DESC');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortOneArgumentMultipleColumns() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('TeamID ASC, Name DESC');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleSortASC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name', 'asc');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleSortDESC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name', 'desc');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortWithArraySyntaxSortASC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort(array('Name'=>'asc'));
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortWithArraySyntaxSortDESC() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort(array('Name'=>'desc'));
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSortWithMultipleArraySyntaxSort() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort(array('TeamID'=>'asc','Name'=>'desc'));
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
public function testSortWithCompositeSyntax() {
|
|
|
|
// Phil commented on team with founder surname "Aaron"
|
|
|
|
$list = DataObjectTest_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);
|
|
|
|
}
|
|
|
|
|
2015-10-29 06:37:25 +01:00
|
|
|
public function testSortInvalidParameters() {
|
|
|
|
$this->setExpectedException(
|
|
|
|
'InvalidArgumentException',
|
|
|
|
'Fans is not a linear relation on model DataObjectTest_Player'
|
|
|
|
);
|
|
|
|
$list = DataObjectTest_Team::get();
|
|
|
|
$list = $list->sort('Founder.Fans.Surname'); // Can't sort on has_many
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilter() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_Team::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Title','Team 2');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testSimpleFilterEndsWith() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Name:EndsWith', 'b');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterExactMatchFilter() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Name:ExactMatch', 'Bob');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterGreaterThanFilter() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('TeamID:GreaterThan', $this->idFromFixture('DataObjectTest_Team', 'team1'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
2013-10-30 00:03:03 +01:00
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'First comment should be from Phil');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 00:03:03 +01:00
|
|
|
public function testSimpleFilterGreaterThanOrEqualFilter() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter('TeamID:GreaterThanOrEqual',
|
|
|
|
$this->idFromFixture('DataObjectTest_Team', '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 = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter('TeamID:LessThan',
|
|
|
|
$this->idFromFixture('DataObjectTest_Team', '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 = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter('TeamID:LessThanOrEqual',
|
|
|
|
$this->idFromFixture('DataObjectTest_Team', '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');
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
|
|
|
|
public function testSimplePartialMatchFilter() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Name:PartialMatch', 'o')->sort('Name');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Name:StartsWith', 'B');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleFilterWithNonExistingComparisator() {
|
|
|
|
$this->setExpectedException('InvalidArgumentException');
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Comment:Bogus', 'team comment');
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->filter('Name', array('aziz', 'bob'); // aziz and bob in list
|
|
|
|
*/
|
|
|
|
public function testSimpleFilterWithMultiple() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter('Name', array('Bob','Phil'));
|
2012-12-06 20:45:52 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testMultipleFilterWithNoMatch() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter(array('Name'=>'Bob', 'Comment'=>'Phil is a unique guy, and comments on team2'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(0, $list->count());
|
|
|
|
}
|
2013-01-15 01:11:35 +01:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>'bob, 'Age'=>21)); // bob with the age 21
|
|
|
|
*/
|
|
|
|
public function testFilterMultipleArray() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
|
2012-12-06 20:45:52 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testFilterMultipleWithTwoMatches() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter(array('TeamID'=>$this->idFromFixture('DataObjectTest_Team', 'team1')));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
public function testFilterMultipleWithArrayFilter() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->filter(array('Name'=>array('Bob','Phil')));
|
2012-12-06 20:45:52 +01:00
|
|
|
$list = $list->sort('Name', 'ASC');
|
2011-12-08 22:08:46 +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');
|
|
|
|
}
|
2013-01-15 01:11:35 +01:00
|
|
|
|
|
|
|
public function testFilterMultipleWithArrayFilterAndModifiers() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter(array('Name:StartsWith'=>array('Bo', 'Jo')));
|
|
|
|
$list = $list->sort('Name', 'ASC');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name);
|
|
|
|
$this->assertEquals('Joe', $list->last()->Name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->filter(array('Name'=>array('aziz','bob'), 'Age'=>array(21, 43)));
|
|
|
|
*/
|
|
|
|
public function testFilterArrayInArray() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = $list->filter(array(
|
|
|
|
'Name'=>array('Bob','Phil'),
|
|
|
|
'TeamID'=>array($this->idFromFixture('DataObjectTest_Team', 'team1'))));
|
2012-09-20 09:26:05 +02:00
|
|
|
$this->assertEquals(1, $list->count(), 'There should be one comment');
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
2012-06-24 01:57:04 +02:00
|
|
|
|
2012-09-20 09:26:05 +02:00
|
|
|
public function testFilterWithModifiers() {
|
|
|
|
$list = DataObjectTest_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', 'team1'));
|
|
|
|
$this->assertEquals(2, $gtList->count());
|
|
|
|
}
|
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
public function testFilterAny() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filterAny('Name', 'Bob');
|
|
|
|
$this->assertEquals(1, $list->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-10-12 12:24:03 +02:00
|
|
|
|
|
|
|
public function testFilterAnyMultipleArray() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filterAny(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilterAnyOnFilter() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter(array(
|
|
|
|
'TeamID'=>$this->idFromFixture('DataObjectTest_Team', 'team1')
|
|
|
|
));
|
|
|
|
$list = $list->filterAny(array(
|
|
|
|
'Name'=>array('Phil', 'Joe'),
|
|
|
|
'Comment'=>'This is a team comment by Bob'
|
|
|
|
));
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments from Bob, matched by comment and team'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Joe',
|
|
|
|
$list->offsetGet(1)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments by Joe, matched by name and team (not by comment)'
|
|
|
|
);
|
|
|
|
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filter(array(
|
|
|
|
'TeamID'=>$this->idFromFixture('DataObjectTest_Team', 'team1')
|
|
|
|
));
|
|
|
|
$list = $list->filterAny(array(
|
|
|
|
'Name'=>array('Phil', 'Joe'),
|
|
|
|
'Comment'=>'This is a team comment by Bob'
|
|
|
|
));
|
|
|
|
$list = $list->sort('Name');
|
|
|
|
$list = $list->filter(array('Name' => 'Bob'));
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments from Bob, matched by name and team'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
public function testFilterAnyMultipleWithArrayFilter() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filterAny(array('Name'=>array('Bob','Phil')));
|
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-12 12:24:03 +02:00
|
|
|
public function testFilterAnyArrayInArray() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filterAny(array(
|
|
|
|
'Name'=>array('Bob','Phil'),
|
|
|
|
'TeamID'=>array($this->idFromFixture('DataObjectTest_Team', 'team1'))))
|
|
|
|
->sort('Name');
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Bob',
|
|
|
|
$list->offsetGet(0)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments from Bob, matched by name and team'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Joe',
|
|
|
|
$list->offsetGet(1)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments by Joe, matched by team (not by name)'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Phil',
|
|
|
|
$list->offsetGet(2)->Name,
|
2012-10-12 12:24:03 +02:00
|
|
|
'Results should include comments from Phil, matched by name (even if he\'s not in Team1)'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:29:36 +02:00
|
|
|
public function testFilterOnJoin() {
|
|
|
|
$list = DataObjectTest_TeamComment::get()
|
|
|
|
->leftJoin('DataObjectTest_Team',
|
|
|
|
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"'
|
|
|
|
)->filter(array(
|
|
|
|
'Title' => 'Team 1'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
2013-07-05 01:45:34 +02:00
|
|
|
$values = $list->column('Name');
|
|
|
|
$this->assertEquals(array_intersect($values, array('Joe', 'Bob')), $values);
|
2013-06-08 10:29:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 20:43:32 +02:00
|
|
|
public function testFilterOnImplicitJoin() {
|
|
|
|
// Many to many
|
|
|
|
$list = DataObjectTest_Team::get()
|
|
|
|
->filter('Players.FirstName', array('Captain', 'Captain 2'));
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
|
|
|
|
// Has many
|
|
|
|
$list = DataObjectTest_Team::get()
|
|
|
|
->filter('Comments.Name', array('Joe', 'Phil'));
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
|
|
|
|
// Has one
|
|
|
|
$list = DataObjectTest_Player::get()
|
|
|
|
->filter('FavouriteTeam.Title', 'Team 1');
|
|
|
|
|
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('007', $list->first()->ShirtNumber);
|
|
|
|
}
|
|
|
|
|
2016-05-02 06:41:30 +02:00
|
|
|
public function testFilterOnInvalidRelation() {
|
|
|
|
$this->setExpectedException(
|
|
|
|
'InvalidArgumentException',
|
|
|
|
"MascotAnimal is not a relation on model DataObjectTest_Team"
|
|
|
|
);
|
|
|
|
// Filter on missing relation 'MascotAnimal'
|
|
|
|
DataObjectTest_Team::get()
|
|
|
|
->filter('MascotAnimal.Name', 'Richard')
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2012-06-24 01:57:04 +02:00
|
|
|
public function testFilterAndExcludeById() {
|
|
|
|
$id = $this->idFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$list = DataObjectTest_SubTeam::get()->filter('ID', $id);
|
|
|
|
$this->assertEquals($id, $list->first()->ID);
|
|
|
|
|
|
|
|
$list = DataObjectTest_SubTeam::get();
|
|
|
|
$this->assertEquals(3, count($list));
|
|
|
|
$this->assertEquals(2, count($list->exclude('ID', $id)));
|
2012-06-25 00:43:37 +02:00
|
|
|
|
|
|
|
// Check that classes with namespaces work.
|
|
|
|
$obj = new DataObjectTest\NamespacedClass();
|
|
|
|
$obj->Name = "Test";
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
$list = DataObjectTest\NamespacedClass::get()->filter('ID', $obj->ID);
|
|
|
|
$this->assertEquals('Test', $list->First()->Name);
|
|
|
|
$this->assertEquals(0, $list->exclude('ID', $obj->ID)->count());
|
2012-06-24 01:57:04 +02:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:39:39 +02:00
|
|
|
public function testFilterByNull() {
|
|
|
|
$list = DataObjectTest_Fan::get();
|
|
|
|
// Force DataObjectTest_Fan/fan5::Email to empty string
|
|
|
|
$fan5id = $this->idFromFixture('DataObjectTest_Fan', 'fan5');
|
|
|
|
DB::prepared_query("UPDATE \"DataObjectTest_Fan\" SET \"Email\" = '' WHERE \"ID\" = ?", array($fan5id));
|
|
|
|
|
|
|
|
// Filter by null email
|
|
|
|
$nullEmails = $list->filter('Email', null);
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
)
|
|
|
|
), $nullEmails);
|
|
|
|
|
|
|
|
// Filter by non-null
|
|
|
|
$nonNullEmails = $list->filter('Email:not', null);
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Hamish',
|
|
|
|
)
|
|
|
|
), $nonNullEmails);
|
|
|
|
|
|
|
|
// Filter by empty only
|
|
|
|
$emptyOnly = $list->filter('Email', '');
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Hamish',
|
|
|
|
)
|
|
|
|
), $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', '');
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
)
|
|
|
|
), $nonEmptyOnly);
|
|
|
|
|
|
|
|
// Filter by many including null, empty string, and non-empty
|
|
|
|
$items1 = $list->filter('Email', array(null, '', 'damian@thefans.com'));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Hamish',
|
|
|
|
)
|
|
|
|
), $items1);
|
|
|
|
|
|
|
|
// Filter exclusion of above list
|
|
|
|
$items2 = $list->filter('Email:not', array(null, '', 'damian@thefans.com'));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
|
|
|
),
|
|
|
|
), $items2);
|
|
|
|
|
|
|
|
// Filter by many including empty string and non-empty
|
|
|
|
$items3 = $list->filter('Email', array('', 'damian@thefans.com'));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Damian',
|
|
|
|
'Email' => 'damian@thefans.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Hamish',
|
|
|
|
)
|
|
|
|
), $items3);
|
|
|
|
|
|
|
|
// Filter by many including empty string and non-empty
|
|
|
|
// This also relies no the workaround for null comparison as in the $nonEmptyOnly test
|
|
|
|
$items4 = $list->filter('Email:not', array('', 'damian@thefans.com'));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
)
|
|
|
|
), $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(array(
|
|
|
|
'Email:not' => array('', 'damian@thefans.com'),
|
|
|
|
'Email' => null
|
|
|
|
));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Richard',
|
|
|
|
'Email' => 'richie@richers.com',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
)
|
|
|
|
), $items5);
|
|
|
|
|
|
|
|
// Filter by null or empty values
|
|
|
|
$items6 = $list->filter('Email', array(null, ''));
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array(
|
|
|
|
'Name' => 'Stephen',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Mitch',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Name' => 'Hamish',
|
|
|
|
)
|
|
|
|
), $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 = DataObjectTest_Fan::get();
|
|
|
|
|
|
|
|
// Only an explicit NOT NULL should include null values
|
|
|
|
$items6 = $list->filter('Email:not:case', array(null, '', 'damian@thefans.com'));
|
|
|
|
$this->assertSQLContains(' AND "DataObjectTest_Fan"."Email" IS NOT NULL', $items6->sql());
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2015-05-19 03:39:39 +02:00
|
|
|
// These should all include values where Email IS NULL
|
|
|
|
$items7 = $list->filter('Email:nocase', array(null, '', 'damian@thefans.com'));
|
|
|
|
$this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items7->sql());
|
|
|
|
$items8 = $list->filter('Email:not:case', array('', 'damian@thefans.com'));
|
|
|
|
$this->assertSQLContains(' OR "DataObjectTest_Fan"."Email" IS NULL', $items8->sql());
|
|
|
|
|
|
|
|
// These should not contain any null checks at all
|
|
|
|
$items9 = $list->filter('Email:nocase', array('', 'damian@thefans.com'));
|
|
|
|
$this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NULL', $items9->sql());
|
|
|
|
$this->assertSQLNotContains('"DataObjectTest_Fan"."Email" IS NOT NULL', $items9->sql());
|
|
|
|
}
|
|
|
|
|
2013-10-30 14:08:55 +01:00
|
|
|
/**
|
|
|
|
* $list = $list->filterByCallback(function($item, $list) { return $item->Age == 21; })
|
|
|
|
*/
|
|
|
|
public function testFilterByCallback() {
|
|
|
|
$team1ID = $this->idFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->filterByCallback(function ($item, $list) use ($team1ID) {
|
|
|
|
return $item->TeamID == $team1ID;
|
|
|
|
});
|
|
|
|
|
|
|
|
$result = $list->column('Name');
|
|
|
|
$expected = array_intersect($result, array('Joe', 'Bob'));
|
|
|
|
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($expected, $result, 'List should only contain comments from Team 1 (Joe and Bob)');
|
|
|
|
$this->assertTrue($list instanceof SS_Filterable, 'The List should be of type SS_Filterable');
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
*/
|
|
|
|
public function testSimpleExclude() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude('Name', 'Bob');
|
|
|
|
$list = $list->sort('Name');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
//
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude('Name', array('aziz', 'bob'); // exclude aziz and bob from list
|
|
|
|
*/
|
|
|
|
public function testSimpleExcludeWithMultiple() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude('Name', array('Joe','Phil'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Bob', $list->first()->Name, 'First comment should be from Bob');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>21)); // negative version
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMiss() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude(array('Name'=>'Bob', 'Comment'=>'Does not match any comments'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude bob that has Age 21
|
|
|
|
*/
|
|
|
|
public function testMultipleExclude() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-09-06 09:54:15 +02:00
|
|
|
$list = $list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
}
|
2012-06-22 04:34:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that if an exclude() is applied to a filter(), the filter() is still preserved.
|
|
|
|
*/
|
|
|
|
public function testExcludeOnFilter() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-12-08 12:20:20 +01:00
|
|
|
$list = $list->filter('Comment', 'Phil is a unique guy, and comments on team2');
|
2012-06-22 04:34:06 +02:00
|
|
|
$list = $list->exclude('Name', 'Bob');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains(
|
2015-05-19 03:39:39 +02:00
|
|
|
'WHERE ("DataObjectTest_TeamComment"."Comment" = ?) AND (("DataObjectTest_TeamComment"."Name" != ? '
|
|
|
|
. 'OR "DataObjectTest_TeamComment"."Name" IS NULL))',
|
2013-06-21 00:32:08 +02:00
|
|
|
$sql);
|
|
|
|
$this->assertEquals(array('Phil is a unique guy, and comments on team2', 'Bob'), $parameters);
|
2012-06-22 04:34:06 +02:00
|
|
|
}
|
2012-09-06 09:54:15 +02:00
|
|
|
|
|
|
|
public function testExcludeWithSearchFilter() {
|
|
|
|
$list = DataObjectTest_TeamComment::get();
|
|
|
|
$list = $list->exclude('Name:LessThan', 'Bob');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$sql = $list->sql($parameters);
|
|
|
|
$this->assertSQLContains('WHERE (("DataObjectTest_TeamComment"."Name" >= ?))', $sql);
|
|
|
|
$this->assertEquals(array('Bob'), $parameters);
|
2012-09-06 09:54:15 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-10 02:24:45 +01:00
|
|
|
/**
|
|
|
|
* Test exact match filter with empty array items
|
|
|
|
*/
|
|
|
|
public function testEmptyFilter() {
|
2016-01-20 01:16:27 +01:00
|
|
|
$this->setExpectedException("InvalidArgumentException", 'Cannot filter "DataObjectTest_TeamComment"."Name" against an empty set');
|
2015-11-10 02:24:45 +01:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2016-01-20 01:16:27 +01:00
|
|
|
$list->exclude('Name', array());
|
2015-11-10 02:24:45 +01:00
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMultipleThatCheersEitherTeam() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude(array('Name'=>'Bob', 'TeamID'=>array(
|
2012-02-11 02:24:49 +01:00
|
|
|
$this->idFromFixture('DataObjectTest_Team', 'team1'),
|
|
|
|
$this->idFromFixture('DataObjectTest_Team', 'team2')
|
|
|
|
)));
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // negative version
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithMultipleThatCheersOnNonExistingTeam() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude(array('Name'=>'Bob', 'TeamID'=>array(3)));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43))); //negative version
|
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithNoExclusion() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = $list->exclude(array(
|
|
|
|
'Name'=>array('Bob','Joe'),
|
|
|
|
'Comment' => 'Phil is a unique guy, and comments on team2'));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithTwoArray() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->exclude(array('Name' => array('Bob','Joe'), 'TeamID' => array(
|
2012-02-11 02:24:49 +01:00
|
|
|
$this->idFromFixture('DataObjectTest_Team', 'team1'),
|
|
|
|
$this->idFromFixture('DataObjectTest_Team', 'team2')
|
|
|
|
)));
|
2011-12-08 22:08:46 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals('Phil', $list->last()->Name, 'Only comment should be from Phil');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function testMultipleExcludeWithTwoArrayOneTeam() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = $list->exclude(array(
|
|
|
|
'Name' => array('Bob', 'Phil'),
|
|
|
|
'TeamID' => array($this->idFromFixture('DataObjectTest_Team', 'team1'))));
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name');
|
2011-12-08 22:08:46 +01:00
|
|
|
$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');
|
|
|
|
}
|
2011-12-18 04:28:09 +01:00
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-18 04:28:09 +01:00
|
|
|
*/
|
|
|
|
public function testSortByRelation() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2011-12-18 04:28:09 +01:00
|
|
|
$list = $list->sort(array('Team.Title' => 'DESC'));
|
|
|
|
$this->assertEquals(3, $list->count());
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team2'), $list->first()->TeamID,
|
|
|
|
'First comment should be for Team 2');
|
|
|
|
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $list->last()->TeamID,
|
|
|
|
'Last comment should be for Team 1');
|
2011-12-18 04:28:09 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
public function testReverse() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$list = DataObjectTest_TeamComment::get();
|
2012-06-15 03:25:23 +02:00
|
|
|
$list = $list->sort('Name');
|
|
|
|
$list = $list->reverse();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
$this->assertEquals('Bob', $list->last()->Name, 'Last comment should be from Bob');
|
|
|
|
$this->assertEquals('Phil', $list->first()->Name, 'First comment should be from Phil');
|
|
|
|
}
|
2012-05-01 07:09:57 +02:00
|
|
|
|
|
|
|
public function testSortByComplexExpression() {
|
2014-08-15 08:53:05 +02:00
|
|
|
// Test an expression with both spaces and commas. This test also tests that column() can be called
|
2013-06-21 00:32:08 +02:00
|
|
|
// with a complex sort expression, so keep using column() below
|
2012-09-26 23:34:00 +02:00
|
|
|
$list = DataObjectTest_Team::get()->sort(
|
2013-06-21 00:32:08 +02:00
|
|
|
'CASE WHEN "DataObjectTest_Team"."ClassName" = \'DataObjectTest_SubTeam\' THEN 0 ELSE 1 END, "Title" DESC'
|
|
|
|
);
|
2012-05-01 07:09:57 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'Subteam 3',
|
|
|
|
'Subteam 2',
|
|
|
|
'Subteam 1',
|
|
|
|
'Team 3',
|
|
|
|
'Team 2',
|
|
|
|
'Team 1',
|
|
|
|
), $list->column("Title"));
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|