2009-05-21 23:18:14 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
2009-07-07 00:34:54 +04:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
2009-05-21 23:18:14 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* QueryCacheTest
|
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
|
|
|
class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
2011-09-13 07:59:24 +04:00
|
|
|
/**
|
|
|
|
* @var \ReflectionProperty
|
|
|
|
*/
|
|
|
|
private $cacheDataReflection;
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
protected function setUp() {
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->cacheDataReflection = new \ReflectionProperty("Doctrine\Common\Cache\ArrayCache", "data");
|
|
|
|
$this->cacheDataReflection->setAccessible(true);
|
2009-05-21 23:18:14 +04:00
|
|
|
$this->useModelSet('cms');
|
|
|
|
parent::setUp();
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-09-13 07:59:24 +04:00
|
|
|
/**
|
|
|
|
* @param ArrayCache $cache
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
private function getCacheSize(ArrayCache $cache)
|
|
|
|
{
|
|
|
|
return sizeof($this->cacheDataReflection->getValue($cache));
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
public function testQueryCache_DependsOnHints()
|
2009-05-21 23:18:14 +04:00
|
|
|
{
|
2010-02-10 22:09:25 +03:00
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
|
|
|
|
|
|
|
$cache = new ArrayCache();
|
|
|
|
$query->setQueryCacheDriver($cache);
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$query->getResult();
|
2012-05-26 21:29:51 +04:00
|
|
|
$this->assertEquals(2, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query->setHint('foo', 'bar');
|
|
|
|
|
|
|
|
$query->getResult();
|
2012-05-26 21:29:51 +04:00
|
|
|
$this->assertEquals(3, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param <type> $query
|
|
|
|
* @depends testQueryCache_DependsOnHints
|
|
|
|
*/
|
|
|
|
public function testQueryCache_DependsOnFirstResult($query)
|
|
|
|
{
|
|
|
|
$cache = $query->getQueryCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query->setFirstResult(10);
|
2010-02-25 18:47:20 +03:00
|
|
|
$query->setMaxResults(9999);
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query->getResult();
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param <type> $query
|
|
|
|
* @depends testQueryCache_DependsOnHints
|
|
|
|
*/
|
|
|
|
public function testQueryCache_DependsOnMaxResults($query)
|
|
|
|
{
|
|
|
|
$cache = $query->getQueryCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2009-05-21 23:18:14 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$query->setMaxResults(10);
|
|
|
|
|
|
|
|
$query->getResult();
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
}
|
|
|
|
|
2010-02-14 00:42:09 +03:00
|
|
|
/**
|
|
|
|
* @param <type> $query
|
|
|
|
* @depends testQueryCache_DependsOnHints
|
|
|
|
*/
|
|
|
|
public function testQueryCache_DependsOnHydrationMode($query)
|
|
|
|
{
|
|
|
|
$cache = $query->getQueryCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2010-02-14 00:42:09 +03:00
|
|
|
|
|
|
|
$query->getArrayResult();
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
|
2010-02-14 00:42:09 +03:00
|
|
|
}
|
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
public function testQueryCache_NoHitSaveParserResult()
|
|
|
|
{
|
2010-04-10 19:35:18 +04:00
|
|
|
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
|
2009-05-21 23:18:14 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-05-26 21:29:51 +04:00
|
|
|
$cache = new \Doctrine\Common\Cache\ArrayCache();
|
2010-02-10 22:09:25 +03:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
$query->setQueryCacheDriver($cache);
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2009-08-03 21:18:37 +04:00
|
|
|
$users = $query->getResult();
|
2012-05-26 21:29:51 +04:00
|
|
|
|
|
|
|
$data = $this->cacheDataReflection->getValue($cache);
|
|
|
|
$this->assertEquals(2, count($data));
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Query\ParserResult', array_pop($data));
|
2010-02-10 22:09:25 +03:00
|
|
|
}
|
2009-05-21 23:18:14 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
public function testQueryCache_HitDoesNotSaveParserResult()
|
|
|
|
{
|
2010-04-10 19:35:18 +04:00
|
|
|
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$sqlExecMock = $this->getMock('Doctrine\ORM\Query\Exec\AbstractSqlExecutor', array('execute'));
|
|
|
|
$sqlExecMock->expects($this->once())
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue( 10 ));
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$parserResultMock = $this->getMock('Doctrine\ORM\Query\ParserResult');
|
|
|
|
$parserResultMock->expects($this->once())
|
|
|
|
->method('getSqlExecutor')
|
|
|
|
->will($this->returnValue($sqlExecMock));
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2011-12-20 01:56:19 +04:00
|
|
|
$cache = $this->getMock('Doctrine\Common\Cache\CacheProvider',
|
2011-10-11 15:22:26 +04:00
|
|
|
array('doFetch', 'doContains', 'doSave', 'doDelete', 'doFlush', 'doGetStats'));
|
2012-05-26 21:29:51 +04:00
|
|
|
$cache->expects($this->at(0))->method('doFetch')->will($this->returnValue(1));
|
|
|
|
$cache->expects($this->at(1))
|
2011-09-13 07:59:24 +04:00
|
|
|
->method('doFetch')
|
2010-02-10 22:09:25 +03:00
|
|
|
->with($this->isType('string'))
|
|
|
|
->will($this->returnValue($parserResultMock));
|
|
|
|
$cache->expects($this->never())
|
2011-09-13 07:59:24 +04:00
|
|
|
->method('doSave');
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$query->setQueryCacheDriver($cache);
|
|
|
|
|
|
|
|
$users = $query->getResult();
|
2009-05-21 23:18:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|