1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/tests/QueryTestCase.php

1215 lines
52 KiB
PHP
Raw Normal View History

2006-05-10 01:39:07 +04:00
<?php
2007-02-16 01:25:26 +03:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Query_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Query_TestCase extends Doctrine_UnitTestCase
{
public function prepareTables() {
2007-02-16 01:25:26 +03:00
$this->tables[] = "Forum_Category";
$this->tables[] = "Forum_Entry";
$this->tables[] = "Forum_Board";
$this->tables[] = "Forum_Thread";
2006-06-26 22:55:42 +04:00
2006-06-03 13:10:43 +04:00
$this->tables[] = "ORM_TestEntry";
$this->tables[] = "ORM_TestItem";
$this->tables[] = "Log_Status";
$this->tables[] = "Log_Entry";
$this->tables[] = "EnumTest";
$this->tables[] = "Task";
$this->tables[] = "Resource";
$this->tables[] = "ResourceType";
2006-06-03 13:10:43 +04:00
2006-06-25 22:34:53 +04:00
try {
$this->dbh->query("DROP TABLE test_items");
} catch(PDOException $e) {
2006-06-26 22:55:42 +04:00
2006-06-25 22:34:53 +04:00
}
try {
$this->dbh->query("DROP TABLE test_entries");
} catch(PDOException $e) {
2006-06-26 22:55:42 +04:00
}
parent::prepareTables();
$this->connection->clear();
}
/**
2006-10-01 20:14:26 +04:00
public function testValidLazyPropertyFetching() {
$q = new Doctrine_Query($this->connection);
$q->from("User(id, name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
$this->connection->clear();
2006-09-18 21:32:12 +04:00
2006-10-01 20:14:26 +04:00
$q->from("User(name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue($users[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertTrue($users[0]->getState(), Doctrine_Record::STATE_CLEAN);
2006-10-01 20:14:26 +04:00
$this->assertEqual($count + 1, count($this->dbh));
$this->assertTrue($users[1]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[1]->email_id));
$this->assertTrue($users[1]->getState(), Doctrine_Record::STATE_CLEAN);
2006-10-01 20:14:26 +04:00
$this->assertEqual($count + 2, count($this->dbh));
$this->assertTrue($users[2]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[2]->email_id));
$this->assertTrue($users[2]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($count + 3, count($this->dbh));
}
2006-10-09 20:32:37 +04:00
public function testMultilineParsing() {
$dql = "
FROM User u
WHERE User.id = ?
";
2006-10-09 20:32:37 +04:00
$q = new Doctrine_Query();
$q->parseQuery($dql);
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e e WHERE e.id = ? AND (e.type = 0)");
2006-10-09 20:32:37 +04:00
}
2006-09-15 01:10:02 +04:00
public function testUnknownFunction() {
$q = new Doctrine_Query();
$f = false;
try {
$q->from('User')->where('User.name.someunknownfunc()');
} catch(Doctrine_Query_Exception $e) {
$f = true;
}
$this->assertTrue($f);
}
public function testBadFunctionLogic() {
$q = new Doctrine_Query();
$f = false;
try {
$q->from('User')->where('User.name.contains(?)');
} catch(Doctrine_Query_Exception $e) {
$f = true;
}
$this->assertTrue($f);
}
2006-09-15 01:10:02 +04:00
public function testDqlContainsFunction() {
$q = new Doctrine_Query();
$this->connection->clear();
$q->from('User')->where('User.Phonenumber.phonenumber.contains(?)');
$this->assertEqual(count($q->getTableStack()), 2);
$this->assertEqual(count($q->getRelationStack()), 1);
$coll = $q->execute(array('123 123'));
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e e LEFT JOIN phonenumber ON e.id = p.entity_id WHERE e.id IN (SELECT entity_id FROM phonenumber WHERE phonenumber = ?) AND (e.type = 0)');
2006-09-15 01:10:02 +04:00
$this->assertEqual($coll->count(), 3);
$this->assertEqual($coll[0]->name, 'zYne');
$this->assertEqual($coll[0]->Phonenumber->count(), 1);
$this->assertEqual($coll[1]->Phonenumber->count(), 3);
$this->assertEqual($coll[2]->Phonenumber->count(), 1);
}
public function testDqlFunctionWithMultipleParams() {
$q = new Doctrine_Query();
$this->connection->clear();
$q->from('User')->where('User.Phonenumber.phonenumber.like(?,?)');
$this->assertEqual(count($q->getTableStack()), 2);
$this->assertEqual(count($q->getRelationStack()), 1);
2006-09-15 01:10:02 +04:00
$coll = $q->execute(array('%123%', '%5%'));
2006-09-15 01:10:02 +04:00
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e e LEFT JOIN phonenumber ON e.id = p.entity_id WHERE e.id IN (SELECT entity_id FROM phonenumber WHERE phonenumber LIKE ?) AND e.id IN (SELECT entity_id FROM phonenumber WHERE phonenumber LIKE ?) AND (e.type = 0)');
$this->assertEqual($coll->count(), 3);
$this->assertEqual($coll[0]->name, 'Arnold Schwarzenegger');
$this->assertEqual($coll[0]->Phonenumber->count(), 3);
$this->assertEqual($coll[1]->Phonenumber->count(), 3);
$this->assertEqual($coll[2]->Phonenumber->count(), 3);
2006-09-15 01:10:02 +04:00
}
public function testDqlLikeFunction() {
$q = new Doctrine_Query();
$this->connection->clear();
$q->from('User')->where('User.Phonenumber.phonenumber.like(?)');
$this->assertEqual(count($q->getTableStack()), 2);
$this->assertEqual(count($q->getRelationStack()), 1);
$coll = $q->execute(array('123%'));
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e e LEFT JOIN phonenumber ON e.id = p.entity_id WHERE e.id IN (SELECT entity_id FROM phonenumber WHERE phonenumber LIKE ?) AND (e.type = 0)');
$this->assertEqual($coll->count(), 5);
$this->assertEqual($coll[0]->name, 'zYne');
$this->assertEqual($coll[0]->Phonenumber->count(), 1);
$this->assertEqual($coll[1]->Phonenumber->count(), 3);
$this->assertEqual($coll[2]->Phonenumber->count(), 1);
$this->assertEqual($coll[3]->Phonenumber->count(), 3);
$this->assertEqual($coll[4]->Phonenumber->count(), 3);
}
public function testEnumConversion() {
$e[0] = new EnumTest();
$e[0]->status = 'open';
$e[1] = new EnumTest();
$e[1]->status = 'verified';
2006-06-26 22:55:42 +04:00
$this->connection->flush();
$this->assertEqual($e[0]->id, 1);
$this->assertEqual($e[1]->id, 2);
$q = new Doctrine_Query;
$coll = $q->from('EnumTest')
->where("EnumTest.status = 'open'")
->execute();
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.status AS e__status FROM enum_test e WHERE e.status = 0');
$this->assertEqual($coll->count(), 1);
$q = new Doctrine_Query;
$coll = $q->from('EnumTest')
->where("EnumTest.status = 'verified'")
->execute();
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.status AS e__status FROM enum_test e WHERE e.status = 1');
$this->assertEqual($coll->count(), 1);
2006-06-26 22:55:42 +04:00
}
2006-09-13 01:36:36 +04:00
public function testManyToManyFetchingWithColumnAggregationInheritance() {
$query = new Doctrine_Query($this->connection);
$query->from('User u INNER JOIN u.Group g');
2006-09-13 01:36:36 +04:00
$users = $query->execute();
$this->assertEqual($users->count(), 1);
$this->assertEqual($users[0]->Group->count(), 1);
$query->from('User-l.Group-l');
$users = $query->execute();
$this->assertEqual($users->count(), 8);
$this->assertEqual($users[0]->Group->count(), 0);
$this->assertEqual($users[1]->Group->count(), 1);
$this->assertEqual($users[2]->Group->count(), 0);
$this->assertEqual($users[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($users[1]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($users[2]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($users[0]->getModified(), array());
$this->assertEqual($users[1]->getModified(), array());
$this->assertEqual($users[2]->getModified(), array());
$this->assertEqual($users[6]->getModified(), array());
$this->assertEqual($users[0]->type, 0);
$this->assertEqual($users[1]->type, 0);
$this->assertEqual($users[2]->type, 0);
$this->connection->flush();
}
public function testManyToManyFetchingWithColumnAggregationInheritance2() {
$query = new Doctrine_Query();
2006-09-13 01:36:36 +04:00
$users = $query->query("FROM User-b WHERE User.Group.name = 'Action Actors'");
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id WHERE e2.name = 'Action Actors' AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
2006-09-13 01:36:36 +04:00
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(),1);
$this->assertEqual(count($this->dbh->query($query->getQuery())->fetchAll()), 1);
}
public function testManyToManyFetchingWithColumnAggregationInheritance3() {
$query = new Doctrine_Query();
2006-09-13 01:36:36 +04:00
$users = $query->query("FROM User-b WHERE User.Group.Phonenumber.phonenumber LIKE '123 123'");
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e e LEFT JOIN groupuser ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.group_id LEFT JOIN phonenumber ON e2.id = p.entity_id WHERE p.phonenumber LIKE '123 123' AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
2006-09-13 01:36:36 +04:00
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(),1);
}
*/
public function testManyToManyFetchingWithColumnAggregationInheritance4() {
2006-09-13 01:36:36 +04:00
$query = new Doctrine_Query();
$query->parseQuery("FROM User.Group WHERE User.Group.name = 'Action Actors'");
$users = $query->execute();
2006-09-13 01:36:36 +04:00
$this->assertEqual($users->count(), 1);
$count = $this->dbh->count();
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual(get_class($users[0]), 'User');
$this->assertEqual($users[0]->Group->count(), 1);
$this->assertEqual($count, $this->dbh->count());
$this->assertEqual($users[0]->Group[0]->name, 'Action Actors');
$this->assertEqual($count, $this->dbh->count());
}
/**
public function testSelectingAggregateValues() {
2006-08-26 03:50:55 +04:00
$q = new Doctrine_Query();
$q->from("User(COUNT(1), MAX(name))");
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array, array(array('COUNT(1)' => '8', 'MAX(e.name)' => 'zYne')));
$this->assertEqual($q->getQuery(), "SELECT COUNT(1), MAX(e.name) FROM entity e e WHERE (e.type = 0)");
$q = new Doctrine_Query();
$q->from("Phonenumber(COUNT(1))");
2006-08-26 03:50:55 +04:00
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array, array(array('COUNT(1)' => '15')));
$this->assertEqual($q->getQuery(), "SELECT COUNT(1) FROM phonenumber p");
$q = new Doctrine_Query();
$q->from("User.Phonenumber(COUNT(id))");
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array[0]['COUNT(p.id)'], 14);
$this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, COUNT(p.id) FROM entity e e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)");
$q = new Doctrine_Query();
$q->select('MAX(u.id), MIN(e.address)')->from("User u INNER JOIN u.Email e");
$coll = $q->execute();
$this->assertEqual($coll->getAggregateValue('MAX(e.id)'), 11);
$this->assertEqual($coll['MIN(email.address)'], 'arnold@example.com');
2006-06-30 03:04:39 +04:00
2006-08-26 03:50:55 +04:00
$q = new Doctrine_Query();
$q->from("User(MAX(id)).Email(MIN(address)), User.Phonenumber(COUNT(1))");
$array = $q->execute();
$this->assertTrue(is_array($array));
2006-06-30 03:04:39 +04:00
$this->assertEqual($array[0]['MAX(e.id)'], 11);
2006-08-26 03:50:55 +04:00
$this->assertEqual($array[0]['MIN(email.address)'], 'arnold@example.com');
$this->assertEqual($array[0]['COUNT(1)'], 14);
2006-09-13 01:36:36 +04:00
//$q = new Doctrine_Query();
//$q->from("User.Phonenumber(COUNT(id))")->groupby("User.id");
//$coll = $q->execute();
//print Doctrine_Lib::formatSql($q->getQuery());
//print_r($coll);
//$this->assertEqual(count($coll), 8);
2006-06-30 03:04:39 +04:00
}
*/
2006-08-16 03:25:53 +04:00
2006-08-26 03:50:55 +04:00
public function testMultipleFetching() {
$count = $this->dbh->count();
2006-08-22 03:20:33 +04:00
$this->connection->getTable('User')->clear();
$this->connection->getTable('Email')->clear();
$this->connection->getTable('Phonenumber')->clear();
$users = $this->query->from("User-l.Phonenumber-i, User-l:Email-i")->execute();
$this->assertEqual(($count + 1),$this->dbh->count());
$this->assertEqual(count($users), 8);
$this->assertEqual($users[0]->Phonenumber->count(), 1);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[0]->Phonenumber[0]->phonenumber, "123 123");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[1]->Phonenumber->count(), 3);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[1]->Phonenumber[0]->phonenumber, "123 123");
$this->assertEqual($users[1]->Phonenumber[1]->phonenumber, "456 456");
$this->assertEqual($users[1]->Phonenumber[2]->phonenumber, "789 789");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[7]->Phonenumber->count(), 1);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[7]->Phonenumber[0]->phonenumber, "111 567 333");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertTrue($users[0]->Email instanceof Email);
$this->assertEqual($users[0]->Email->address, "zYne@example.com");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[0]->email_id, $users[0]->Email->id);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertTrue($users[1]->Email instanceof Email);
$this->assertEqual($users[1]->Email->address, "arnold@example.com");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[1]->email_id, $users[1]->Email->id);
$this->assertEqual(($count + 1), $this->dbh->count());
}
public function testGetPath() {
$this->query->from("User.Group.Email");
$this->assertEqual($this->query->getTableAlias("User"), "e");
$this->assertEqual($this->query->getTableAlias("User.Group"), "e2");
$this->query->from("Task.Subtask.Subtask");
$this->assertEqual($this->query->getTableAlias("Task"), "t");
$this->assertEqual($this->query->getTableAlias("Task.Subtask"), "t2");
$this->assertEqual($this->query->getTableAlias("Task.Subtask.Subtask"), "t3");
2006-06-26 22:55:42 +04:00
$this->assertEqual($this->query->getQuery(),
"SELECT t.id AS t__id, t.name AS t__name, t.parent_id AS t__parent_id, t2.id AS t2__id, t2.name AS t2__name, t2.parent_id AS t2__parent_id, t3.id AS t3__id, t3.name AS t3__name, t3.parent_id AS t3__parent_id FROM task t LEFT JOIN task t2 ON t.id = t2.parent_id LEFT JOIN task t3 ON t2.id = t3.parent_id");
}
2006-06-22 00:02:40 +04:00
public function testMultiComponentFetching2() {
2006-08-22 03:20:33 +04:00
$this->connection->clear();
2006-06-22 00:02:40 +04:00
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-06-22 00:02:40 +04:00
$query->from("User.Email, User.Phonenumber");
2006-06-22 00:02:40 +04:00
$users = $query->execute();
$count = count($this->dbh);
$this->assertEqual($users->count(), 8);
$this->assertTrue($users[0]->Email instanceof Email);
$this->assertEqual($users[0]->Phonenumber->count(), 1);
$this->assertEqual($count, count($this->dbh));
}
public function testHaving() {
2006-08-22 03:20:33 +04:00
$this->connection->clear();
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
$query->from('User-l.Phonenumber-l');
$query->having("COUNT(User.Phonenumber.phonenumber) > 2");
$query->groupby('User.id');
$users = $query->execute();
$this->assertEqual($users->count(), 3);
// test that users are in right order
$this->assertEqual($users[0]->id, 5);
$this->assertEqual($users[1]->id, 8);
$this->assertEqual($users[2]->id, 10);
// test expanding
$count = $this->dbh->count();
$this->assertEqual($users[0]->Phonenumber->count(), 1);
$this->assertEqual($users[1]->Phonenumber->count(), 1);
$this->assertEqual($users[2]->Phonenumber->count(), 1);
$users[0]->Phonenumber[1];
$this->assertEqual(++$count, $this->dbh->count());
$this->assertEqual($users[0]->Phonenumber->count(), 3);
$users[1]->Phonenumber[1];
$this->assertEqual(++$count, $this->dbh->count());
$this->assertEqual($users[1]->Phonenumber->count(), 3);
$users[2]->Phonenumber[1];
$this->assertEqual(++$count, $this->dbh->count());
$this->assertEqual($users[2]->Phonenumber->count(), 3);
2006-06-30 03:04:39 +04:00
2006-08-22 03:20:33 +04:00
$this->connection->clear();
2006-06-30 03:04:39 +04:00
$query->from('User-l.Phonenumber-l');
$query->having("COUNT(User.Phonenumber.phonenumber) > 2");
$query->groupby('User.id');
2006-08-22 03:20:33 +04:00
$users = $this->connection->query("FROM User-l.Phonenumber-l GROUP BY User.id HAVING COUNT(User.Phonenumber.phonenumber) > 2");
2006-06-30 03:04:39 +04:00
$this->assertEqual($users->count(), 3);
// test that users are in right order
$this->assertEqual($users[0]->id, 5);
$this->assertEqual($users[1]->id, 8);
$this->assertEqual($users[2]->id, 10);
}
2006-06-09 02:11:36 +04:00
public function testNestedManyToManyRelations() {
$task = new Task();
$task->name = "T1";
$task->ResourceAlias[0]->name = "R1";
$task->ResourceAlias[1]->name = "R2";
2006-06-09 02:11:36 +04:00
$task->ResourceAlias[0]->Type[0]->type = 'TY1';
//$task->ResourceAlias[1]->Type[0]->type = 'TY2';
$task = new Task();
$task->name = "T2";
$task->ResourceAlias[0]->name = "R3";
2006-06-09 02:11:36 +04:00
$task->ResourceAlias[0]->Type[0]->type = 'TY2';
$task->ResourceAlias[0]->Type[1]->type = 'TY3';
$task->ResourceAlias[1]->name = "R4";
$task->ResourceAlias[2]->name = "R5";
2006-06-09 02:11:36 +04:00
$task->ResourceAlias[2]->Type[0]->type = 'TY4';
$task->ResourceAlias[2]->Type[1]->type = 'TY5';
$task->ResourceAlias[3]->name = "R6";
$this->assertEqual($task->ResourceAlias[0]->name, "R3");
$this->assertEqual($task->ResourceAlias[1]->name, "R4");
$this->assertEqual($task->ResourceAlias[2]->name, "R5");
$this->assertEqual($task->ResourceAlias[3]->name, "R6");
$task = new Task();
$task->name = "T3";
$task->ResourceAlias[0]->name = "R7";
2006-06-09 02:11:36 +04:00
$task = new Task();
$task->name = "T4";
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-06-09 02:11:36 +04:00
2006-08-22 03:20:33 +04:00
$this->connection->clear();
2006-06-09 02:11:36 +04:00
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-06-09 02:11:36 +04:00
$query->from("Task.ResourceAlias.Type");
$tasks = $query->execute();
$this->assertEqual($tasks->count(), 4);
$this->assertEqual($tasks[0]->ResourceAlias->count(), 2);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
$this->assertEqual($tasks[2]->ResourceAlias->count(), 1);
$this->assertEqual($tasks[3]->ResourceAlias->count(), 0);
$this->assertEqual($tasks[0]->ResourceAlias[0]->Type->count(), 1);
$this->assertEqual($tasks[0]->ResourceAlias[1]->Type->count(), 0);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2006-08-22 03:20:33 +04:00
$this->connection->clear();
$query->from("Task")->where("Task.ResourceAlias.Type.type = 'TY2' || Task.ResourceAlias.Type.type = 'TY1'");
$tasks = $query->execute();
$this->assertEqual($tasks->count(),2);
$this->assertEqual(count($this->dbh->query($query->getQuery())->fetchAll(PDO::FETCH_ASSOC)),2);
$this->assertEqual($tasks[0]->name, 'T1');
$this->assertEqual($tasks[1]->name, 'T2');
2006-06-09 02:11:36 +04:00
}
public function testManyToManyFetchingWithColonOperator() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-06-09 02:11:36 +04:00
$task = new Task();
// clear identity maps
2006-08-22 03:20:33 +04:00
$this->connection->getTable('Task')->clear();
$this->connection->getTable('Assignment')->clear();
$this->connection->getTable('Resource')->clear();
$tasks[1] = $task->getTable()->find(2);
$this->assertEqual($tasks[1]->ResourceAlias[0]->name, "R3");
$this->assertEqual($tasks[1]->ResourceAlias[1]->name, "R4");
$this->assertEqual($tasks[1]->ResourceAlias[2]->name, "R5");
$this->assertEqual($tasks[1]->ResourceAlias[3]->name, "R6");
// clear identity maps
$task->getTable()->clear();
2006-08-22 03:20:33 +04:00
$this->connection->getTable('Assignment')->clear();
$this->connection->getTable('Resource')->clear();
$query->from("Task-l:ResourceAlias-l");
$tasks = $query->execute();
$this->assertEqual($tasks->count(), 3);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks instanceof Doctrine_Collection);
$this->assertEqual($tasks[0]->ResourceAlias->count(), 2);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[0]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[1]->ResourceAlias instanceof Doctrine_Collection);
// sanity checking
$this->assertEqual($tasks[1]->ResourceAlias[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[1]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[2]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[3]->getState(), Doctrine_Record::STATE_PROXY);
$count = count($this->dbh);
$this->assertEqual($tasks[1]->ResourceAlias[0]->name, "R3");
$this->assertEqual($tasks[1]->ResourceAlias[1]->name, "R4");
$this->assertEqual($tasks[1]->ResourceAlias[2]->name, "R5");
$this->assertEqual($tasks[1]->ResourceAlias[3]->name, "R6");
$this->assertEqual(count($this->dbh), ($count + 4));
$this->assertEqual($tasks[2]->ResourceAlias->count(), 1);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[2]->ResourceAlias instanceof Doctrine_Collection);
}
public function testManyToManyFetchingWithDotOperator() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-08-22 03:20:33 +04:00
$this->connection->getTable('Task')->clear();
$this->connection->getTable('Assignment')->clear();
$this->connection->getTable('Resource')->clear();
$tasks = $query->query("FROM Task-l.ResourceAlias-l");
$this->assertEqual($tasks->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks instanceof Doctrine_Collection);
$this->assertEqual($tasks[0]->ResourceAlias->count(), 2);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[0]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[1]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[1]->ResourceAlias instanceof Doctrine_Collection);
// sanity checking
$this->assertEqual($tasks[1]->ResourceAlias[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[1]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[2]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($tasks[1]->ResourceAlias[3]->getState(), Doctrine_Record::STATE_PROXY);
$count = count($this->dbh);
$this->assertEqual($tasks[1]->ResourceAlias[0]->name, "R3");
$this->assertEqual($tasks[1]->ResourceAlias[1]->name, "R4");
$this->assertEqual($tasks[1]->ResourceAlias[2]->name, "R5");
$this->assertEqual($tasks[1]->ResourceAlias[3]->name, "R6");
$this->assertEqual(count($this->dbh), ($count + 4));
$this->assertEqual($tasks[2]->ResourceAlias->count(), 1);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[2]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[3]->ResourceAlias->count(), 0);
$this->assertTrue($tasks[3]->ResourceAlias instanceof Doctrine_Collection);
}
2006-06-09 02:11:36 +04:00
public function testManyToManyFetchingWithDotOperatorAndLoadedIdentityMaps() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
$tasks = $query->query("FROM Task-l.ResourceAlias-l");
$this->assertEqual($tasks->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks instanceof Doctrine_Collection);
$this->assertEqual($tasks[0]->ResourceAlias->count(), 2);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[0]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[1]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[1]->ResourceAlias->count(), 4);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[1]->ResourceAlias instanceof Doctrine_Collection);
// sanity checking
$this->assertEqual($tasks[1]->ResourceAlias[0]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($tasks[1]->ResourceAlias[1]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($tasks[1]->ResourceAlias[2]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($tasks[1]->ResourceAlias[3]->getState(), Doctrine_Record::STATE_CLEAN);
$count = count($this->dbh);
$this->assertEqual($tasks[1]->ResourceAlias[0]->name, "R3");
$this->assertEqual($tasks[1]->ResourceAlias[1]->name, "R4");
$this->assertEqual($tasks[1]->ResourceAlias[2]->name, "R5");
$this->assertEqual($tasks[1]->ResourceAlias[3]->name, "R6");
$this->assertEqual(count($this->dbh), $count);
$this->assertEqual($tasks[2]->ResourceAlias->count(), 1);
2007-01-21 21:31:51 +03:00
$this->assertTrue($tasks[2]->ResourceAlias instanceof Doctrine_Collection);
$this->assertEqual($tasks[3]->ResourceAlias->count(), 0);
$this->assertTrue($tasks[3]->ResourceAlias instanceof Doctrine_Collection);
}
2006-06-09 02:11:36 +04:00
2006-06-05 13:57:53 +04:00
public function testOneToOneSharedRelations() {
2006-06-03 13:10:43 +04:00
$status = new Log_Status();
$status->name = 'success';
$entries[0] = new Log_Entry();
$entries[0]->stamp = '2006-06-06';
$entries[0]->Log_Status = $status;
$this->assertTrue($entries[0]->Log_Status instanceof Log_Status);
$entries[1] = new Log_Entry();
$entries[1]->stamp = '2006-06-06';
$entries[1]->Log_Status = $status;
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-06-03 13:10:43 +04:00
// clear the identity maps
$entries[0]->Log_Status->getTable()->clear();
$entries[0]->getTable()->clear();
2006-08-22 03:20:33 +04:00
$entries = $this->connection->query("FROM Log_Entry-I.Log_Status-i");
2006-06-03 13:10:43 +04:00
$this->assertEqual($entries->count(), 2);
$this->assertTrue($entries[0]->Log_Status instanceof Log_Status);
$this->assertEqual($entries[0]->Log_Status->name, 'success');
// the second Log_Status is fetched from identityMap
2006-06-05 13:57:53 +04:00
2006-06-03 13:10:43 +04:00
$this->assertTrue($entries[1]->Log_Status instanceof Log_Status);
$this->assertEqual($entries[1]->Log_Status->name, 'success');
// the following line is possible since doctrine uses identityMap
2007-02-16 01:25:26 +03:00
$this->assertEqual($entries[0]->Log_Status->getOID(), $entries[1]->Log_Status->getOID());
2006-06-03 13:10:43 +04:00
$entries[0]->Log_Status->delete();
2007-02-16 01:25:26 +03:00
$this->assertEqual($entries[0]->Log_Status->getOID(), $entries[1]->Log_Status->getOID());
2006-06-03 13:10:43 +04:00
$this->assertEqual($entries[0]->Log_Status->getState(), Doctrine_Record::STATE_TCLEAN);
2006-06-03 13:10:43 +04:00
// clear the identity maps
$entries[0]->Log_Status->getTable()->clear();
$entries[0]->getTable()->clear();
2006-08-22 03:20:33 +04:00
$entries = $this->connection->query("FROM Log_Entry-I.Log_Status-i");
2006-06-03 13:10:43 +04:00
$this->assertEqual($entries->count(), 2);
$this->assertTrue($entries[0]->Log_Status instanceof Log_Status);
2006-06-03 13:10:43 +04:00
$this->assertEqual($entries[0]->Log_Status->name, null);
$this->assertEqual($entries[1]->Log_Status->name, null);
2006-06-03 13:10:43 +04:00
}
2006-06-03 13:10:43 +04:00
public function testOneToOneRelationFetchingWithCustomTableNames() {
$entry = new ORM_TestEntry();
$entry->name = 'entry 1';
$entry->amount = '123.123';
$entry->ORM_TestItem->name = 'item 1';
$entry = new ORM_TestEntry();
$entry->name = 'entry 2';
$entry->amount = '123.123';
$entry->ORM_TestItem->name = 'item 2';
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-06-03 13:10:43 +04:00
$count = $this->dbh->count();
2006-08-22 03:20:33 +04:00
$entries = $this->connection->query("FROM ORM_TestEntry-i.ORM_TestItem-i");
2006-06-03 13:10:43 +04:00
$this->assertEqual($entries->count(), 2);
$this->assertTrue($entries[0] instanceof ORM_TestEntry);
$this->assertTrue($entries[0]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[0]->name, 'entry 1');
$this->assertTrue($entries[1] instanceof ORM_TestEntry);
$this->assertTrue($entries[1]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[1]->name, 'entry 2');
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertTrue($entries[0]->ORM_TestItem instanceof ORM_TestItem);
$this->assertEqual($entries[0]->ORM_TestItem->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[0]->ORM_TestItem->name, 'item 1');
$this->assertTrue($entries[1]->ORM_TestItem instanceof ORM_TestItem);
$this->assertEqual($entries[1]->ORM_TestItem->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($entries[1]->ORM_TestItem->name, 'item 2');
$this->assertEqual(($count + 1), $this->dbh->count());
}
2006-05-31 12:46:44 +04:00
public function testImmediateFetching() {
$count = $this->dbh->count();
2006-08-22 03:20:33 +04:00
$this->connection->getTable('User')->clear();
$this->connection->getTable('Email')->clear();
$this->connection->getTable('Phonenumber')->clear();
2006-05-31 12:46:44 +04:00
$users = $this->query->from("User-i.Email-i")->execute();
$this->assertEqual(($count + 1),$this->dbh->count());
$this->assertEqual(count($users), 8);
$this->assertEqual(get_class($users[0]->Email), 'Email');
$this->assertEqual(($count + 1),$this->dbh->count());
$this->assertEqual($users[0]->Email->address, 'zYne@example.com');
$this->assertEqual(($count + 1),$this->dbh->count());
2006-06-01 15:58:05 +04:00
$this->assertEqual(get_class($users[1]->Email), 'Email');
$this->assertEqual(($count + 1),$this->dbh->count());
$this->assertEqual($users[1]->Email->address, 'arnold@example.com');
$this->assertEqual(($count + 1),$this->dbh->count());
2006-05-31 12:46:44 +04:00
}
public function testLazyPropertyFetchingWithMultipleColumns() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
$q->from("User-l(name, email_id)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
2007-01-21 21:31:51 +03:00
$this->assertTrue($users instanceof Doctrine_Collection);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count, count($this->dbh));
$users[0]->getTable()->clear();
$q->from("User-b(name, email_id)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
2007-01-21 21:31:51 +03:00
$this->assertTrue($users instanceof Doctrine_Collection);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count, count($this->dbh));
$this->assertTrue(is_numeric($users[1]->email_id));
$this->assertEqual($count, count($this->dbh));
$this->assertTrue(is_numeric($users[2]->email_id));
$this->assertEqual($count, count($this->dbh));
$q->from("User-b(name, email_id):Email, User-b(name, email_id).Phonenumber");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection_Batch);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count, count($this->dbh));
$this->assertTrue(is_numeric($users[1]->email_id));
$this->assertEqual($count, count($this->dbh));
$this->assertTrue(is_numeric($users[2]->email_id));
$this->assertEqual($count, count($this->dbh));
}
2006-05-31 02:35:00 +04:00
public function testForeignKeyRelationFetching() {
$count = $this->dbh->count();
$users = $this->query->from("User-l.Phonenumber-i")->execute();
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual(count($users), 8);
$this->assertEqual($users[0]->Phonenumber->count(), 1);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[0]->Phonenumber[0]->phonenumber, "123 123");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[1]->Phonenumber->count(), 3);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[1]->Phonenumber[0]->phonenumber, "123 123");
$this->assertEqual($users[1]->Phonenumber[1]->phonenumber, "456 456");
$this->assertEqual($users[1]->Phonenumber[2]->phonenumber, "789 789");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[7]->Phonenumber->count(), 1);
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[7]->Phonenumber[0]->phonenumber, "111 567 333");
$this->assertEqual(($count + 1), $this->dbh->count());
$this->assertEqual($users[0]->name, "zYne");
$this->assertEqual(($count + 2), $this->dbh->count());
}
public function testOneToOneRelationFetching() {
$count = $this->dbh->count();
$users = $this->query->from("User-l:Email-i")->execute();
$this->assertEqual(($count + 1), $this->dbh->count());
2007-01-21 21:31:51 +03:00
$this->assertTrue($users instanceof Doctrine_Collection);
$count = $this->dbh->count();
foreach($users as $user) {
// iterate through users and test that no additional queries are needed
$user->Email->address;
$this->assertEqual($count, $this->dbh->count());
}
$users[0]->Account->amount = 3000;
$this->assertEqual($users[0]->Account->amount, 3000);
$this->assertTrue($users[0]->Account instanceof Account);
$this->assertEqual($users[0]->id, $users[0]->Account->entity_id);
$users[0]->Account->save();
$users[0]->refresh();
$this->assertEqual($users[0]->Account->amount, 3000);
$this->assertTrue($users[0]->Account instanceof Account);
$this->assertEqual($users[0]->id, $users[0]->Account->entity_id);
$this->assertEqual($users[0]->Account->getState(), Doctrine_Record::STATE_CLEAN);
$users[0]->getTable()->clear();
$users[0]->Account->getTable()->clear();
$count = $this->dbh->count();
$users = $this->query->from("User-l:Account-i")->execute();
$this->assertEqual(($count + 1), $this->dbh->count());
2007-01-21 21:31:51 +03:00
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertEqual($users->count(), 1);
$this->assertEqual($users[0]->Account->amount,3000);
}
public function testQueryWithComplexAliases() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-05-19 14:22:15 +04:00
$board = new Forum_Board();
$table = $board->getTable();
$fk = $table->getRelation("Threads");
2006-05-19 14:22:15 +04:00
$this->assertEqual($table->getComponentName(), "Forum_Board");
$this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
2006-05-19 14:22:15 +04:00
$this->assertEqual($fk->getTable()->getComponentName(), "Forum_Thread");
$entry = new Forum_Entry();
$this->assertTrue($entry->getTable()->getRelation("Thread") instanceof Doctrine_Relation_LocalKey);
$board->name = "Doctrine Forum";
$board->Threads[0];
$board->Category->name = "General discussion";
$this->assertEqual($board->name, "Doctrine Forum");
$this->assertEqual($board->Category->name, "General discussion");
$this->assertEqual($board->Category->getState(), Doctrine_Record::STATE_TDIRTY);
2006-05-19 14:22:15 +04:00
$this->assertEqual($board->Threads[0]->getState(), Doctrine_Record::STATE_TDIRTY);
$this->assertTrue($board->Threads[0] instanceof Forum_Thread);
2006-05-19 14:22:15 +04:00
$thread = $board->Threads[0];
$thread->Entries[0]->topic = "My first topic";
$thread->Entries[1]->topic = "My second topic";
2006-05-19 14:22:15 +04:00
$this->assertEqual($thread->Entries[0]->topic, "My first topic");
$this->assertEqual($thread->Entries[0]->getState(), Doctrine_Record::STATE_TDIRTY);
$this->assertTrue($thread->Entries[0] instanceof Forum_Entry);
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-05-25 16:25:26 +04:00
$board->getTable()->clear();
$board = $board->getTable()->find($board->id);
$this->assertEqual($board->Threads->count(), 1);
$this->assertEqual($board->name, "Doctrine Forum");
$this->assertEqual($board->Category->name, "General discussion");
2006-05-19 14:22:15 +04:00
$this->assertEqual($board->Category->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($board->Threads[0]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertTrue($board->Threads[0] instanceof Forum_Thread);
$this->assertEqual($board->Threads[0]->Entries->count(), 2);
2006-05-19 14:22:15 +04:00
2006-05-19 14:22:15 +04:00
$q->from("Forum_Board");
$coll = $q->execute();
$this->assertEqual($coll->count(), 1);
2006-08-22 03:20:33 +04:00
$table = $this->connection->getTable("Forum_Board")->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY);
$table = $this->connection->getTable("Forum_Thread")->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY);
2006-05-25 16:25:26 +04:00
$q->from("Forum_Board.Threads");
$this->assertEqual($q->getQuery(), "SELECT f.id AS f__id, f2.id AS f2__id FROM forum__board f LEFT JOIN forum__thread f2 ON f.id = f2.board_id");
2006-05-19 14:22:15 +04:00
$coll = $q->execute();
$this->assertEqual($coll->count(), 1);
2006-05-25 16:25:26 +04:00
$q->from("Forum_Board-l.Threads-l");
$this->assertEqual($q->getQuery(), "SELECT f.id AS f__id, f2.id AS f2__id FROM forum__board f LEFT JOIN forum__thread f2 ON f.id = f2.board_id");
2006-08-22 03:20:33 +04:00
//$this->connection->clear();
$q->from("Forum_Board-l.Threads-l.Entries-l");
$this->assertEqual($q->getQuery(), "SELECT f.id AS f__id, f2.id AS f2__id, f3.id AS f3__id FROM forum__board f LEFT JOIN forum__thread f2 ON f.id = f2.board_id LEFT JOIN forum__entry f3 ON f2.id = f3.thread_id");
$boards = $q->execute();
$this->assertEqual($boards->count(), 1);
$count = count($this->dbh);
$this->assertEqual($boards[0]->Threads->count(), 1);
$this->assertEqual(count($this->dbh), $count);
$this->assertEqual($boards[0]->Threads[0]->Entries->count(), 2);
2006-05-25 16:32:57 +04:00
$q->from("Forum_Board-l.Threads-l.Entries-i");
$this->assertEqual($boards->count(), 1);
$count = count($this->dbh);
$this->assertEqual($boards[0]->Threads->count(), 1);
$this->assertEqual(count($this->dbh), $count);
$this->assertEqual($boards[0]->Threads[0]->Entries->count(), 2);
}
2006-05-17 21:58:40 +04:00
public function testQueryWithAliases() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-05-25 16:25:26 +04:00
2006-05-17 21:58:40 +04:00
$task = new Task();
$task->name = "Task 1";
$task->ResourceAlias[0]->name = "Resource 1";
$task->ResourceAlias[1]->name = "Resource 2";
$task->save();
2006-05-25 16:25:26 +04:00
2006-05-17 21:58:40 +04:00
$coll = $query->query("FROM Task WHERE Task.ResourceAlias.name = 'Resource 1'");
$this->assertEqual($coll->count(), 1);
$this->assertTrue($coll[0] instanceof Task);
}
2006-05-10 01:39:07 +04:00
public function testQueryArgs() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-05-10 01:39:07 +04:00
$query = $query->from("User-l");
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("from"), "entity e");
2006-05-10 01:39:07 +04:00
$query = $query->orderby("User.name");
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("orderby"), array("e.name"));
2006-05-10 01:39:07 +04:00
$query = $query->orderby("User.created");
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("orderby"), array("e.created"));
2006-05-10 01:39:07 +04:00
$query = $query->where("User.name LIKE 'zYne%'");
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("where"), array("e.name LIKE 'zYne%'"));
2006-05-10 01:39:07 +04:00
$query = $query->where("User.name LIKE 'Arnold%'");
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("where"), array("e.name LIKE 'Arnold%'"));
2006-05-10 01:39:07 +04:00
$query = $query->limit(5);
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("limit"), 5);
$query = $query->offset(5);
$this->assertTrue($query instanceof Doctrine_Query);
$this->assertEqual($query->get("offset"), 5);
2006-08-16 13:21:47 +04:00
$query->remove('limit')->remove('offset');
2006-10-19 00:17:49 +04:00
$this->assertFalse(strpos($query->getQuery(),"OFFSET"));
2006-05-10 01:39:07 +04:00
$this->assertFalse(strpos($query->getQuery(),"LIMIT"));
$coll = $query->execute();
2007-01-21 21:31:51 +03:00
$this->assertTrue($coll instanceof Doctrine_Collection);
2006-05-10 01:39:07 +04:00
$this->assertEqual($coll->count(), 1);
$query->where("User.name LIKE ?")->limit(3);
$this->assertEqual($query->limit, 3);
$this->assertTrue(strpos($query->getQuery(),"LIMIT"));
}
public function testLimit() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-08-16 03:25:53 +04:00
$coll = $query->query("FROM User(id) LIMIT 3");
2006-05-10 01:39:07 +04:00
$this->assertEqual($query->limit, 3);
$this->assertEqual($coll->count(), 3);
}
public function testOffset() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-05-10 01:39:07 +04:00
$coll = $query->query("FROM User LIMIT 3 OFFSET 3");
$this->assertEqual($query->offset, 3);
$this->assertEqual($coll->count(), 3);
}
public function testPreparedQuery() {
2006-08-22 03:20:33 +04:00
$coll = $this->connection->query("FROM User WHERE User.name = :name", array(":name" => "zYne"));
2006-05-10 01:39:07 +04:00
$this->assertEqual($coll->count(), 1);
}
public function testOrderBy() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
$query->from("User-b")->orderby("User.name ASC, User.Email.address");
$users = $query->execute();
2006-05-25 16:25:26 +04:00
2006-05-10 01:39:07 +04:00
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e LEFT JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) ORDER BY e.name ASC, e2.address");
2006-05-10 01:39:07 +04:00
$this->assertEqual($users->count(),8);
$this->assertTrue($users[0]->name == "Arnold Schwarzenegger");
}
public function testBatchFetching() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
$users = $query->query("FROM User-b");
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e WHERE (e.type = 0)");
$this->assertEqual($users[0]->name, "zYne");
$this->assertTrue($users instanceof Doctrine_Collection_Batch);
}
public function testLazyFetching() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
$users = $query->query("FROM User-l");
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e WHERE (e.type = 0)");
$this->assertEqual($users[0]->name, "zYne");
2007-01-21 21:31:51 +03:00
$this->assertTrue($users instanceof Doctrine_Collection);
}
2006-05-17 21:58:40 +04:00
public function testFetchingWithCollectionExpanding() {
2006-05-10 01:39:07 +04:00
// DYNAMIC COLLECTION EXPANDING
2006-08-22 03:20:33 +04:00
$query = new Doctrine_Query($this->connection);
2006-05-10 01:39:07 +04:00
$user = $this->objTable->find(5);
$user->Group[1]->name = "Tough guys inc.";
$user->Group[2]->name = "Terminators";
$this->assertEqual($user->Group[0]->name, "Action Actors");
$user->save();
$this->assertEqual($user->Group[0]->name, "Action Actors");
$this->assertEqual(count($user->Group), 3);
$user = $this->objTable->find(5);
$this->assertEqual(count($user->Group), 3);
//$users = $query->query("FROM User, User.Group WHERE User.Group.name LIKE 'Action Actors'");
//$this->assertEqual(count($users),1);
//$this->assertEqual($users[0]->Group[0]->name, "Action Actors");
//$this->assertEqual(count($users[0]->Group), 1);
//$this->assertEqual($users[0]->Group[1]->name, "Tough guys inc.");
//$this->assertEqual($users[0]->Group[2]->name, "Terminators");
//$this->assertEqual(count($users[0]->Group), 3);
2006-08-22 03:20:33 +04:00
$this->connection->getTable("User")->clear();
$this->connection->getTable("Phonenumber")->clear();
2006-05-10 01:39:07 +04:00
2006-05-25 16:25:26 +04:00
$users = $query->query("FROM User-b.Phonenumber-l WHERE User.Phonenumber.phonenumber LIKE '%123%'");
2006-05-10 01:39:07 +04:00
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE p.phonenumber LIKE '%123%' AND (e.type = 0)");
2006-05-10 01:39:07 +04:00
2006-08-22 03:20:33 +04:00
$count = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
$users[1]->Phonenumber[0]->phonenumber;
$users[1]->Phonenumber[1]->phonenumber;
$this->assertEqual($users[1]->Phonenumber[1]->getState(),Doctrine_Record::STATE_CLEAN);
$users[1]->Phonenumber[2]->phonenumber;
$this->assertEqual($users[1]->Phonenumber[1]->getState(),Doctrine_Record::STATE_CLEAN);
2006-08-22 03:20:33 +04:00
$count2 = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
$this->assertEqual($count + 4,$count2);
// DYNAMIC FETCHMODES
$e = false;
try {
$users = $query->query("FROM User-unknown");
} catch(Exception $e) {
}
$this->assertTrue($e instanceof Doctrine_Query_Exception);
2006-05-10 01:39:07 +04:00
$users = $query->query("FROM User-i");
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE (e.type = 0)");
2006-05-10 01:39:07 +04:00
2006-08-22 03:20:33 +04:00
$count = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
$this->assertEqual($users[0]->name, "zYne");
$this->assertTrue($users instanceof Doctrine_Collection_Immediate);
2006-08-22 03:20:33 +04:00
$count2 = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
//$this->clearCache();
2006-05-25 16:25:26 +04:00
$users = $query->query("FROM User-b.Phonenumber-b");
2006-05-10 01:39:07 +04:00
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)");
2006-05-10 01:39:07 +04:00
$this->assertEqual($users->count(),8);
// EXPECTED THAT ONE NEW QUERY IS NEEDED TO GET THE FIRST USER's PHONENUMBER
2006-08-22 03:20:33 +04:00
$count = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
$users[0]->Phonenumber[0]->phonenumber;
2006-08-22 03:20:33 +04:00
$count2 = $this->connection->getDBH()->count();
2006-05-10 01:39:07 +04:00
$this->assertEqual($count + 1,$count2);
2006-05-25 16:25:26 +04:00
$users = $query->query("FROM User-l:Email-b");
2006-05-10 01:39:07 +04:00
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id, e2.id AS e2__id FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)");
2006-05-10 01:39:07 +04:00
$this->assertEqual($users->count(),8);
$users = $query->query("FROM Email-b WHERE Email.address LIKE '%@example%'");
2006-05-10 01:39:07 +04:00
$this->assertEqual($query->getQuery(),
"SELECT e.id AS e__id FROM email e WHERE e.address LIKE '%@example%'");
2006-05-10 01:39:07 +04:00
$this->assertEqual($users->count(),8);
$users = $query->query("FROM User-b WHERE User.name LIKE '%Jack%'");
$this->assertEqual($query->getQuery(), "SELECT e.id AS e__id FROM entity e WHERE e.name LIKE '%Jack%' AND (e.type = 0)");
2006-05-10 01:39:07 +04:00
$this->assertEqual($users->count(),0);
2006-06-25 22:34:53 +04:00
$users = $query->query("FROM User-b WHERE User.Phonenumber.phonenumber LIKE '%123%'");
2006-05-10 01:39:07 +04:00
$this->assertEqual(trim($query->getQuery()),
"SELECT e.id AS e__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE p.phonenumber LIKE '%123%' AND (e.type = 0)");
2006-06-25 22:34:53 +04:00
$this->assertEqual($users->count(),5);
2006-05-10 01:39:07 +04:00
2006-05-27 00:15:27 +04:00
//$values = $query->query("SELECT COUNT(User.name) AS users, MAX(User.name) AS max FROM User");
//$this->assertEqual(trim($query->getQuery()),"SELECT COUNT(e.name) AS users, MAX(e.name) AS max FROM entity e e WHERE (e.type = 0)");
2006-05-27 00:15:27 +04:00
//$this->assertTrue(is_array($values));
//$this->assertTrue(isset($values['users']));
//$this->assertTrue(isset($values['max']));
2006-05-25 16:25:26 +04:00
}
2006-05-10 01:39:07 +04:00
}
?>