ResultCacheTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\CMS\CmsUser;
  4. use Doctrine\Tests\Models\CMS\CmsArticle;
  5. use Doctrine\Common\Cache\ArrayCache;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * ResultCacheTest
  9. *
  10. * @author robo
  11. */
  12. class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
  13. {
  14. protected function setUp() {
  15. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  16. $this->markTestSkipped('Test not compatible with 2.2 common');
  17. }
  18. $this->useModelSet('cms');
  19. parent::setUp();
  20. }
  21. public function testResultCache()
  22. {
  23. $user = new CmsUser;
  24. $user->name = 'Roman';
  25. $user->username = 'romanb';
  26. $user->status = 'dev';
  27. $this->_em->persist($user);
  28. $this->_em->flush();
  29. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  30. $cache = new ArrayCache();
  31. $query->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
  32. $this->assertFalse($cache->contains('my_cache_id'));
  33. $users = $query->getResult();
  34. $this->assertTrue($cache->contains('my_cache_id'));
  35. $this->assertEquals(1, count($users));
  36. $this->assertEquals('Roman', $users[0]->name);
  37. $this->_em->clear();
  38. $query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  39. $query2->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
  40. $users = $query2->getResult();
  41. $this->assertTrue($cache->contains('my_cache_id'));
  42. $this->assertEquals(1, count($users));
  43. $this->assertEquals('Roman', $users[0]->name);
  44. }
  45. public function testSetResultCacheId()
  46. {
  47. $cache = new ArrayCache;
  48. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  49. $query->setResultCacheDriver($cache);
  50. $query->setResultCacheId('testing_result_cache_id');
  51. $this->assertFalse($cache->contains('testing_result_cache_id'));
  52. $users = $query->getResult();
  53. $this->assertTrue($cache->contains('testing_result_cache_id'));
  54. }
  55. public function testUseResultCache()
  56. {
  57. $cache = new \Doctrine\Common\Cache\ArrayCache();
  58. $this->_em->getConfiguration()->setResultCacheImpl($cache);
  59. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  60. $query->useResultCache(true);
  61. $query->setResultCacheId('testing_result_cache_id');
  62. $users = $query->getResult();
  63. $this->assertTrue($cache->contains('testing_result_cache_id'));
  64. $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
  65. }
  66. /**
  67. * @group DDC-1026
  68. */
  69. public function testUseResultCacheParams()
  70. {
  71. $cache = new \Doctrine\Common\Cache\ArrayCache();
  72. $this->_em->getConfiguration()->setResultCacheImpl($cache);
  73. $sqlCount = count($this->_sqlLoggerStack->queries);
  74. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');
  75. $query->setParameter(1, 1);
  76. $query->useResultCache(true);
  77. $query->getResult();
  78. $query->setParameter(1, 2);
  79. $query->getResult();
  80. $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "Two non-cached queries.");
  81. $query->setParameter(1, 1);
  82. $query->useResultCache(true);
  83. $query->getResult();
  84. $query->setParameter(1, 2);
  85. $query->getResult();
  86. $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "The next two sql should have been cached, but were not.");
  87. }
  88. public function testNativeQueryResultCaching()
  89. {
  90. $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
  91. $rsm->addScalarResult('id', 'u');
  92. $query = $this->_em->createNativeQuery('select u.id FROM cms_users u WHERE u.id = ?', $rsm);
  93. $query->setParameter(1, 10);
  94. $cache = new ArrayCache();
  95. $query->setResultCacheDriver($cache)->useResultCache(true);
  96. $this->assertEquals(0, count($cache->getIds()));
  97. $query->getResult();
  98. $this->assertEquals(1, count($cache->getIds()));
  99. return $query;
  100. }
  101. /**
  102. * @param <type> $query
  103. * @depends testNativeQueryResultCaching
  104. */
  105. public function testResultCacheDependsOnQueryHints($query)
  106. {
  107. $cache = $query->getResultCacheDriver();
  108. $cacheCount = count($cache->getIds());
  109. $query->setHint('foo', 'bar');
  110. $query->getResult();
  111. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  112. }
  113. /**
  114. * @param <type> $query
  115. * @depends testNativeQueryResultCaching
  116. */
  117. public function testResultCacheDependsOnParameters($query)
  118. {
  119. $cache = $query->getResultCacheDriver();
  120. $cacheCount = count($cache->getIds());
  121. $query->setParameter(1, 50);
  122. $query->getResult();
  123. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  124. }
  125. /**
  126. * @param <type> $query
  127. * @depends testNativeQueryResultCaching
  128. */
  129. public function testResultCacheDependsOnHydrationMode($query)
  130. {
  131. $cache = $query->getResultCacheDriver();
  132. $cacheCount = count($cache->getIds());
  133. $this->assertNotEquals(\Doctrine\ORM\Query::HYDRATE_ARRAY, $query->getHydrationMode());
  134. $query->getArrayResult();
  135. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  136. }
  137. /**
  138. * @group DDC-909
  139. */
  140. public function testResultCacheWithObjectParameter()
  141. {
  142. $user1 = new CmsUser;
  143. $user1->name = 'Roman';
  144. $user1->username = 'romanb';
  145. $user1->status = 'dev';
  146. $user2 = new CmsUser;
  147. $user2->name = 'Benjamin';
  148. $user2->username = 'beberlei';
  149. $user2->status = 'dev';
  150. $article = new CmsArticle();
  151. $article->text = "foo";
  152. $article->topic = "baz";
  153. $article->user = $user1;
  154. $this->_em->persist($article);
  155. $this->_em->persist($user1);
  156. $this->_em->persist($user2);
  157. $this->_em->flush();
  158. $query = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
  159. $query->setParameter(1, $user1);
  160. $cache = new ArrayCache();
  161. $query->setResultCacheDriver($cache)->useResultCache(true);
  162. $articles = $query->getResult();
  163. $this->assertEquals(1, count($articles));
  164. $this->assertEquals('baz', $articles[0]->topic);
  165. $this->_em->clear();
  166. $query2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
  167. $query2->setParameter(1, $user1);
  168. $query2->setResultCacheDriver($cache)->useResultCache(true);
  169. $articles = $query2->getResult();
  170. $this->assertEquals(1, count($articles));
  171. $this->assertEquals('baz', $articles[0]->topic);
  172. $query3 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
  173. $query3->setParameter(1, $user2);
  174. $query3->setResultCacheDriver($cache)->useResultCache(true);
  175. $articles = $query3->getResult();
  176. $this->assertEquals(0, count($articles));
  177. }
  178. }