QueryCacherTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Datasource;
  16. use Cake\Cache\Cache;
  17. use Cake\Datasource\QueryCacher;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Query cacher test
  21. */
  22. class QueryCacherTest extends TestCase
  23. {
  24. /**
  25. * Setup method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->engine = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  33. $this->engine->expects($this->any())
  34. ->method('init')
  35. ->will($this->returnValue(true));
  36. Cache::config('queryCache', $this->engine);
  37. Cache::enable();
  38. }
  39. /**
  40. * Teardown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. parent::tearDown();
  47. Cache::drop('queryCache');
  48. }
  49. /**
  50. * Test fetching with a function to generate the key.
  51. *
  52. * @return void
  53. */
  54. public function testFetchFunctionKey()
  55. {
  56. $this->_mockRead('my_key', 'A winner');
  57. $query = $this->getMockBuilder('stdClass')->getMock();
  58. $cacher = new QueryCacher(function ($q) use ($query) {
  59. $this->assertSame($query, $q);
  60. return 'my_key';
  61. }, 'queryCache');
  62. $result = $cacher->fetch($query);
  63. $this->assertEquals('A winner', $result);
  64. }
  65. /**
  66. * Test fetching with a function to generate the key but the function is poop.
  67. *
  68. * @return void
  69. */
  70. public function testFetchFunctionKeyNoString()
  71. {
  72. $this->expectException(\RuntimeException::class);
  73. $this->expectExceptionMessage('Cache key functions must return a string. Got false.');
  74. $this->_mockRead('my_key', 'A winner');
  75. $query = $this->getMockBuilder('stdClass')->getMock();
  76. $cacher = new QueryCacher(function ($q) {
  77. return false;
  78. }, 'queryCache');
  79. $cacher->fetch($query);
  80. }
  81. /**
  82. * Test fetching with a cache instance.
  83. *
  84. * @return void
  85. */
  86. public function testFetchCacheHitStringEngine()
  87. {
  88. $this->_mockRead('my_key', 'A winner');
  89. $cacher = new QueryCacher('my_key', 'queryCache');
  90. $query = $this->getMockBuilder('stdClass')->getMock();
  91. $result = $cacher->fetch($query);
  92. $this->assertEquals('A winner', $result);
  93. }
  94. /**
  95. * Test fetching with a cache hit.
  96. *
  97. * @return void
  98. */
  99. public function testFetchCacheHit()
  100. {
  101. $this->_mockRead('my_key', 'A winner');
  102. $cacher = new QueryCacher('my_key', $this->engine);
  103. $query = $this->getMockBuilder('stdClass')->getMock();
  104. $result = $cacher->fetch($query);
  105. $this->assertEquals('A winner', $result);
  106. }
  107. /**
  108. * Test fetching with a cache miss.
  109. *
  110. * @return void
  111. */
  112. public function testFetchCacheMiss()
  113. {
  114. $this->_mockRead('my_key', false);
  115. $cacher = new QueryCacher('my_key', $this->engine);
  116. $query = $this->getMockBuilder('stdClass')->getMock();
  117. $result = $cacher->fetch($query);
  118. $this->assertNull($result, 'Cache miss should not have an isset() return.');
  119. }
  120. /**
  121. * Helper for building mocks.
  122. */
  123. protected function _mockRead($key, $value = false)
  124. {
  125. $this->engine->expects($this->any())
  126. ->method('read')
  127. ->with($key)
  128. ->will($this->returnValue($value));
  129. }
  130. }