QueryCacheTest.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. protected function setUp() {
  14. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  15. $this->markTestSkipped('Test not compatible with 2.2 common');
  16. }
  17. $this->useModelSet('cms');
  18. parent::setUp();
  19. }
  20. public function testQueryCache_DependsOnHints()
  21. {
  22. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  23. $cache = new ArrayCache();
  24. $query->setQueryCacheDriver($cache);
  25. $query->getResult();
  26. $this->assertEquals(1, count($cache->getIds()));
  27. $query->setHint('foo', 'bar');
  28. $query->getResult();
  29. $this->assertEquals(2, count($cache->getIds()));
  30. return $query;
  31. }
  32. /**
  33. * @param <type> $query
  34. * @depends testQueryCache_DependsOnHints
  35. */
  36. public function testQueryCache_DependsOnFirstResult($query)
  37. {
  38. $cache = $query->getQueryCacheDriver();
  39. $cacheCount = count($cache->getIds());
  40. $query->setFirstResult(10);
  41. $query->setMaxResults(9999);
  42. $query->getResult();
  43. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  44. }
  45. /**
  46. * @param <type> $query
  47. * @depends testQueryCache_DependsOnHints
  48. */
  49. public function testQueryCache_DependsOnMaxResults($query)
  50. {
  51. $cache = $query->getQueryCacheDriver();
  52. $cacheCount = count($cache->getIds());
  53. $query->setMaxResults(10);
  54. $query->getResult();
  55. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  56. }
  57. /**
  58. * @param <type> $query
  59. * @depends testQueryCache_DependsOnHints
  60. */
  61. public function testQueryCache_DependsOnHydrationMode($query)
  62. {
  63. $cache = $query->getQueryCacheDriver();
  64. $cacheCount = count($cache->getIds());
  65. $query->getArrayResult();
  66. $this->assertEquals($cacheCount + 1, count($cache->getIds()));
  67. }
  68. public function testQueryCache_NoHitSaveParserResult()
  69. {
  70. $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
  71. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  72. $cache = $this->getMock('Doctrine\Common\Cache\AbstractCache', array('_doFetch', '_doContains', '_doSave', '_doDelete', 'getIds'));
  73. $cache->expects($this->at(0))
  74. ->method('_doFetch')
  75. ->with($this->isType('string'))
  76. ->will($this->returnValue(false));
  77. $cache->expects($this->at(1))
  78. ->method('_doSave')
  79. ->with($this->isType('string'), $this->isInstanceOf('Doctrine\ORM\Query\ParserResult'), $this->equalTo(null));
  80. $query->setQueryCacheDriver($cache);
  81. $users = $query->getResult();
  82. }
  83. public function testQueryCache_HitDoesNotSaveParserResult()
  84. {
  85. $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache());
  86. $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
  87. $sqlExecMock = $this->getMock('Doctrine\ORM\Query\Exec\AbstractSqlExecutor', array('execute'));
  88. $sqlExecMock->expects($this->once())
  89. ->method('execute')
  90. ->will($this->returnValue( 10 ));
  91. $parserResultMock = $this->getMock('Doctrine\ORM\Query\ParserResult');
  92. $parserResultMock->expects($this->once())
  93. ->method('getSqlExecutor')
  94. ->will($this->returnValue($sqlExecMock));
  95. $cache = $this->getMock('Doctrine\Common\Cache\AbstractCache', array('_doFetch', '_doContains', '_doSave', '_doDelete', 'getIds'));
  96. $cache->expects($this->once())
  97. ->method('_doFetch')
  98. ->with($this->isType('string'))
  99. ->will($this->returnValue($parserResultMock));
  100. $cache->expects($this->never())
  101. ->method('_doSave');
  102. $query->setQueryCacheDriver($cache);
  103. $users = $query->getResult();
  104. }
  105. }