QueryCacherTest.php 3.4 KB

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