2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DataQueryTest extends SapphireTest {
|
2014-04-08 22:46:52 +02:00
|
|
|
|
|
|
|
protected static $fixture_file = 'DataQueryTest.yml';
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataQueryTest_A',
|
|
|
|
'DataQueryTest_B',
|
2014-04-08 22:46:52 +02:00
|
|
|
'DataQueryTest_C',
|
2012-12-10 06:11:07 +01:00
|
|
|
'DataQueryTest_D',
|
2014-04-08 22:46:52 +02:00
|
|
|
'DataQueryTest_E',
|
2012-12-10 06:11:07 +01:00
|
|
|
);
|
|
|
|
|
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\"");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql($parameters));
|
2011-10-29 06:11:27 +02:00
|
|
|
|
|
|
|
$dq = new DataQuery('Member');
|
|
|
|
$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql($parameters));
|
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-09-20 13:28:54 +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-12-14 10:57:28 +01:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
public function testDisjunctiveGroup() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2012-10-03 18:08:34 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConjunctiveGroup() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->conjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2012-10-03 18:08:34 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') AND (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* @todo Test paramaterised
|
|
|
|
*/
|
2012-09-20 13:28:54 +02:00
|
|
|
public function testNestedGroups() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subSubDq = $subDq->conjunctiveGroup();
|
|
|
|
$subSubDq->where('DataQueryTest_A.Age = 18');
|
|
|
|
$subSubDq->where('DataQueryTest_A.Age = 50');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2012-10-03 18:08:34 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR ((DataQueryTest_A.Age = 18) "
|
|
|
|
. "AND (DataQueryTest_A.Age = 50)) OR (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptySubgroup() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
$dq->conjunctiveGroup();
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Empty groups should have no where condition at all
|
|
|
|
$this->assertSQLNotContains('WHERE', $dq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubgroupHandoff() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
|
|
|
|
$orgDq = clone $dq;
|
|
|
|
|
|
|
|
$subDq->sort('"DataQueryTest_A"."Name"');
|
|
|
|
$orgDq->sort('"DataQueryTest_A"."Name"');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
|
|
|
|
$subDq->limit(5, 7);
|
|
|
|
$orgDq->limit(5, 7);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
2013-10-03 03:20:31 +02:00
|
|
|
|
|
|
|
public function testOrderByMultiple() {
|
|
|
|
$dq = new DataQuery('SQLQueryTest_DO');
|
|
|
|
$dq = $dq->sort('"Name" ASC, MID("Name", 8, 1) DESC');
|
|
|
|
$this->assertContains(
|
2013-06-21 00:32:08 +02:00
|
|
|
'ORDER BY "SQLQueryTest_DO"."Name" ASC, "_SortColumn0" DESC',
|
|
|
|
$dq->sql($parameters)
|
2013-10-03 03:20:31 +02:00
|
|
|
);
|
|
|
|
}
|
2014-04-08 22:46:52 +02:00
|
|
|
|
|
|
|
public function testDefaultSort() {
|
|
|
|
$query = new DataQuery('DataQueryTest_E');
|
|
|
|
$result = $query->column('Title');
|
|
|
|
$this->assertEquals(array('First', 'Second', 'Last'), $result);
|
|
|
|
}
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DataQueryTest_A extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'Name' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'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 {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestC' => 'DataQueryTest_C',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataQueryTest_C extends DataObject implements TestOnly {
|
2014-04-08 22:46:52 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
2012-12-10 06:11:07 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestA' => 'DataQueryTest_A',
|
|
|
|
'TestB' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestAs' => 'DataQueryTest_A',
|
|
|
|
'TestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'ManyTestAs' => 'DataQueryTest_A',
|
|
|
|
'ManyTestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
class DataQueryTest_D extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-12-10 06:11:07 +01:00
|
|
|
'Relation' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|
2014-04-08 22:46:52 +02:00
|
|
|
|
|
|
|
class DataQueryTest_E extends DataQueryTest_C implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'SortOrder' => 'Int'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $default_sort = '"DataQueryTest_E"."SortOrder" ASC';
|
|
|
|
}
|