QueryCacherTest.php 3.9 KB

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