From 8a21ab4755166127a9e60b98ab2e4759bc383997 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 27 Aug 2010 21:28:26 +0200 Subject: [PATCH] DDC-770 - Cleanup Query instance when its cloned --- lib/Doctrine/ORM/AbstractQuery.php | 16 +++++++ .../Tests/ORM/Functional/QueryTest.php | 22 --------- tests/Doctrine/Tests/ORM/Query/AllTests.php | 1 + tests/Doctrine/Tests/ORM/Query/QueryTest.php | 47 +++++++++++++++++++ 4 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Query/QueryTest.php diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 7c3c42f9c..21974b792 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -138,10 +138,16 @@ abstract class AbstractQuery /** * Frees the resources used by the query object. + * + * Resets Parameters, Parameter Types and Query Hints. + * + * @return void */ public function free() { $this->_params = array(); + $this->_paramTypes = array(); + $this->_hints = array(); } /** @@ -569,4 +575,14 @@ abstract class AbstractQuery * @return Doctrine\DBAL\Driver\Statement The executed database statement that holds the results. */ abstract protected function _doExecute(); + + /** + * Cleanup Query resource when clone is called. + * + * @return void + */ + public function __clone() + { + $this->free(); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 9995d0560..9a0de4bd2 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -19,28 +19,6 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->useModelSet('cms'); parent::setUp(); } - - /** - * @expectedException Doctrine\ORM\Query\QueryException - */ - public function testParameterIndexZeroThrowsException() - { - $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); - $query->execute(array(42)); // same as array(0 => 42), 0 is invalid parameter position - } - - public function testGetParameters() - { - $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); - $this->assertEquals(array(), $query->getParameters()); - } - - public function testGetParameters_HasSomeAlready() - { - $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); - $query->setParameter(2, 84); - $this->assertEquals(array(2 => 84), $query->getParameters()); - } public function testSimpleQueries() { diff --git a/tests/Doctrine/Tests/ORM/Query/AllTests.php b/tests/Doctrine/Tests/ORM/Query/AllTests.php index 02ab07365..0d94856f9 100644 --- a/tests/Doctrine/Tests/ORM/Query/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Query/AllTests.php @@ -26,6 +26,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Query\UpdateSqlGenerationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Query\ExprTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Query\ParserResultTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Query\QueryTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Query/QueryTest.php b/tests/Doctrine/Tests/ORM/Query/QueryTest.php new file mode 100644 index 000000000..f606f0900 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Query/QueryTest.php @@ -0,0 +1,47 @@ +_em = $this->_getTestEntityManager(); + } + + /** + * @expectedException Doctrine\ORM\Query\QueryException + */ + public function testParameterIndexZeroThrowsException() + { + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); + $query->execute(array(42)); // same as array(0 => 42), 0 is invalid parameter position + } + + public function testGetParameters() + { + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); + $this->assertEquals(array(), $query->getParameters()); + } + + public function testGetParameters_HasSomeAlready() + { + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); + $query->setParameter(2, 84); + $this->assertEquals(array(2 => 84), $query->getParameters()); + } + + public function testFree() + { + $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); + $query->setParameter(2, 84, \PDO::PARAM_INT); + + $query->free(); + + $this->assertEquals(array(), $query->getParameters()); + } +} \ No newline at end of file