2 namespace Doctrine\Tests\ORM\Functional;
4 use Doctrine\Tests\OrmFunctionalTestCase;
5 use Doctrine\Tests\Models\Cms\CmsUser;
6 use Doctrine\DBAL\Cache\QueryCacheProfile;
7 use Doctrine\Common\Cache\ArrayCache;
12 class HydrationCacheTest extends OrmFunctionalTestCase
14 public function setUp()
16 $this->useModelSet('cms');
21 $user->name = "Benjamin";
22 $user->username = "beberlei";
23 $user->status = 'active';
25 $this->_em->persist($user);
30 public function testHydrationCache()
32 $cache = new ArrayCache();
33 $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u";
35 $users = $this->_em->createQuery($dql)
36 ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
39 $c = $this->getCurrentQueryCount();
40 $users = $this->_em->createQuery($dql)
41 ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
44 $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
46 $users = $this->_em->createQuery($dql)
47 ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
50 $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration is part of cache key.");
52 $users = $this->_em->createQuery($dql)
53 ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
56 $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration now cached");
58 $users = $this->_em->createQuery($dql)
59 ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
62 $this->assertTrue($cache->contains('cachekey'), 'Explicit cache key');
64 $users = $this->_em->createQuery($dql)
65 ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
67 $this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached");
70 public function testHydrationParametersSerialization()
72 $cache = new ArrayCache();
74 $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u WHERE u.id = ?1";
75 $query = $this->_em->createQuery($dql)
76 ->setParameter(1, $userId = 1)
77 ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache));
81 $c = $this->getCurrentQueryCount();
85 $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");