2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DataQueryTest extends SapphireTest {
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataQueryTest_A',
|
|
|
|
'DataQueryTest_B',
|
|
|
|
'DataQueryTest_D',
|
|
|
|
);
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-10-29 06:11:27 +02:00
|
|
|
* Test the leftJoin() and innerJoin method of the DataQuery object
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testJoins() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$dq = new DataQuery('Member');
|
2011-10-29 06:11:27 +02:00
|
|
|
$dq->innerJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertContains("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql());
|
2011-10-29 06:11:27 +02:00
|
|
|
|
|
|
|
$dq = new DataQuery('Member');
|
|
|
|
$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql());
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-06-25 01:34:02 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelationReturn() {
|
2012-06-25 01:34:02 +02:00
|
|
|
$dq = new DataQuery('DataQueryTest_C');
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestA'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestAs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('ManyTestAs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2012-06-25 01:34:02 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestB'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestBs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('ManyTestBs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
public function testRelationOrderWithCustomJoin() {
|
|
|
|
$dataQuery = new DataQuery('DataQueryTest_B');
|
|
|
|
$dataQuery->innerJoin('DataQueryTest_D', '"DataQueryTest_D"."RelationID" = "DataQueryTest_B"."ID"');
|
|
|
|
$dataQuery->execute();
|
|
|
|
}
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DataQueryTest_A extends DataObject implements TestOnly {
|
|
|
|
public static $db = array(
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $has_one = array(
|
|
|
|
'TestC' => 'DataQueryTest_C',
|
|
|
|
);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2012-06-25 01:34:02 +02:00
|
|
|
class DataQueryTest_B extends DataQueryTest_A {
|
|
|
|
public static $db = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $has_one = array(
|
|
|
|
'TestC' => 'DataQueryTest_C',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataQueryTest_C extends DataObject implements TestOnly {
|
2012-12-10 06:11:07 +01:00
|
|
|
|
2012-06-25 01:34:02 +02:00
|
|
|
public static $has_one = array(
|
|
|
|
'TestA' => 'DataQueryTest_A',
|
|
|
|
'TestB' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $has_many = array(
|
|
|
|
'TestAs' => 'DataQueryTest_A',
|
|
|
|
'TestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $many_many = array(
|
|
|
|
'ManyTestAs' => 'DataQueryTest_A',
|
|
|
|
'ManyTestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
class DataQueryTest_D extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $has_one = array(
|
|
|
|
'Relation' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|