2009-07-21 14:48:19 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
2010-12-03 19:34:56 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle;
|
2009-07-21 14:48:19 +04:00
|
|
|
use Doctrine\Common\Cache\ArrayCache;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResultCacheTest
|
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
|
|
|
class ResultCacheTest 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-07-21 14:48:19 +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-07-21 14:48:19 +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));
|
|
|
|
}
|
2009-07-21 14:48:19 +04:00
|
|
|
|
2009-10-23 02:39:37 +04:00
|
|
|
public function testResultCache()
|
2009-07-21 14:48:19 +04:00
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'dev';
|
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$cache = new ArrayCache();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$query->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
|
|
|
|
|
|
|
|
$this->assertFalse($cache->contains('my_cache_id'));
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2009-08-03 21:18:37 +04:00
|
|
|
$users = $query->getResult();
|
2009-07-21 14:48:19 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$this->assertTrue($cache->contains('my_cache_id'));
|
2009-07-21 14:48:19 +04:00
|
|
|
$this->assertEquals(1, count($users));
|
|
|
|
$this->assertEquals('Roman', $users[0]->name);
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2009-07-21 14:48:19 +04:00
|
|
|
$this->_em->clear();
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2009-07-21 14:48:19 +04:00
|
|
|
$query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
2010-02-10 22:09:25 +03:00
|
|
|
$query2->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
|
2010-01-29 04:38:37 +03:00
|
|
|
|
2009-08-03 21:18:37 +04:00
|
|
|
$users = $query2->getResult();
|
2009-07-21 14:48:19 +04:00
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
$this->assertTrue($cache->contains('my_cache_id'));
|
2009-07-21 14:48:19 +04:00
|
|
|
$this->assertEquals(1, count($users));
|
|
|
|
$this->assertEquals('Roman', $users[0]->name);
|
|
|
|
}
|
|
|
|
|
2009-10-23 02:39:37 +04:00
|
|
|
public function testSetResultCacheId()
|
|
|
|
{
|
|
|
|
$cache = new ArrayCache;
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
2009-10-24 04:28:43 +04:00
|
|
|
$query->setResultCacheDriver($cache);
|
2009-10-23 02:39:37 +04:00
|
|
|
$query->setResultCacheId('testing_result_cache_id');
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$this->assertFalse($cache->contains('testing_result_cache_id'));
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-10-23 02:39:37 +04:00
|
|
|
$users = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertTrue($cache->contains('testing_result_cache_id'));
|
|
|
|
}
|
2009-10-24 04:28:43 +04:00
|
|
|
|
|
|
|
public function testUseResultCache()
|
|
|
|
{
|
|
|
|
$cache = new \Doctrine\Common\Cache\ArrayCache();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
|
|
|
|
$query->useResultCache(true);
|
2011-10-24 01:28:23 +04:00
|
|
|
$query->setResultCacheDriver($cache);
|
2009-10-24 04:28:43 +04:00
|
|
|
$query->setResultCacheId('testing_result_cache_id');
|
|
|
|
$users = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertTrue($cache->contains('testing_result_cache_id'));
|
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
|
2009-10-24 04:28:43 +04:00
|
|
|
}
|
2010-02-10 22:09:25 +03:00
|
|
|
|
2011-02-26 02:38:06 +03:00
|
|
|
/**
|
|
|
|
* @group DDC-1026
|
|
|
|
*/
|
|
|
|
public function testUseResultCacheParams()
|
|
|
|
{
|
|
|
|
$cache = new \Doctrine\Common\Cache\ArrayCache();
|
|
|
|
|
|
|
|
$sqlCount = count($this->_sqlLoggerStack->queries);
|
|
|
|
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');
|
|
|
|
$query->setParameter(1, 1);
|
2011-10-24 01:28:23 +04:00
|
|
|
$query->setResultCacheDriver($cache);
|
2011-02-26 02:38:06 +03:00
|
|
|
$query->useResultCache(true);
|
|
|
|
$query->getResult();
|
|
|
|
|
|
|
|
$query->setParameter(1, 2);
|
|
|
|
$query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "Two non-cached queries.");
|
|
|
|
|
|
|
|
$query->setParameter(1, 1);
|
|
|
|
$query->useResultCache(true);
|
|
|
|
$query->getResult();
|
|
|
|
|
|
|
|
$query->setParameter(1, 2);
|
|
|
|
$query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "The next two sql should have been cached, but were not.");
|
|
|
|
}
|
|
|
|
|
2010-02-10 22:09:25 +03:00
|
|
|
public function testNativeQueryResultCaching()
|
|
|
|
{
|
|
|
|
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
|
|
|
|
$rsm->addScalarResult('id', 'u');
|
|
|
|
$query = $this->_em->createNativeQuery('select u.id FROM cms_users u WHERE u.id = ?', $rsm);
|
|
|
|
$query->setParameter(1, 10);
|
|
|
|
|
|
|
|
$cache = new ArrayCache();
|
|
|
|
$query->setResultCacheDriver($cache)->useResultCache(true);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->assertEquals(0, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
$query->getResult();
|
2011-09-13 07:59:24 +04:00
|
|
|
$this->assertEquals(1, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-24 01:28:23 +04:00
|
|
|
* @param string $query
|
2010-02-10 22:09:25 +03:00
|
|
|
* @depends testNativeQueryResultCaching
|
|
|
|
*/
|
2011-10-24 01:28:23 +04:00
|
|
|
public function testResultCacheNotDependsOnQueryHints($query)
|
2010-02-10 22:09:25 +03:00
|
|
|
{
|
|
|
|
$cache = $query->getResultCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query->setHint('foo', 'bar');
|
|
|
|
$query->getResult();
|
|
|
|
|
2011-10-24 01:28:23 +04:00
|
|
|
$this->assertEquals($cacheCount, $this->getCacheSize($cache));
|
2010-02-10 22:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param <type> $query
|
|
|
|
* @depends testNativeQueryResultCaching
|
|
|
|
*/
|
|
|
|
public function testResultCacheDependsOnParameters($query)
|
|
|
|
{
|
|
|
|
$cache = $query->getResultCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2010-02-10 22:09:25 +03:00
|
|
|
|
|
|
|
$query->setParameter(1, 50);
|
|
|
|
$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 testNativeQueryResultCaching
|
|
|
|
*/
|
2011-10-24 01:28:23 +04:00
|
|
|
public function testResultCacheNotDependsOnHydrationMode($query)
|
2010-02-14 00:42:09 +03:00
|
|
|
{
|
|
|
|
$cache = $query->getResultCacheDriver();
|
2011-09-13 07:59:24 +04:00
|
|
|
$cacheCount = $this->getCacheSize($cache);
|
2010-02-14 00:42:09 +03:00
|
|
|
|
|
|
|
$this->assertNotEquals(\Doctrine\ORM\Query::HYDRATE_ARRAY, $query->getHydrationMode());
|
|
|
|
$query->getArrayResult();
|
|
|
|
|
2011-10-24 01:28:23 +04:00
|
|
|
$this->assertEquals($cacheCount, $this->getCacheSize($cache));
|
2010-02-14 00:42:09 +03:00
|
|
|
}
|
2010-12-03 19:34:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-909
|
|
|
|
*/
|
|
|
|
public function testResultCacheWithObjectParameter()
|
|
|
|
{
|
|
|
|
$user1 = new CmsUser;
|
|
|
|
$user1->name = 'Roman';
|
|
|
|
$user1->username = 'romanb';
|
|
|
|
$user1->status = 'dev';
|
|
|
|
|
|
|
|
$user2 = new CmsUser;
|
|
|
|
$user2->name = 'Benjamin';
|
|
|
|
$user2->username = 'beberlei';
|
|
|
|
$user2->status = 'dev';
|
|
|
|
|
|
|
|
$article = new CmsArticle();
|
|
|
|
$article->text = "foo";
|
|
|
|
$article->topic = "baz";
|
|
|
|
$article->user = $user1;
|
|
|
|
|
|
|
|
$this->_em->persist($article);
|
|
|
|
$this->_em->persist($user1);
|
|
|
|
$this->_em->persist($user2);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
|
|
|
|
$query->setParameter(1, $user1);
|
|
|
|
|
|
|
|
$cache = new ArrayCache();
|
|
|
|
|
|
|
|
$query->setResultCacheDriver($cache)->useResultCache(true);
|
|
|
|
|
|
|
|
$articles = $query->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($articles));
|
|
|
|
$this->assertEquals('baz', $articles[0]->topic);
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$query2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
|
|
|
|
$query2->setParameter(1, $user1);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-12-03 19:34:56 +03:00
|
|
|
$query2->setResultCacheDriver($cache)->useResultCache(true);
|
|
|
|
|
|
|
|
$articles = $query2->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($articles));
|
|
|
|
$this->assertEquals('baz', $articles[0]->topic);
|
|
|
|
|
|
|
|
$query3 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
|
|
|
|
$query3->setParameter(1, $user2);
|
|
|
|
|
|
|
|
$query3->setResultCacheDriver($cache)->useResultCache(true);
|
|
|
|
|
|
|
|
$articles = $query3->getResult();
|
|
|
|
|
|
|
|
$this->assertEquals(0, count($articles));
|
|
|
|
}
|
2009-10-23 02:39:37 +04:00
|
|
|
}
|