2010-08-27 23:28:26 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Query;
|
|
|
|
|
2012-01-09 11:26:07 +04:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
2012-05-28 20:16:42 +04:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Query\Parameter;
|
2010-08-27 23:28:26 +04:00
|
|
|
|
|
|
|
class QueryTest extends \Doctrine\Tests\OrmTestCase
|
|
|
|
{
|
|
|
|
protected $_em = null;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->_em = $this->_getTestEntityManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetParameters()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
|
2012-05-28 20:16:42 +04:00
|
|
|
|
|
|
|
$parameters = new ArrayCollection();
|
|
|
|
|
|
|
|
$this->assertEquals($parameters, $query->getParameters());
|
2010-08-27 23:28:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-05-28 20:16:42 +04:00
|
|
|
|
|
|
|
$parameters = new ArrayCollection();
|
|
|
|
$parameters->add(new Parameter(2, 84));
|
|
|
|
|
|
|
|
$this->assertEquals($parameters, $query->getParameters());
|
2010-08-27 23:28:26 +04:00
|
|
|
}
|
|
|
|
|
2010-09-13 00:44:02 +04:00
|
|
|
public function testSetParameters()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
|
2012-05-28 20:16:42 +04:00
|
|
|
|
|
|
|
$parameters = new ArrayCollection();
|
|
|
|
$parameters->add(new Parameter(1, 'foo'));
|
|
|
|
$parameters->add(new Parameter(2, 'bar'));
|
|
|
|
|
|
|
|
$query->setParameters($parameters);
|
|
|
|
|
|
|
|
$this->assertEquals($parameters, $query->getParameters());
|
2010-09-13 00:44:02 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 23:28:26 +04:00
|
|
|
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();
|
|
|
|
|
2012-05-28 20:16:42 +04:00
|
|
|
$this->assertEquals(0, count($query->getParameters()));
|
2010-08-27 23:28:26 +04:00
|
|
|
}
|
2010-08-30 22:30:11 +04:00
|
|
|
|
|
|
|
public function testClone()
|
|
|
|
{
|
|
|
|
$dql = "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1";
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery($dql);
|
|
|
|
$query->setParameter(2, 84, \PDO::PARAM_INT);
|
|
|
|
$query->setHint('foo', 'bar');
|
|
|
|
|
|
|
|
$cloned = clone $query;
|
|
|
|
|
|
|
|
$this->assertEquals($dql, $cloned->getDql());
|
2012-05-28 20:16:42 +04:00
|
|
|
$this->assertEquals(0, count($cloned->getParameters()));
|
2010-08-30 22:30:11 +04:00
|
|
|
$this->assertFalse($cloned->getHint('foo'));
|
|
|
|
}
|
2010-09-13 00:44:02 +04:00
|
|
|
|
|
|
|
public function testFluentQueryInterface()
|
|
|
|
{
|
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
|
|
|
|
$q2 = $q->expireQueryCache(true)
|
|
|
|
->setQueryCacheLifetime(3600)
|
|
|
|
->setQueryCacheDriver(null)
|
|
|
|
->expireResultCache(true)
|
|
|
|
->setHint('foo', 'bar')
|
|
|
|
->setHint('bar', 'baz')
|
|
|
|
->setParameter(1, 'bar')
|
2012-05-28 20:16:42 +04:00
|
|
|
->setParameters(new ArrayCollection(array(new Parameter(2, 'baz'))))
|
2010-09-13 00:44:02 +04:00
|
|
|
->setResultCacheDriver(null)
|
|
|
|
->setResultCacheId('foo')
|
|
|
|
->setDql('foo')
|
|
|
|
->setFirstResult(10)
|
|
|
|
->setMaxResults(10);
|
|
|
|
|
|
|
|
$this->assertSame($q2, $q);
|
|
|
|
}
|
2011-01-23 18:47:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-968
|
|
|
|
*/
|
|
|
|
public function testHints()
|
|
|
|
{
|
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
|
|
|
|
$q->setHint('foo', 'bar')->setHint('bar', 'baz');
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $q->getHint('foo'));
|
|
|
|
$this->assertEquals('baz', $q->getHint('bar'));
|
|
|
|
$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $q->getHints());
|
|
|
|
}
|
2012-01-09 11:26:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1588
|
|
|
|
*/
|
|
|
|
public function testQueryDefaultResultCache()
|
|
|
|
{
|
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
|
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
|
|
|
|
$q->useResultCache(true);
|
|
|
|
$this->assertSame($this->_em->getConfiguration()->getResultCacheImpl(), $q->getQueryCacheProfile()->getResultCacheDriver());
|
|
|
|
}
|
2012-01-25 01:51:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException
|
|
|
|
**/
|
|
|
|
public function testIterateWithNoDistinctAndWrongSelectClause()
|
|
|
|
{
|
|
|
|
$q = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
|
|
|
|
$q->iterate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException
|
|
|
|
**/
|
|
|
|
public function testIterateWithNoDistinctAndWithValidSelectClause()
|
|
|
|
{
|
|
|
|
$q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
|
|
|
|
$q->iterate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterateWithDistinct()
|
|
|
|
{
|
|
|
|
$q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a");
|
|
|
|
$q->iterate();
|
|
|
|
}
|
2012-03-25 05:50:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1697
|
|
|
|
*/
|
2012-05-28 20:16:42 +04:00
|
|
|
public function testCollectionParameters()
|
2012-03-25 05:50:54 +04:00
|
|
|
{
|
|
|
|
$cities = array(
|
|
|
|
0 => "Paris",
|
|
|
|
3 => "Canne",
|
|
|
|
9 => "St Julien"
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = $this->_em
|
|
|
|
->createQuery("SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)")
|
|
|
|
->setParameter('cities', $cities);
|
|
|
|
|
|
|
|
$parameters = $query->getParameters();
|
2012-05-28 20:16:42 +04:00
|
|
|
$parameter = $parameters->first();
|
2012-03-25 05:50:54 +04:00
|
|
|
|
2012-05-28 20:16:42 +04:00
|
|
|
$this->assertEquals('cities', $parameter->getName());
|
|
|
|
$this->assertEquals($cities, $parameter->getValue());
|
2012-03-25 05:50:54 +04:00
|
|
|
}
|
2012-01-09 11:26:07 +04:00
|
|
|
}
|