2011-05-01 07:12:50 +02:00
< ? php
class DataListTest extends SapphireTest {
// 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'
);
function testListCreationSortAndLimit () {
// By default, a DataList will contain all items of that class
2011-05-03 13:59:51 +02:00
$list = DataList :: create ( 'DataObjectTest_TeamComment' ) -> 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 );
// 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
$this -> assertEquals ( array ( 'Joe' , 'Phil' ), $list -> getRange ( 1 , 2 ) -> column ( 'Name' ));
}
2011-10-26 23:43:38 +02:00
function testDataClass () {
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
$this -> assertEquals ( 'DataObjectTest_TeamComment' , $list -> dataClass ());
}
function testClone () {
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
$this -> assertEquals ( $list , clone ( $list ));
}
function testSql () {
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
$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 () {
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
$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 () {
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
$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 () {
2011-10-29 03:49:38 +02:00
$list = DataList :: create ( 'DataObjectTest_TeamComment' ) -> 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 () {
2011-10-29 06:08:47 +02:00
$map = DataList :: create ( 'DataObjectTest_TeamComment' ) -> 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'
);
2011-10-26 23:43:38 +02:00
$this -> assertEquals ( $expected , $map );
2011-10-29 06:08:47 +02:00
$otherMap = DataList :: create ( 'DataObjectTest_TeamComment' ) -> 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
);
2011-10-26 23:43:38 +02:00
$this -> assertEquals ( $otherExpected , $otherMap );
}
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().
$list = DataList :: create ( 'DataObjectTest_TeamComment' );
// 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' );
2011-05-01 07:12:50 +02:00
$team = DataList :: create ( " DataObjectTest_Team " ) -> byID ( $id );
// byID() returns a DataObject, rather than a DataList
2011-11-15 16:52:15 +01:00
$this -> assertType ( '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
}
/**
* Test DataList -> removeByID ()
*/
function testRemoveByID () {
$list = DataList :: create ( " DataObjectTest_Team " );
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
}
/**
* Test DataList -> canSortBy ()
*/
function testCanSortBy () {
2011-11-15 16:51:17 +01:00
// Basic check
2011-05-01 07:12:50 +02:00
$team = DataList :: create ( " DataObjectTest_Team " );
2011-11-15 16:51:17 +01:00
$this -> assertTrue ( $team -> canSortBy ( " Title " ));
$this -> assertFalse ( $team -> canSortBy ( " SomethingElse " ));
2011-05-01 07:12:50 +02:00
2011-11-15 16:51:17 +01:00
// Subclasses
2011-05-01 07:12:50 +02:00
$subteam = DataList :: create ( " DataObjectTest_SubTeam " );
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 () {
2011-11-15 16:51:17 +01:00
$list = DataList :: create ( " DataObjectTest_Team " ) -> 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 () {
2011-11-15 16:51:17 +01:00
$list = DataList :: create ( " DataObjectTest_Team " );
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 () {
2011-11-15 16:51:17 +01:00
$list = DataList :: create ( " DataObjectTest_Team " );
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-05-01 07:12:50 +02:00
}