QueryCacheTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\CMS\CmsUser;
  4. use Doctrine\Common\Cache\ArrayCache;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. /**
  7. * QueryCacheTest
  8. *
  9. * @author robo
  10. */
  11. class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
  12. {
  13. /**
  14. * @var \ReflectionProperty
  15. */
  16. private $cacheDataReflection;
  17. protected function setUp() {
  18. $this->cacheDataReflection = new \ReflectionProperty("Doctrine\Common\Cache\ArrayCache", "data");
  19. $this->cacheDataReflection->setAccessible(true);
  20. $this->useModelSet('cms');
  21. parent::setUp();
  22. }
  23. /**
  24. * @param ArrayCache $cache
  25. * @return integer
  26. */
  27. private function getCacheSize(ArrayCache $cache)
  28. {
  29. return sizeof($this->cacheDataReflection->getValue($cache));
  30. }
  31. public function testQueryCache_DependsOnHints()
  32. {
  33. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  34. $cache = new ArrayCache();
  35. $query->setQueryCacheDriver($cache);
  36. $query->getResult();
  37. $this->assertEquals(1, $this->getCacheSize($cache));
  38. $query->setHint('foo', 'bar');
  39. $query->getResult();
  40. $this->assertEquals(2, $this->getCacheSize($cache));
  41. return $query;
  42. }
  43. /**
  44. * @param <type> $query
  45. * @depends testQueryCache_DependsOnHints
  46. */
  47. public function testQueryCache_DependsOnFirstResult($query)
  48. {
  49. $cache = $query->getQueryCacheDriver();
  50. $cacheCount = $this->getCacheSize($cache);
  51. $query->setFirstResult(10);
  52. $query->setMaxResults(9999);
  53. $query->getResult();
  54. $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
  55. }
  56. /**
  57. * @param <type> $query
  58. * @depends testQueryCache_DependsOnHints
  59. */
  60. public function testQueryCache_DependsOnMaxResults($query)
  61. {
  62. $cache = $query->getQueryCacheDriver();
  63. $cacheCount = $this->getCacheSize($cache);
  64. $query->setMaxResults(10);
  65. $query->getResult();
  66. $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
  67. }
  68. /**
  69. * @param <type> $query
  70. * @depends testQueryCache_DependsOnHints
  71. */
  72. public function testQueryCache_DependsOnHydrationMode($query)
  73. {
  74. $cache = $query->getQueryCacheDriver();
  75. $cacheCount = $this->getCacheSize($cache);
  76. $query->getArrayResult();
  77. $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
  78. }
  79. public function testQueryCache_NoHitSaveParserResult()
  80. {
  81. $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
  82. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  83. $cache = $this->getMock('Doctrine\Common\Cache\ArrayCache', array('doFetch', 'doSave', 'doGetStats'));
  84. $cache->expects($this->at(0))
  85. ->method('doFetch')
  86. ->with($this->isType('string'))
  87. ->will($this->returnValue(false));
  88. $cache->expects($this->at(1))
  89. ->method('doSave')
  90. ->with($this->isType('string'), $this->isInstanceOf('Doctrine\ORM\Query\ParserResult'), $this->equalTo(null));
  91. $query->setQueryCacheDriver($cache);
  92. $users = $query->getResult();
  93. }
  94. public function testQueryCache_HitDoesNotSaveParserResult()
  95. {
  96. $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
  97. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  98. $sqlExecMock = $this->getMock('Doctrine\ORM\Query\Exec\AbstractSqlExecutor', array('execute'));
  99. $sqlExecMock->expects($this->once())
  100. ->method('execute')
  101. ->will($this->returnValue( 10 ));
  102. $parserResultMock = $this->getMock('Doctrine\ORM\Query\ParserResult');
  103. $parserResultMock->expects($this->once())
  104. ->method('getSqlExecutor')
  105. ->will($this->returnValue($sqlExecMock));
  106. $cache = $this->getMock('Doctrine\Common\Cache\CacheProvider',
  107. array('doFetch', 'doContains', 'doSave', 'doDelete', 'doFlush', 'doGetStats'));
  108. $cache->expects($this->once())
  109. ->method('doFetch')
  110. ->with($this->isType('string'))
  111. ->will($this->returnValue($parserResultMock));
  112. $cache->expects($this->never())
  113. ->method('doSave');
  114. $query->setQueryCacheDriver($cache);
  115. $users = $query->getResult();
  116. }
  117. }