QueryCacherTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. use stdClass;
  21. /**
  22. * Query cacher test
  23. */
  24. class QueryCacherTest extends TestCase
  25. {
  26. /**
  27. * @var \Cake\Cache\CacheEngineInterface
  28. */
  29. protected $engine;
  30. /**
  31. * Setup method
  32. *
  33. * @return void
  34. */
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. Cache::setConfig('queryCache', ['className' => 'Array']);
  39. $this->engine = Cache::pool('queryCache');
  40. Cache::enable();
  41. }
  42. /**
  43. * Teardown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown(): void
  48. {
  49. parent::tearDown();
  50. Cache::drop('queryCache');
  51. }
  52. /**
  53. * Test fetching with a function to generate the key.
  54. *
  55. * @return void
  56. */
  57. public function testFetchFunctionKey()
  58. {
  59. $this->engine->set('my_key', 'A winner');
  60. $query = new stdClass();
  61. $cacher = new QueryCacher(function ($q) use ($query) {
  62. $this->assertSame($query, $q);
  63. return 'my_key';
  64. }, 'queryCache');
  65. $result = $cacher->fetch($query);
  66. $this->assertSame('A winner', $result);
  67. }
  68. /**
  69. * Test fetching with a function to generate the key but the function is poop.
  70. *
  71. * @return void
  72. */
  73. public function testFetchFunctionKeyNoString()
  74. {
  75. $this->expectException(\RuntimeException::class);
  76. $this->expectExceptionMessage('Cache key functions must return a string. Got false.');
  77. $this->engine->set('my_key', 'A winner');
  78. $query = new stdClass();
  79. $cacher = new QueryCacher(function ($q) {
  80. return false;
  81. }, 'queryCache');
  82. $cacher->fetch($query);
  83. }
  84. /**
  85. * Test fetching with a cache instance.
  86. *
  87. * @return void
  88. */
  89. public function testFetchCacheHitStringEngine()
  90. {
  91. $this->engine->set('my_key', 'A winner');
  92. $cacher = new QueryCacher('my_key', 'queryCache');
  93. $query = new stdClass();
  94. $result = $cacher->fetch($query);
  95. $this->assertSame('A winner', $result);
  96. }
  97. /**
  98. * Test fetching with a cache hit.
  99. *
  100. * @return void
  101. */
  102. public function testFetchCacheHit()
  103. {
  104. $this->engine->set('my_key', 'A winner');
  105. $cacher = new QueryCacher('my_key', $this->engine);
  106. $query = new stdClass();
  107. $result = $cacher->fetch($query);
  108. $this->assertSame('A winner', $result);
  109. }
  110. /**
  111. * Test fetching with a cache miss.
  112. *
  113. * @return void
  114. */
  115. public function testFetchCacheMiss()
  116. {
  117. $this->engine->set('my_key', false);
  118. $cacher = new QueryCacher('my_key', $this->engine);
  119. $query = new stdClass();
  120. $result = $cacher->fetch($query);
  121. $this->assertNull($result, 'Cache miss should not have an isset() return.');
  122. }
  123. }