From dfbc7c1cffce92a36ff2dcc763142ee5f0d9bdd1 Mon Sep 17 00:00:00 2001 From: guilhermeblanco Date: Tue, 27 May 2008 04:52:50 +0000 Subject: [PATCH] Changed Doctrine_Query to accept a Doctrine_EntityManager instead of a Doctrine_Connection. Updated Doctrine_EntityManager. Updated test cases and included one new passing test case for SELECT generation. Fixed whitespace bug in DELETE and UPDATE statements. --- lib/Doctrine/EntityManager.php | 2 +- lib/Doctrine/Query.php | 51 +--- lib/Doctrine/Query/Parser.php | 6 +- lib/Doctrine/Query/ParserResult.php | 2 +- .../Query/Production/DeleteStatement.php | 2 +- .../Query/Production/UpdateStatement.php | 2 +- lib/Doctrine/Query/Production/WhereClause.php | 2 +- lib/Doctrine/Query/SqlBuilder.php | 6 +- tests/Orm/Query/DeleteSqlGenerationTest.php | 258 ++++++++-------- tests/Orm/Query/DqlGenerationTest.php | 285 +++++++++--------- tests/Orm/Query/IdentifierRecognitionTest.php | 16 +- tests/Orm/Query/LanguageRecognitionTest.php | 19 +- tests/Orm/Query/SelectSqlGenerationTest.php | 46 ++- tests/Orm/Query/UpdateSqlGenerationTest.php | 31 +- 14 files changed, 365 insertions(+), 363 deletions(-) diff --git a/lib/Doctrine/EntityManager.php b/lib/Doctrine/EntityManager.php index e846a3f7e..5e7dd4f57 100644 --- a/lib/Doctrine/EntityManager.php +++ b/lib/Doctrine/EntityManager.php @@ -246,7 +246,7 @@ class Doctrine_EntityManager { $query = new Doctrine_Query($this); if ( ! empty($dql)) { - $query->parseQuery($dql); + $query->setDql($dql); } return $query; diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 6950039c6..d7367656a 100755 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -37,9 +37,9 @@ class Doctrine_Query extends Doctrine_Query_Abstract { /** - * @var Doctrine_Connection The connection used by this query object. + * @var Doctrine_EntityManager The entity manager used by this query object. */ - protected $_connection; + protected $_entityManager; /** * @var Doctrine_Hydrator The hydrator object used to hydrate query results. @@ -93,15 +93,10 @@ class Doctrine_Query extends Doctrine_Query_Abstract // End of Caching Stuff - public function __construct(Doctrine_Connection $conn = null, Doctrine_Hydrator_Abstract $hydrator = null) + public function __construct(Doctrine_EntityManager $entityManager) { - $this->setConnection($conn); - - if ($hydrator === null) { - $hydrator = new Doctrine_Hydrator(Doctrine_EntityManager::getManager()); - } - - $this->_hydrator = $hydrator; + $this->_entityManager = $entityManager; + $this->_hydrator = new Doctrine_Hydrator($entityManager); $this->free(); } @@ -120,29 +115,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract /** - * Retrieves the assocated Doctrine_Connection to this Doctrine_Query + * Retrieves the assocated Doctrine_EntityManager to this Doctrine_Query * - * @return Doctrine_Connection + * @return Doctrine_EntityManager */ - public function getConnection() + public function getEntityManager() { - return $this->_connection; - } - - - /** - * Defines an assocated Doctrine_Connection to this Doctrine_Query - * - * @param Doctrine_Connection $conn A valid Doctrine_Connection - * @return void - */ - public function setConnection(Doctrine_Connection $conn = null) - { - if ($conn === null) { - $conn = Doctrine_EntityManager::getManager()->getConnection(); - } - - $this->_connection = $conn; + return $this->_entityManager; } @@ -230,7 +209,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract public function parse() { if ($this->_state === self::STATE_DIRTY) { - $parser = new Doctrine_Query_Parser($this->getDql()); + $parser = new Doctrine_Query_Parser($this); $this->_parserResult = $parser->parse(); $this->_state = self::STATE_CLEAN; } @@ -313,7 +292,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract protected function _execute2($params) { // If there is a CacheDriver associated to cache queries... - if ($this->_queryCache || $this->_connection->getAttribute(Doctrine::ATTR_QUERY_CACHE)) { + if ($this->_queryCache || $this->_entityManager->getConnection()->getAttribute(Doctrine::ATTR_QUERY_CACHE)) { $queryCacheDriver = $this->getQueryCacheDriver(); // Calculate hash for dql query. @@ -347,7 +326,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract // Double the params if we are using limit-subquery algorithm // We always have an instance of Doctrine_Query_ParserResult on hands... if ($this->_parserResult->isLimitSubqueryUsed() && - $this->_connection->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') { + $this->_entityManager->getConnection()->getAttribute(Doctrine::ATTR_DRIVER_NAME) !== 'mysql') { $params = array_merge($params, $params); } @@ -362,7 +341,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract protected function _prepareParams(array $params) { // Convert boolean params - $params = $this->_connection->convertBooleans($params); + $params = $this->_entityManager->getConnection()->convertBooleans($params); // Convert enum params return $this->convertEnums($params); @@ -399,7 +378,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract if ($this->_resultCache instanceof Doctrine_Cache_Interface) { return $this->_resultCache; } else { - return $this->_connection->getResultCacheDriver(); + return $this->_entityManager->getConnection()->getResultCacheDriver(); } } @@ -488,7 +467,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract if ($this->_queryCache instanceof Doctrine_Cache_Interface) { return $this->_queryCache; } else { - return $this->_connection->getQueryCacheDriver(); + return $this->_entityManager->getConnection()->getQueryCacheDriver(); } } diff --git a/lib/Doctrine/Query/Parser.php b/lib/Doctrine/Query/Parser.php index c5fafa1bd..be417c54f 100644 --- a/lib/Doctrine/Query/Parser.php +++ b/lib/Doctrine/Query/Parser.php @@ -111,10 +111,10 @@ class Doctrine_Query_Parser * @param string $dql DQL to be parsed. * @param Doctrine_Connection $connection The connection to use */ - public function __construct($dql, Doctrine_Connection $connection = null) + public function __construct(Doctrine_Query $query) { - $this->_scanner = new Doctrine_Query_Scanner($dql); - $this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($connection); + $this->_scanner = new Doctrine_Query_Scanner($query->getDql()); + $this->_sqlBuilder = Doctrine_Query_SqlBuilder::fromConnection($query->getEntityManager()); $this->_keywordTable = new Doctrine_Query_Token(); $this->_parserResult = new Doctrine_Query_ParserResult( diff --git a/lib/Doctrine/Query/ParserResult.php b/lib/Doctrine/Query/ParserResult.php index dd43926e3..d91293a7c 100755 --- a/lib/Doctrine/Query/ParserResult.php +++ b/lib/Doctrine/Query/ParserResult.php @@ -74,7 +74,7 @@ class Doctrine_Query_ParserResult extends Doctrine_Query_AbstractResult * * @param array $queryFields Query fields. */ - public function setQueryComponents(array $queryFields) + public function setQueryFields(array $queryFields) { $this->_queryFields = $queryFields; } diff --git a/lib/Doctrine/Query/Production/DeleteStatement.php b/lib/Doctrine/Query/Production/DeleteStatement.php index 8236f30ac..fdbc0cf3b 100644 --- a/lib/Doctrine/Query/Production/DeleteStatement.php +++ b/lib/Doctrine/Query/Production/DeleteStatement.php @@ -54,6 +54,6 @@ class Doctrine_Query_Production_DeleteStatement extends Doctrine_Query_Productio // The 1=1 is needed to workaround the affected_rows in MySQL. // Simple "DELETE FROM table_name" gives 0 affected rows. return $this->_deleteClause->buildSql() . (($this->_whereClause !== null) - ? $this->_whereClause->buildSql() : ' WHERE 1 = 1'); + ? ' ' . $this->_whereClause->buildSql() : ' WHERE 1 = 1'); } } diff --git a/lib/Doctrine/Query/Production/UpdateStatement.php b/lib/Doctrine/Query/Production/UpdateStatement.php index 3da5925f6..25cff2a95 100644 --- a/lib/Doctrine/Query/Production/UpdateStatement.php +++ b/lib/Doctrine/Query/Production/UpdateStatement.php @@ -54,6 +54,6 @@ class Doctrine_Query_Production_UpdateStatement extends Doctrine_Query_Productio // The 1=1 is needed to workaround the affected_rows in MySQL. // Simple "UPDATE table_name SET column_name = value" gives 0 affected rows. return $this->_updateClause->buildSql() . (($this->_whereClause !== null) - ? $this->_whereClause->buildSql() : ' WHERE 1 = 1'); + ? ' ' . $this->_whereClause->buildSql() : ' WHERE 1 = 1'); } } diff --git a/lib/Doctrine/Query/Production/WhereClause.php b/lib/Doctrine/Query/Production/WhereClause.php index 93134987b..c683da523 100644 --- a/lib/Doctrine/Query/Production/WhereClause.php +++ b/lib/Doctrine/Query/Production/WhereClause.php @@ -47,6 +47,6 @@ class Doctrine_Query_Production_WhereClause extends Doctrine_Query_Production public function buildSql() { - return ' WHERE ' . $this->_conditionalExpression->buildSql(); + return 'WHERE ' . $this->_conditionalExpression->buildSql(); } } diff --git a/lib/Doctrine/Query/SqlBuilder.php b/lib/Doctrine/Query/SqlBuilder.php index aea429f38..48dfbc1a8 100755 --- a/lib/Doctrine/Query/SqlBuilder.php +++ b/lib/Doctrine/Query/SqlBuilder.php @@ -42,11 +42,9 @@ abstract class Doctrine_Query_SqlBuilder protected $_connection; - public static function fromConnection(Doctrine_Connection $connection = null) + public static function fromConnection(Doctrine_EntityManager $entityManager) { - if ($connection === null) { - $connection = Doctrine_EntityManager::getManager()->getConnection(); - } + $connection = $entityManager->getConnection(); $className = "Doctrine_Query_SqlBuilder_" . $connection->getDriverName(); $sqlBuilder = new $className(); diff --git a/tests/Orm/Query/DeleteSqlGenerationTest.php b/tests/Orm/Query/DeleteSqlGenerationTest.php index 2d8aa71d6..65c13c7ea 100755 --- a/tests/Orm/Query/DeleteSqlGenerationTest.php +++ b/tests/Orm/Query/DeleteSqlGenerationTest.php @@ -37,25 +37,38 @@ require_once 'lib/DoctrineTestInit.php'; */ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase { + public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) + { + try { + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery($dqlToBeTested); + + parent::assertEquals($sqlToBeConfirmed, $query->getSql()); + + $query->free(); + } catch (Doctrine_Exception $e) { + $this->fail($e->getMessage()); + } + } + + public function testWithoutWhere() { - $q = new Doctrine_Query(); - // NO WhereClause - $q->setDql('DELETE CmsUser u'); - $this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u', + 'DELETE FROM cms_user cu WHERE 1 = 1' + ); - $q->setDql('DELETE FROM CmsUser u'); - $this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE FROM CmsUser u', + 'DELETE FROM cms_user cu WHERE 1 = 1' + ); } public function testWithWhere() { - $q = new Doctrine_Query(); - // "WHERE" ConditionalExpression // ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm} // ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor} @@ -66,96 +79,66 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase // | InExpression | NullComparisonExpression) | ExistsExpression // If this one test fail, all others will fail too. That's the simplest case possible - $q->setDql('DELETE CmsUser u WHERE id = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE id = ?', + 'DELETE FROM cms_user cu WHERE cu.id = ?' + ); } public function testWithConditionalExpressions() { - $q = new Doctrine_Query(); - - $q->setDql('DELETE CmsUser u WHERE u.username = ? OR u.name = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ? OR cu.name = ?', $q->getSql()); - $q->free(); - - $q->setDql('DELETE CmsUser u WHERE u.id = ? OR ( u.username = ? OR u.name = ? )'); - $this->assertEquals( - 'DELETE FROM cms_user cu WHERE cu.id = ? OR (cu.username = ? OR cu.name = ?)', - $q->getSql() + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.username = ? OR u.name = ?', + 'DELETE FROM cms_user cu WHERE cu.username = ? OR cu.name = ?' ); - $q->free(); - $q->setDql('DELETE FROM CmsUser WHERE id = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); - $q->free(); - } + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.id = ? OR ( u.username = ? OR u.name = ? )', + 'DELETE FROM cms_user cu WHERE cu.id = ? OR (cu.username = ? OR cu.name = ?)' + ); - - public function testInvalidSyntaxIsRejected() - { - $q = new Doctrine_Query(); - - $invalidDql = 'FOOBAR CmsUser'; - $q->setDql($invalidDql); - try { - $q->getSql(); - $this->fail("Invalid DQL '$invalidDql' was not rejected."); - } catch (Doctrine_Exception $parseEx) {} - $q->free(); - - $invalidDql = 'DELETE FROM CmsUser.articles'; - $q->setDql($invalidDql); - try { - $q->getSql(); - $this->fail("Invalid DQL '$invalidDql' was not rejected."); - } catch (Doctrine_Exception $parseEx) {} - $q->free(); - - $invalidDql = 'DELETE FROM CmsUser cu WHERE cu.articles.id > ?'; - $q->setDql($invalidDql); - try { - $q->getSql(); - $this->fail("Invalid DQL '$invalidDql' was not rejected."); - } catch (Doctrine_Exception $parseEx) {} - $q->free(); + $this->assertSqlGeneration( + 'DELETE FROM CmsUser WHERE id = ?', + 'DELETE FROM cms_user cu WHERE cu.id = ?' + ); } public function testParserIsCaseAgnostic() { - $q = new Doctrine_Query(); - $q->setDql('delete from CmsUser u where u.username = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ?', $q->getSql()); + $this->assertSqlGeneration( + "delete from CmsUser u where u.username = ?", + "DELETE FROM cms_user cu WHERE cu.username = ?" + ); } public function testWithConditionalTerms() { - $q = new Doctrine_Query(); - - $q->setDql('DELETE CmsUser u WHERE u.username = ? AND u.name = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.username = ? AND cu.name = ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE u.username = ? AND u.name = ?", + "DELETE FROM cms_user cu WHERE cu.username = ? AND cu.name = ?" + ); } public function testWithConditionalFactors() { - $q = new Doctrine_Query(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE NOT id != ?", + "DELETE FROM cms_user cu WHERE NOT cu.id <> ?" + ); - $q->setDql('DELETE CmsUser u WHERE NOT id != ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE NOT cu.id <> ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE NOT ( id != ? )", + "DELETE FROM cms_user cu WHERE NOT (cu.id <> ?)" + ); - $q->setDql('DELETE CmsUser u WHERE NOT ( id != ? )'); - $this->assertEquals('DELETE FROM cms_user cu WHERE NOT (cu.id <> ?)', $q->getSql()); - $q->free(); - - $q->setDql('DELETE CmsUser u WHERE NOT ( id != ? AND username = ? )'); - $this->assertEquals('DELETE FROM cms_user cu WHERE NOT (cu.id <> ? AND cu.username = ?)', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE NOT ( id != ? AND username = ? )", + "DELETE FROM cms_user cu WHERE NOT (cu.id <> ? AND cu.username = ?)" + ); } @@ -164,93 +147,96 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase public function testWithExprAndComparison() { - $q = new Doctrine_Query(); - // id = ? was already tested (see testDeleteWithWhere()) + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id > ?", + "DELETE FROM cms_user cu WHERE cu.id > ?" + ); - $q->setDql('DELETE CmsUser u WHERE id > ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id > ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id >= ?", + "DELETE FROM cms_user cu WHERE cu.id >= ?" + ); - $q->setDql('DELETE CmsUser u WHERE id >= ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id >= ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id < ?", + "DELETE FROM cms_user cu WHERE cu.id < ?" + ); - $q->setDql('DELETE CmsUser u WHERE id < ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id < ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id <= ?", + "DELETE FROM cms_user cu WHERE cu.id <= ?" + ); - $q->setDql('DELETE CmsUser u WHERE id <= ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <= ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id <> ?", + "DELETE FROM cms_user cu WHERE cu.id <> ?" + ); - $q->setDql('DELETE CmsUser u WHERE id <> ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <> ?', $q->getSql()); - $q->free(); - - $q->setDql('DELETE CmsUser u WHERE id != ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id <> ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE id != ?", + "DELETE FROM cms_user cu WHERE cu.id <> ?" + ); } public function testWithExprAndBetween() { - $q = new Doctrine_Query(); - // "WHERE" Expression BetweenExpression - $q->setDql('DELETE CmsUser u WHERE u.id NOT BETWEEN ? AND ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id NOT BETWEEN ? AND ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE u.id NOT BETWEEN ? AND ?", + "DELETE FROM cms_user cu WHERE cu.id NOT BETWEEN ? AND ?" + ); - $q->setDql('DELETE CmsUser u WHERE u.id BETWEEN ? AND ? AND u.username != ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id BETWEEN ? AND ? AND cu.username <> ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE u.id BETWEEN ? AND ? AND u.username != ?", + "DELETE FROM cms_user cu WHERE cu.id BETWEEN ? AND ? AND cu.username <> ?" + ); } public function testWithExprAndLike() { - $q = new Doctrine_Query(); - // "WHERE" Expression LikeExpression - $q->setDql('DELETE CmsUser u WHERE u.username NOT LIKE ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.username NOT LIKE ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.username NOT LIKE ?', + 'DELETE FROM cms_user cu WHERE cu.username NOT LIKE ?' + ); - $q->setDql("DELETE CmsUser u WHERE u.username LIKE ? ESCAPE '\\'"); - $this->assertEquals("DELETE FROM cms_user cu WHERE cu.username LIKE ? ESCAPE '\\'", $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + "DELETE CmsUser u WHERE u.username LIKE ? ESCAPE '\\'", + "DELETE FROM cms_user cu WHERE cu.username LIKE ? ESCAPE '\\'" + ); } public function testWithExprAndIn() { - $q = new Doctrine_Query(); - // "WHERE" Expression InExpression - $q->setDql('DELETE CmsUser u WHERE u.id IN ( ?, ?, ?, ? )'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id IN (?, ?, ?, ?)', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.id IN ( ?, ?, ?, ? )', + 'DELETE FROM cms_user cu WHERE cu.id IN (?, ?, ?, ?)' + ); - $q->setDql('DELETE CmsUser u WHERE u.id NOT IN ( ?, ? )'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id NOT IN (?, ?)', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.id NOT IN ( ?, ? )', + 'DELETE FROM cms_user cu WHERE cu.id NOT IN (?, ?)' + ); } public function testWithExprAndNull() { - $q = new Doctrine_Query(); - // "WHERE" Expression NullComparisonExpression - $q->setDql('DELETE CmsUser u WHERE u.name IS NULL'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.name IS NULL', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.name IS NULL', + 'DELETE FROM cms_user cu WHERE cu.name IS NULL' + ); - $q->setDql('DELETE CmsUser u WHERE u.name IS NOT NULL'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.name IS NOT NULL', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE u.name IS NOT NULL', + 'DELETE FROM cms_user cu WHERE cu.name IS NOT NULL' + ); } @@ -258,15 +244,15 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase public function testWithPrimaryAsAtom() { - $q = new Doctrine_Query(); - // Atom = string | integer | float | boolean | input_parameter - $q->setDql('DELETE CmsUser u WHERE 1 = 1'); - $this->assertEquals('DELETE FROM cms_user cu WHERE 1 = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE 1 = 1', + 'DELETE FROM cms_user cu WHERE 1 = 1' + ); - $q->setDql('DELETE CmsUser u WHERE ? = 1'); - $this->assertEquals('DELETE FROM cms_user cu WHERE ? = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'DELETE CmsUser u WHERE ? = 1', + 'DELETE FROM cms_user cu WHERE ? = 1' + ); } } diff --git a/tests/Orm/Query/DqlGenerationTest.php b/tests/Orm/Query/DqlGenerationTest.php index 2393a2bf2..f37139daa 100755 --- a/tests/Orm/Query/DqlGenerationTest.php +++ b/tests/Orm/Query/DqlGenerationTest.php @@ -34,216 +34,215 @@ */ class Orm_Query_DqlGenerationTest extends Doctrine_OrmTestCase { - const QueryClass = 'Doctrine_Query'; + protected function createQuery() + { + $entityManager = Doctrine_EntityManager::getManager(); + return $entityManager->createQuery(); + } + public function testSelect() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); // select and from - $q->setDql('FROM User u'); - $this->assertEquals('FROM User u', $q->getDql()); // Internally we use SELECT * FROM User u to process the DQL - $q->free(); + $query->setDql('FROM User u'); + $this->assertEquals('FROM User u', $query->getDql()); // Internally we use SELECT * FROM User u to process the DQL + $query->free(); - $q->select()->from('User u'); - $this->assertEquals('SELECT * FROM User u', $q->getDql()); - $q->free(); + $query->select()->from('User u'); + $this->assertEquals('SELECT * FROM User u', $query->getDql()); + $query->free(); - $q->select('u.*')->from('User u'); - $this->assertEquals('SELECT u.* FROM User u', $q->getDql()); - $q->free(); + $query->select('u.*')->from('User u'); + $this->assertEquals('SELECT u.* FROM User u', $query->getDql()); + $query->free(); - $q->select('u.id')->from('User u'); - $this->assertEquals('SELECT u.id FROM User u', $q->getDql()); - $q->free(); + $query->select('u.id')->from('User u'); + $this->assertEquals('SELECT u.id FROM User u', $query->getDql()); + $query->free(); - $q->select('u.id, u.name')->from('User u'); - $this->assertEquals('SELECT u.id, u.name FROM User u', $q->getDql()); - $q->free(); + $query->select('u.id, u.name')->from('User u'); + $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql()); + $query->free(); - $q->select('u.name AS myCustomName')->from('User u'); - $this->assertEquals('SELECT u.name AS myCustomName FROM User u', $q->getDql()); - $q->free(); + $query->select('u.name AS myCustomName')->from('User u'); + $this->assertEquals('SELECT u.name AS myCustomName FROM User u', $query->getDql()); + $query->free(); - $q->select('u.id')->select('u.name')->from('User u'); - $this->assertEquals('SELECT u.id, u.name FROM User u', $q->getDql()); - $q->free(); + $query->select('u.id')->select('u.name')->from('User u'); + $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql()); + $query->free(); } public function testSelectDistinct() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); - $q->select()->distinct()->from('User u'); - $this->assertEquals('SELECT DISTINCT * FROM User u', $q->getDql()); - $q->free(); + $query->select()->distinct()->from('User u'); + $this->assertEquals('SELECT DISTINCT * FROM User u', $query->getDql()); + $query->free(); - $q->select('u.name')->distinct(false)->from('User u'); - $this->assertEquals('SELECT u.name FROM User u', $q->getDql()); - $q->free(); + $query->select('u.name')->distinct(false)->from('User u'); + $this->assertEquals('SELECT u.name FROM User u', $query->getDql()); + $query->free(); - $q->select()->distinct(false)->from('User u'); - $this->assertEquals('SELECT * FROM User u', $q->getDql()); - $q->free(); + $query->select()->distinct(false)->from('User u'); + $this->assertEquals('SELECT * FROM User u', $query->getDql()); + $query->free(); - $q->select('u.name')->distinct()->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name FROM User u', $q->getDql()); - $q->free(); + $query->select('u.name')->distinct()->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql()); + $query->free(); - $q->select('u.name, u.email')->distinct()->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); - $q->free(); + $query->select('u.name, u.email')->distinct()->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql()); + $query->free(); - $q->select('u.name')->select('u.email')->distinct()->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); - $q->free(); + $query->select('u.name')->select('u.email')->distinct()->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql()); + $query->free(); - $q->select('DISTINCT u.name')->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name FROM User u', $q->getDql()); - $q->free(); + $query->select('DISTINCT u.name')->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql()); + $query->free(); - $q->select('DISTINCT u.name, u.email')->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); - $q->free(); + $query->select('DISTINCT u.name, u.email')->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql()); + $query->free(); - $q->select('DISTINCT u.name')->select('u.email')->from('User u'); - $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $q->getDql()); - $q->free(); + $query->select('DISTINCT u.name')->select('u.email')->from('User u'); + $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql()); + $query->free(); } public function testSelectJoin() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); - $q->select('u.*')->from('User u')->join('u.Group g')->where('g.id = ?', 1); - $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $q->getDql()); - $this->assertEquals(array(1), $q->getParams()); - $q->free(); + $query->select('u.*')->from('User u')->join('u.Group g')->where('g.id = ?', 1); + $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql()); + $this->assertEquals(array(1), $query->getParams()); + $query->free(); - $q->select('u.*')->from('User u')->innerJoin('u.Group g')->where('g.id = ?', 1); - $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $q->getDql()); - $this->assertEquals(array(1), $q->getParams()); - $q->free(); + $query->select('u.*')->from('User u')->innerJoin('u.Group g')->where('g.id = ?', 1); + $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql()); + $this->assertEquals(array(1), $query->getParams()); + $query->free(); - $q->select('u.*')->from('User u')->leftJoin('u.Group g')->where('g.id IS NULL'); - $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.Group g WHERE g.id IS NULL', $q->getDql()); - $q->free(); + $query->select('u.*')->from('User u')->leftJoin('u.Group g')->where('g.id IS NULL'); + $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.Group g WHERE g.id IS NULL', $query->getDql()); + $query->free(); - $q->select('u.*')->from('User u')->leftJoin('u.UserGroup ug')->leftJoin('ug.Group g')->where('g.name = ?', 'admin'); - $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.UserGroup ug LEFT JOIN ug.Group g WHERE g.name = ?', $q->getDql()); - $q->free(); + $query->select('u.*')->from('User u')->leftJoin('u.UserGroup ug')->leftJoin('ug.Group g')->where('g.name = ?', 'admin'); + $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.UserGroup ug LEFT JOIN ug.Group g WHERE g.name = ?', $query->getDql()); + $query->free(); } public function testSelectWhere() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); - $q->select('u.name')->from('User u')->where('u.id = ?', 1); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ?', $q->getDql()); - $this->assertEquals(array(1), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.id = ?', 1); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ?', $query->getDql()); + $this->assertEquals(array(1), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.id = ? AND u.type != ?', array(1, 'admin')); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $q->getDql()); - $this->assertEquals(array(1, 'admin'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.id = ? AND u.type != ?', array(1, 'admin')); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql()); + $this->assertEquals(array(1, 'admin'), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.id = ?', 1)->andWhere('u.type != ?', 'admin'); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $q->getDql()); - $this->assertEquals(array(1, 'admin'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.id = ?', 1)->andWhere('u.type != ?', 'admin'); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql()); + $this->assertEquals(array(1, 'admin'), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('( u.id = ?', 1)->andWhere('u.type != ? )', 'admin'); - $this->assertEquals('SELECT u.name FROM User u WHERE ( u.id = ? AND u.type != ? )', $q->getDql()); - $this->assertEquals(array(1, 'admin'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('( u.id = ?', 1)->andWhere('u.type != ? )', 'admin'); + $this->assertEquals('SELECT u.name FROM User u WHERE ( u.id = ? AND u.type != ? )', $query->getDql()); + $this->assertEquals(array(1, 'admin'), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.id = ? OR u.type != ?', array(1, 'admin')); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $q->getDql()); - $this->assertEquals(array(1, 'admin'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.id = ? OR u.type != ?', array(1, 'admin')); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql()); + $this->assertEquals(array(1, 'admin'), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.id = ?', 1)->orWhere('u.type != ?', 'admin'); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $q->getDql()); - $this->assertEquals(array(1, 'admin'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.id = ?', 1)->orWhere('u.type != ?', 'admin'); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql()); + $this->assertEquals(array(1, 'admin'), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->andwhere('u.id = ?', 1)->andWhere('u.type != ?', 'admin')->orWhere('u.email = ?', 'admin@localhost'); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ? OR u.email = ?', $q->getDql()); - $this->assertEquals(array(1, 'admin', 'admin@localhost'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->andwhere('u.id = ?', 1)->andWhere('u.type != ?', 'admin')->orWhere('u.email = ?', 'admin@localhost'); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ? OR u.email = ?', $query->getDql()); + $this->assertEquals(array(1, 'admin', 'admin@localhost'), $query->getParams()); + $query->free(); } public function testSelectWhereIn() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); - $q->select('u.name')->from('User u')->whereIn('u.id', array(1, 2, 3, 4, 5)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id IN (?, ?, ?, ?, ?)', $q->getDql()); - $this->assertEquals(array(1, 2, 3, 4, 5), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->whereIn('u.id', array(1, 2, 3, 4, 5)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id IN (?, ?, ?, ?, ?)', $query->getDql()); + $this->assertEquals(array(1, 2, 3, 4, 5), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->whereNotIn('u.id', array(1, 2, 3)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.id NOT IN (?, ?, ?)', $q->getDql()); - $this->assertEquals(array(1, 2, 3), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->whereNotIn('u.id', array(1, 2, 3)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.id NOT IN (?, ?, ?)', $query->getDql()); + $this->assertEquals(array(1, 2, 3), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereIn('u.id', array(1, 2)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id IN (?, ?)', $q->getDql()); - $this->assertEquals(array('admin', 1, 2), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereIn('u.id', array(1, 2)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id IN (?, ?)', $query->getDql()); + $this->assertEquals(array('admin', 1, 2), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereNotIn('u.id', array(1, 2)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id NOT IN (?, ?)', $q->getDql()); - $this->assertEquals(array('admin', 1, 2), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereNotIn('u.id', array(1, 2)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id NOT IN (?, ?)', $query->getDql()); + $this->assertEquals(array('admin', 1, 2), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2, 3, 4)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?, ?, ?)', $q->getDql()); - $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2, 3, 4)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?, ?, ?)', $query->getDql()); + $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->orWhereIn('u.id', array(1, 2, 3, 4)); - $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) OR u.id IN (?, ?, ?, ?)', $q->getDql()); - $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->orWhereIn('u.id', array(1, 2, 3, 4)); + $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) OR u.id IN (?, ?, ?, ?)', $query->getDql()); + $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams()); + $query->free(); - $q->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2))->orWhereNotIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.email', array('user@localhost', 'guest@localhost')); - $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?) OR u.type NOT IN (?, ?) AND u.email NOT IN (?, ?)', $q->getDql()); - $this->assertEquals(array('admin', 'moderator', 1, 2, 'admin', 'moderator', 'user@localhost', 'guest@localhost'), $q->getParams()); - $q->free(); + $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2))->orWhereNotIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.email', array('user@localhost', 'guest@localhost')); + $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?) OR u.type NOT IN (?, ?) AND u.email NOT IN (?, ?)', $query->getDql()); + $this->assertEquals(array('admin', 'moderator', 1, 2, 'admin', 'moderator', 'user@localhost', 'guest@localhost'), $query->getParams()); + $query->free(); } public function testDelete() { - $class = self::QueryClass; - $q = new $class(); + $query = $this->createQuery(); - $q->setDql('DELETE CmsUser u'); - $this->assertEquals('DELETE CmsUser u', $q->getDql()); - $q->free(); + $query->setDql('DELETE CmsUser u'); + $this->assertEquals('DELETE CmsUser u', $query->getDql()); + $query->free(); - $q->delete()->from('CmsUser u'); - $this->assertEquals('DELETE FROM CmsUser u', $q->getDql()); - $q->free(); + $query->delete()->from('CmsUser u'); + $this->assertEquals('DELETE FROM CmsUser u', $query->getDql()); + $query->free(); - $q->delete()->from('CmsUser u')->where('u.id = ?', 1); - $this->assertEquals('DELETE FROM CmsUser u WHERE u.id = ?', $q->getDql()); - $q->free(); + $query->delete()->from('CmsUser u')->where('u.id = ?', 1); + $this->assertEquals('DELETE FROM CmsUser u WHERE u.id = ?', $query->getDql()); + $query->free(); - $q->delete()->from('CmsUser u')->where('u.username = ?', 'gblanco')->orWhere('u.name = ?', 'Guilherme'); - $this->assertEquals('DELETE FROM CmsUser u WHERE u.username = ? OR u.name = ?', $q->getDql()); - $q->free(); + $query->delete()->from('CmsUser u')->where('u.username = ?', 'gblanco')->orWhere('u.name = ?', 'Guilherme'); + $this->assertEquals('DELETE FROM CmsUser u WHERE u.username = ? OR u.name = ?', $query->getDql()); + $query->free(); } } diff --git a/tests/Orm/Query/IdentifierRecognitionTest.php b/tests/Orm/Query/IdentifierRecognitionTest.php index 0c0a69c08..095b502da 100755 --- a/tests/Orm/Query/IdentifierRecognitionTest.php +++ b/tests/Orm/Query/IdentifierRecognitionTest.php @@ -39,8 +39,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testSingleAliasDeclarationIsSupported() { - $query = new Doctrine_Query; - $query->setDql('SELECT u.* FROM CmsUser u'); + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u'); $parserResult = $query->parse(); $decl = $parserResult->getQueryComponent('u'); @@ -54,8 +54,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testSingleAliasDeclarationWithIndexByIsSupported() { - $query = new Doctrine_Query; - $query->setDql('SELECT u.* FROM CmsUser u INDEX BY id'); + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id'); $parserResult = $query->parse(); $decl = $parserResult->getQueryComponent('u'); @@ -69,8 +69,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testQueryParserSupportsMultipleAliasDeclarations() { - $query = new Doctrine_Query; - $query->setDql('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p'); + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p'); $parserResult = $query->parse(); $decl = $parserResult->getQueryComponent('u'); @@ -93,8 +93,8 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testQueryParserSupportsMultipleAliasDeclarationsWithIndexBy() { - $query = new Doctrine_Query; - $query->setDql('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.articles a INNER JOIN u.phonenumbers pn INDEX BY phonenumber'); + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.articles a INNER JOIN u.phonenumbers pn INDEX BY phonenumber'); $parserResult = $query->parse(); $decl = $parserResult->getQueryComponent('u'); diff --git a/tests/Orm/Query/LanguageRecognitionTest.php b/tests/Orm/Query/LanguageRecognitionTest.php index 1df701892..f9b078fdc 100755 --- a/tests/Orm/Query/LanguageRecognitionTest.php +++ b/tests/Orm/Query/LanguageRecognitionTest.php @@ -38,11 +38,11 @@ require_once 'lib/DoctrineTestInit.php'; */ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase { - public function assertValidDql($dql, $method = '') + public function assertValidDql($dql) { try { - $query = new Doctrine_Query; - $query->setDql($dql); + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery($dql); $parserResult = $query->parse(); } catch (Doctrine_Exception $e) { $this->fail($e->getMessage()); @@ -52,7 +52,8 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase public function assertInvalidDql($dql) { try { - $query = new Doctrine_Query; + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery($dql); $query->setDql($dql); $parserResult = $query->parse(); @@ -382,4 +383,14 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase $this->assertValidDql("SELECT u.id FROM CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|'"); } + + public function testInvalidSyntaxIsRejected() + { + $this->assertInvalidDql("FOOBAR CmsUser"); + + $this->assertInvalidDql("DELETE FROM CmsUser.articles"); + + $this->assertInvalidDql("DELETE FROM CmsUser cu WHERE cu.articles.id > ?"); + } + } diff --git a/tests/Orm/Query/SelectSqlGenerationTest.php b/tests/Orm/Query/SelectSqlGenerationTest.php index 76c966cbf..a5e5aa138 100755 --- a/tests/Orm/Query/SelectSqlGenerationTest.php +++ b/tests/Orm/Query/SelectSqlGenerationTest.php @@ -37,25 +37,38 @@ require_once 'lib/DoctrineTestInit.php'; */ class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase { - public function testWithoutWhere() + public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) { - $q = new Doctrine_Query(); + try { + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery($dqlToBeTested); - // NO WhereClause - $q->setDql('SELECT u.id FROM CmsUser u'); - $this->assertEquals('SELECT cu.id AS cu__id FROM cms_user cu WHERE 1 = 1', $q->getSql()); - $q->free(); + parent::assertEquals($sqlToBeConfirmed, $query->getSql()); - $q->setDql('SELECT u.* FROM CmsUser u'); - $this->assertEquals('SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE 1 = 1', $q->getSql()); - $q->free(); + $query->free(); + } catch (Doctrine_Exception $e) { + $this->fail($e->getMessage()); + } } -/* + + public function testWithoutWhere() + { + // NO WhereClause + $this->assertSqlGeneration( + 'SELECT u.id FROM CmsUser u', + 'SELECT cu.id AS cu__id FROM cms_user cu WHERE 1 = 1' + ); + + $this->assertSqlGeneration( + 'SELECT u.* FROM CmsUser u', + 'SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE 1 = 1' + ); + } + + public function testWithWhere() { - $q = new Doctrine_Query(); - // "WHERE" ConditionalExpression // ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm} // ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor} @@ -66,9 +79,10 @@ class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase // | InExpression | NullComparisonExpression) | ExistsExpression // If this one test fail, all others will fail too. That's the simplest case possible - $q->setDql('SELECT u.* FROM CmsUser u WHERE id = ?'); - $this->assertEquals('DELETE FROM cms_user cu WHERE cu.id = ?', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'SELECT u.* FROM CmsUser u WHERE id = ?', + 'SELECT cu.id AS cu__id, cu.status AS cu__status, cu.username AS cu__username, cu.name AS cu__name FROM cms_user cu WHERE cu.id = ?' + ); } -*/ + } \ No newline at end of file diff --git a/tests/Orm/Query/UpdateSqlGenerationTest.php b/tests/Orm/Query/UpdateSqlGenerationTest.php index dda959dcd..7900a88e1 100755 --- a/tests/Orm/Query/UpdateSqlGenerationTest.php +++ b/tests/Orm/Query/UpdateSqlGenerationTest.php @@ -37,17 +37,32 @@ */ class Orm_Query_UpdateSqlGenerationTest extends Doctrine_OrmTestCase { + public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) + { + try { + $entityManager = Doctrine_EntityManager::getManager(); + $query = $entityManager->createQuery($dqlToBeTested); + + parent::assertEquals($sqlToBeConfirmed, $query->getSql()); + + $query->free(); + } catch (Doctrine_Exception $e) { + $this->fail($e->getMessage()); + } + } + + public function testWithoutWhere() { - $q = new Doctrine_Query(); - // NO WhereClause - $q->setDql('UPDATE CmsUser u SET name = ?'); - $this->assertEquals('UPDATE cms_user cu SET cu.name = ? WHERE 1 = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'UPDATE CmsUser u SET name = ?', + 'UPDATE cms_user cu SET cu.name = ? WHERE 1 = 1' + ); - $q->setDql('UPDATE CmsUser u SET name = ?, username = ?'); - $this->assertEquals('UPDATE cms_user cu SET cu.name = ?, cu.username = ? WHERE 1 = 1', $q->getSql()); - $q->free(); + $this->assertSqlGeneration( + 'UPDATE CmsUser u SET name = ?, username = ?', + 'UPDATE cms_user cu SET cu.name = ?, cu.username = ? WHERE 1 = 1' + ); } }