SimpleCacheEngineTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.7.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Cache;
  16. use Cake\Cache\Cache;
  17. use Cake\Cache\Engine\FileEngine;
  18. use Cake\Cache\SimpleCacheEngine;
  19. use Cake\TestSuite\TestCase;
  20. use Psr\SimpleCache\InvalidArgumentException;
  21. /**
  22. * SimpleCacheEngine class
  23. */
  24. class SimpleCacheEngineTest extends TestCase
  25. {
  26. public function setUp()
  27. {
  28. parent::setUp();
  29. $this->inner = new FileEngine();
  30. $this->inner->init([
  31. 'prefix' => '',
  32. 'path' => TMP . 'tests',
  33. 'duration' => 5,
  34. ]);
  35. $this->cache = new SimpleCacheEngine($this->inner);
  36. }
  37. public function tearDown()
  38. {
  39. parent::tearDown();
  40. $this->inner->clear(false);
  41. }
  42. public function testGetSuccess()
  43. {
  44. $this->inner->write('key_one', 'Some Value');
  45. $this->assertSame('Some Value', $this->cache->get('key_one'));
  46. $this->assertSame('Some Value', $this->cache->get('key_one', 'default'));
  47. }
  48. public function testGetNoKey()
  49. {
  50. $this->assertSame('default', $this->cache->get('no', 'default'));
  51. $this->assertNull($this->cache->get('no'));
  52. }
  53. public function testGetInvalidKey()
  54. {
  55. $this->expectException(InvalidArgumentException::class);
  56. $this->cache->get('');
  57. }
  58. public function testSetNoTtl()
  59. {
  60. $this->assertTrue($this->cache->set('key', 'a value'));
  61. $this->assertSame('a value', $this->cache->get('key'));
  62. }
  63. public function testSetWithTtl()
  64. {
  65. $this->assertTrue($this->cache->set('key', 'a value'));
  66. $this->assertTrue($this->cache->set('expired', 'a value', 0));
  67. sleep(1);
  68. $this->assertSame('a value', $this->cache->get('key'));
  69. $this->assertNull($this->cache->get('expired'));
  70. $this->assertSame(5, $this->inner->getConfig('duration'));
  71. }
  72. public function testSetInvalidKey()
  73. {
  74. $this->expectException(InvalidArgumentException::class);
  75. $this->cache->set('', 'some data');
  76. }
  77. public function testDelete()
  78. {
  79. $this->cache->set('key', 'a value');
  80. $this->assertTrue($this->cache->delete('key'));
  81. $this->assertFalse($this->cache->delete('undefined'));
  82. }
  83. public function testDeleteInvalidKey()
  84. {
  85. $this->expectException(InvalidArgumentException::class);
  86. $this->cache->delete('');
  87. }
  88. public function testClear()
  89. {
  90. $this->cache->set('key', 'a value');
  91. $this->cache->set('key2', 'other value');
  92. $this->assertTrue($this->cache->clear());
  93. $this->assertNull($this->cache->get('key'));
  94. $this->assertNull($this->cache->get('key2'));
  95. }
  96. public function testGetMultiple()
  97. {
  98. $this->cache->set('key', 'a value');
  99. $this->cache->set('key2', 'other value');
  100. $results = $this->cache->getMultiple(['key', 'key2', 'no']);
  101. $expected = [
  102. 'key' => 'a value',
  103. 'key2' => 'other value',
  104. 'no' => null,
  105. ];
  106. $this->assertSame($expected, $results);
  107. }
  108. public function testGetMultipleDefault()
  109. {
  110. $this->cache->set('key', 'a value');
  111. $this->cache->set('key2', 'other value');
  112. $results = $this->cache->getMultiple(['key', 'key2', 'no'], 'default value');
  113. $expected = [
  114. 'key' => 'a value',
  115. 'key2' => 'other value',
  116. 'no' => 'default value',
  117. ];
  118. $this->assertSame($expected, $results);
  119. }
  120. public function testSetMultiple()
  121. {
  122. $data = [
  123. 'key' => 'a value',
  124. 'key2' => 'other value'
  125. ];
  126. $this->cache->setMultiple($data);
  127. $results = $this->cache->getMultiple(array_keys($data));
  128. $this->assertSame($data, $results);
  129. }
  130. public function testSetMultipleWithTtl()
  131. {
  132. $data = [
  133. 'key' => 'a value',
  134. 'key2' => 'other value'
  135. ];
  136. $this->cache->setMultiple($data, 0);
  137. sleep(1);
  138. $results = $this->cache->getMultiple(array_keys($data));
  139. $this->assertNull($results['key']);
  140. $this->assertNull($results['key2']);
  141. $this->assertSame(5, $this->inner->getConfig('duration'));
  142. }
  143. public function testDeleteMultiple()
  144. {
  145. $data = [
  146. 'key' => 'a value',
  147. 'key2' => 'other value',
  148. 'key3' => 'more data',
  149. ];
  150. $this->cache->setMultiple($data);
  151. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  152. $this->assertNull($this->cache->get('key'));
  153. $this->assertNull($this->cache->get('key3'));
  154. $this->assertSame('other value', $this->cache->get('key2'));
  155. }
  156. public function testDeleteMultipleSomeMisses()
  157. {
  158. $data = [
  159. 'key' => 'a value',
  160. ];
  161. $this->cache->setMultiple($data);
  162. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  163. }
  164. public function testHas()
  165. {
  166. $this->assertFalse($this->cache->has('key'));
  167. $this->cache->set('key', 'value');
  168. $this->assertTrue($this->cache->has('key'));
  169. }
  170. public function testHasInvalidKey()
  171. {
  172. $this->expectException(InvalidArgumentException::class);
  173. $this->cache->has('');
  174. }
  175. }