HydrationCacheTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\OrmFunctionalTestCase;
  4. use Doctrine\Tests\Models\Cms\CmsUser;
  5. use Doctrine\DBAL\Cache\QueryCacheProfile;
  6. use Doctrine\Common\Cache\ArrayCache;
  7. /**
  8. * @group DDC-1766
  9. */
  10. class HydrationCacheTest extends OrmFunctionalTestCase
  11. {
  12. public function setUp()
  13. {
  14. $this->useModelSet('cms');
  15. parent::setUp();
  16. $user = new CmsUser;
  17. $user->name = "Benjamin";
  18. $user->username = "beberlei";
  19. $user->status = 'active';
  20. $this->_em->persist($user);
  21. $this->_em->flush();
  22. $this->_em->clear();
  23. }
  24. public function testHydrationCache()
  25. {
  26. $cache = new ArrayCache();
  27. $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u";
  28. $users = $this->_em->createQuery($dql)
  29. ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  30. ->getResult();
  31. $c = $this->getCurrentQueryCount();
  32. $users = $this->_em->createQuery($dql)
  33. ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  34. ->getResult();
  35. $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
  36. $users = $this->_em->createQuery($dql)
  37. ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  38. ->getArrayResult();
  39. $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration is part of cache key.");
  40. $users = $this->_em->createQuery($dql)
  41. ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  42. ->getArrayResult();
  43. $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration now cached");
  44. $users = $this->_em->createQuery($dql)
  45. ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
  46. ->getArrayResult();
  47. $this->assertTrue($cache->contains('cachekey'), 'Explicit cache key');
  48. $users = $this->_em->createQuery($dql)
  49. ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
  50. ->getArrayResult();
  51. $this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached");
  52. }
  53. public function testHydrationParametersSerialization()
  54. {
  55. $cache = new ArrayCache();
  56. $user = new CmsUser();
  57. $user->id = 1;
  58. $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u WHERE u.id = ?1";
  59. $query = $this->_em->createQuery($dql)
  60. ->setParameter(1, $user)
  61. ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache));
  62. $query->getResult();
  63. $c = $this->getCurrentQueryCount();
  64. $query->getResult();
  65. $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
  66. }
  67. }