2011-05-01 07:12:50 +02:00
< ? php
class DataListTest extends SapphireTest {
2012-01-25 23:53:12 +01:00
2011-05-01 07:12:50 +02:00
// Borrow the model from DataObjectTest
static $fixture_file = 'DataObjectTest.yml' ;
protected $extraDataObjects = array (
'DataObjectTest_Team' ,
'DataObjectTest_Fixture' ,
'DataObjectTest_SubTeam' ,
'OtherSubclassWithSameField' ,
'DataObjectTest_FieldlessTable' ,
'DataObjectTest_FieldlessSubTable' ,
'DataObjectTest_ValidatedObject' ,
'DataObjectTest_Player' ,
'DataObjectTest_TeamComment'
);
2012-01-25 23:53:12 +01:00
public function testSubtract (){
2012-05-28 11:13:42 +02:00
$subtractList = DataObjectTest_TeamComment :: get () -> filter ( 'ID' , 1 );
$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' );
}
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 );
}
2012-02-11 02:24:49 +01:00
2011-05-01 07:12:50 +02:00
function testListCreationSortAndLimit () {
// 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' );
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 );
2012-02-11 02:24:49 +01: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' ));
// We can sort a list
$list = $list -> sort ( 'Name' );
$this -> assertEquals ( array ( 'Bob' , 'Joe' , 'Phil' ), $list -> column ( 'Name' ));
// 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
}
2011-10-26 23:43:38 +02:00
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 ());
}
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 ));
}
function testSql () {
2012-05-28 11:13:42 +02:00
$list = DataObjectTest_TeamComment :: get ();
2011-10-26 23:43:38 +02:00
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", "DataObjectTest_TeamComment"."Created", "DataObjectTest_TeamComment"."LastEdited", "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 \'DataObjectTest_TeamComment\' END AS "RecordClassName" FROM "DataObjectTest_TeamComment"' ;
$this -> assertEquals ( $expected , $list -> sql ());
}
function testInnerJoin () {
2012-05-28 11:13:42 +02:00
$list = DataObjectTest_TeamComment :: get ();
2011-10-26 23:43:38 +02:00
$list -> innerJoin ( 'DataObjectTest_Team' , '"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"' , 'Team' );
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", "DataObjectTest_TeamComment"."Created", "DataObjectTest_TeamComment"."LastEdited", "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 \'DataObjectTest_TeamComment\' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN "DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"' ;
$this -> assertEquals ( $expected , $list -> sql ());
}
function testLeftJoin () {
2012-05-28 11:13:42 +02:00
$list = DataObjectTest_TeamComment :: get ();
2011-10-26 23:43:38 +02:00
$list -> leftJoin ( 'DataObjectTest_Team' , '"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"' , 'Team' );
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", "DataObjectTest_TeamComment"."Created", "DataObjectTest_TeamComment"."LastEdited", "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 \'DataObjectTest_TeamComment\' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN "DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"' ;
$this -> assertEquals ( $expected , $list -> sql ());
}
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' ]);
}
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'
);
2012-02-11 02:24:49 +01: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
);
2012-02-11 02:24:49 +01:00
2011-10-26 23:43:38 +02:00
$this -> assertEquals ( $otherExpected , $otherMap );
}
2012-02-11 02:24:49 +01:00
2011-05-01 07:12:50 +02:00
function testFilter () {
// coming soon!
2011-10-26 23:43:38 +02:00
}
2011-05-01 07:12:50 +02:00
function testWhere () {
// 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 ();
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' ));
// The where() clauses are chained together with AND
$list3 = $list2 -> where ( '"Name" = \'Bob\'' );
$this -> assertEquals ( array (), $list3 -> column ( 'Comment' ));
}
/**
* Test DataList -> byID ()
*/
function testByID () {
// 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 );
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 );
2011-05-01 07:12:50 +02:00
}
2012-02-11 02:24:49 +01:00
2011-05-01 07:12:50 +02:00
/**
* Test DataList -> removeByID ()
*/
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' );
2011-05-01 07:12:50 +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
}
2012-02-11 02:24:49 +01:00
2011-05-01 07:12:50 +02:00
/**
* Test DataList -> canSortBy ()
*/
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 " ));
2012-02-11 02:24:49 +01: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
}
function testDataListArrayAccess () {
2012-05-28 11:13:42 +02:00
$list = DataObjectTest_Team :: get () -> sort ( 'Title' );
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
}
2011-10-29 06:12:02 +02:00
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 );
}
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 );
}
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' );
}
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' );
}
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' );
}
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' );
}
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' );
}
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' );
}
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' );
}
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' );
}
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' );
}
/**
* $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' );
}
2012-02-11 02:24:49 +01: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 ());
$this -> assertEquals ( 'Phil' , $list -> first () -> Name , 'First comment should be from Bob' );
}
2012-02-11 02:24:49 +01:00
// public function testSimpleFilterLessThanFilter() {
2012-05-28 11:13:42 +02:00
// $list = DataObjectTest_TeamComment::get();
2012-02-11 02:24:49 +01:00
// $list = $list->filter('TeamID:LessThan', $this->idFromFixture('DataObjectTest_TeamComment', 'comment2'))->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');
// }
2011-12-08 22:08:46 +01:00
public function testSimpleNegationFilter () {
2012-05-28 11:13:42 +02:00
$list = DataObjectTest_TeamComment :: get ();
2012-06-15 03:25:23 +02:00
$list = $list -> filter ( 'TeamID:Negation' , $this -> idFromFixture ( 'DataObjectTest_Team' , 'team1' ));
2011-12-08 22:08:46 +01:00
$this -> assertEquals ( 1 , $list -> count ());
$this -> assertEquals ( 'Phil' , $list -> first () -> Name , 'First comment should be from Bob' );
}
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' ));
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' );
}
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 ());
}
/**
* $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' ));
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' );
}
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 ());
}
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' )));
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' );
}
/**
* $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-06-15 03:25:23 +02:00
$list = $list -> filter ( array ( 'Name' => array ( 'Bob' , 'Phil' ), 'TeamID' => array ( $this -> idFromFixture ( 'DataObjectTest_Team' , 'team1' ))));
2011-12-08 22:08:46 +01:00
$this -> assertEquals ( 1 , $list -> count (), 'There should be one comments' );
$this -> assertEquals ( 'Bob' , $list -> first () -> Name , 'Only comment should be from Bob' );
}
/**
* $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' );
}
//
/**
* $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 ());
}
/**
* $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 ();
2011-12-08 22:08:46 +01:00
$list -> exclude ( array ( 'Name' => 'Bob' , 'Comment' => 'This is a team comment by Bob' ));
$this -> assertEquals ( 2 , $list -> count ());
}
/**
* $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' );
}
/**
* $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 ());
}
/**
* $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-06-15 03:25:23 +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 ());
}
/**
* $list -> exclude ( array ( 'Name' => array ( 'bob' , 'phil' ), 'Age' => array ( 21 , 43 )));
*/
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' );
}
/**
* $list -> exclude ( array ( 'Name' => array ( 'bob' , 'phil' ), 'Age' => array ( 21 , 43 )));
*/
public function testMultipleExcludeWithTwoArrayOneTeam () {
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' , 'Phil' ), 'TeamID' => array ( $this -> idFromFixture ( 'DataObjectTest_Team' , 'team1' ))));
$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
/**
*
*/
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-02-11 02:24:49 +01: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
}
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 ();
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 () {
// 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
$list = DataObjectTest_Team :: get () -> sort ( 'CASE WHEN "DataObjectTest_Team"."ClassName" = \'DataObjectTest_SubTeam\' THEN 0 ELSE 1 END, "Title" DESC' );
$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
}