From 4f81ab61b2aa0ea0ae84bb90f182e6c9e092cb78 Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Thu, 10 Jan 2008 20:02:41 +0000 Subject: [PATCH] Added support to leftJoin and innerJoin parameters. Included some tests that failed due to an existent Doctrine bug. --- tests/Query/JoinCondition2TestCase.php | 170 +++++++++++++++++++++++++ tests/run.php | 1 + 2 files changed, 171 insertions(+) create mode 100755 tests/Query/JoinCondition2TestCase.php diff --git a/tests/Query/JoinCondition2TestCase.php b/tests/Query/JoinCondition2TestCase.php new file mode 100755 index 000000000..28eaa56c9 --- /dev/null +++ b/tests/Query/JoinCondition2TestCase.php @@ -0,0 +1,170 @@ +. + */ + +/** + * Doctrine_Query_JoinCondition2_TestCase + * + * @package Doctrine + * @author Guilherme Blanco + * @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_JoinCondition2_TestCase extends Doctrine_UnitTestCase +{ + public function prepareTables() + { + $this->tables[] = 'Entity'; + $this->tables[] = 'User'; + $this->tables[] = 'GroupUser'; + $this->tables[] = 'Group'; + parent::prepareTables(); + } + + + public function prepareData() + { + parent::prepareData(); + + $groups = new Doctrine_Collection('Group'); + + $groups[0]->name = 'PHP Users'; + + $groups[1]->name = 'Developers'; + + $groups->save(); + + $zYne = Doctrine_Query::create()->from('User u')->where('u.id = ?', 4)->fetchOne(); + $zYne->Group[0] = $groups[0]; + $zYne->Group[1] = $groups[1]; + $zYne->save(); + } + + + public function testJoinCondifitionsRawLeftJoins() + { + $q = Doctrine_Query::create(); + + $q->select('u.id')->from('User u')->leftJoin('u.Group g WITH g.id = 12')->where('u.id = 4'); + + $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.id = 12 WHERE e.id = 4 AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))'); + + $rs = $q->execute(); + + // Should only find zYne + $this->assertEqual($rs->count(), 1); + + // Grab the number of runned queries + $queryCount = $this->connection->count(); + + // Only one Group fetched for zYne + $this->assertEqual($rs[0]->Group->count(), 1); + + // Check if it executed any other query + $bug = ($this->connection->count() - $queryCount); + + // Should return 0 (no more queries executed) + $this->assertEqual($bug, 0); + } + + + public function testJoinCondifitionsArgumentsLeftJoins() + { + $q = new Doctrine_Query($this->connection); + + $q->select('u.id')->from('User u')->leftJoin('u.Group g WITH g.id = ?', 12)->where('u.id = ?', 4); + + $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id FROM entity e LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.id = ? WHERE e.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))'); + + $rs = $q->execute(); + + // Should only find zYne + $this->assertEqual($rs->count(), 1); + + // Grab the number of runned queries + $queryCount = $this->connection->count(); + + // Only one Group fetched for zYne + $this->assertEqual($rs[0]->Group->count(), 1); + + // Check if it executed any other query + $bug = ($this->connection->count() - $queryCount); + + // Should return 0 (no more queries executed) + $this->assertEqual($bug, 0); + } + + + public function testJoinCondifitionsRawInnerJoins() + { + $q = new Doctrine_Query($this->connection); + + $q->select('u.id')->from('User u')->innerJoin('u.Group g WITH g.id = 12')->where('u.id = 4'); + + $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id AND e2.id = 12 WHERE e.id = 4 AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))'); + + $rs = $q->execute(); + + // Should only find zYne + $this->assertEqual($rs->count(), 1); + + // Grab the number of runned queries + $queryCount = $this->connection->count(); + + // Only one Group fetched for zYne + $this->assertEqual($rs[0]->Group->count(), 1); + + // Check if it executed any other query + $bug = ($this->connection->count() - $queryCount); + + // Should return 0 (no more queries executed) + $this->assertEqual($bug, 0); + } + + + public function testJoinCondifitionsArgumentsInnerJoins() + { + $q = new Doctrine_Query($this->connection); + + $q->select('u.id')->from('User u')->innerJoin('u.Group g WITH g.id = ?', 12)->where('u.id = ?', 4); + + $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id AND e2.id = ? WHERE e.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))'); + + $rs = $q->execute(); + + // Should only find zYne + $this->assertEqual($rs->count(), 1); + + // Grab the number of runned queries + $queryCount = $this->connection->count(); + + // Only one Group fetched for zYne + $this->assertEqual($rs[0]->Group->count(), 1); + + // Check if it executed any other query + $bug = ($this->connection->count() - $queryCount); + + // Should return 0 (no more queries executed) + $this->assertEqual($bug, 0); + } +} diff --git a/tests/run.php b/tests/run.php index 8f72a0e80..981307e6f 100644 --- a/tests/run.php +++ b/tests/run.php @@ -203,6 +203,7 @@ $query_tests->addTestCase(new Doctrine_Query_Where_TestCase()); $query_tests->addTestCase(new Doctrine_Query_From_TestCase()); $query_tests->addTestCase(new Doctrine_Query_Select_TestCase()); $query_tests->addTestCase(new Doctrine_Query_JoinCondition_TestCase()); +$query_tests->addTestCase(new Doctrine_Query_JoinCondition2_TestCase()); $query_tests->addTestCase(new Doctrine_Query_MultipleAggregateValue_TestCase()); $query_tests->addTestCase(new Doctrine_Query_TestCase()); $query_tests->addTestCase(new Doctrine_Query_MysqlSubquery_TestCase());