cacheTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace tests\thinkphp\library\think;
  12. use tests\thinkphp\library\think\config\ConfigInitTrait;
  13. use think\Cache;
  14. use think\Config;
  15. class cacheTest extends \PHPUnit_Framework_TestCase
  16. {
  17. use ConfigInitTrait {
  18. ConfigInitTrait::tearDown as ConfigTearDown;
  19. }
  20. public function tearDown()
  21. {
  22. $this->ConfigTearDown();
  23. call_user_func(\Closure::bind(function () {
  24. Cache::$handler = null;
  25. Cache::$instance = [];
  26. Cache::$readTimes = 0;
  27. Cache::$writeTimes = 0;
  28. }, null, '\think\Cache'));
  29. }
  30. /**
  31. * @dataProvider provideTestConnect
  32. */
  33. public function testConnect($options, $expected)
  34. {
  35. $connection = Cache::connect($options);
  36. $this->assertInstanceOf($expected, $connection);
  37. $this->assertSame($connection, Cache::connect($options));
  38. $instance = $this->getPropertyVal('instance');
  39. $this->assertArrayHasKey(md5(serialize($options)), $instance);
  40. $newConnection = Cache::connect($options, true);
  41. $newInstance = $this->getPropertyVal('instance');
  42. $this->assertInstanceOf($expected, $connection);
  43. $this->assertNotSame($connection, $newConnection);
  44. $this->assertEquals($instance, $newInstance);
  45. }
  46. /**
  47. * @dataProvider provideTestInit
  48. */
  49. public function testInit($options, $expected)
  50. {
  51. $connection = Cache::init($options);
  52. $this->assertInstanceOf($expected, $connection);
  53. $connectionNew = Cache::init(['type' => 'foo']);
  54. $this->assertSame($connection, $connectionNew);
  55. }
  56. public function testStore()
  57. {
  58. Config::set('cache.redis', ['type' => 'redis']);
  59. $connectionDefault = Cache::store();
  60. $this->assertInstanceOf('\think\cache\driver\File', $connectionDefault);
  61. Config::set('cache.type', false);
  62. $connectionNotRedis = Cache::store('redis');
  63. $this->assertSame($connectionDefault, $connectionNotRedis);
  64. Config::set('cache.type', 'complex');
  65. $connectionRedis = Cache::store('redis');
  66. $this->assertNotSame($connectionNotRedis, $connectionRedis);
  67. $this->assertInstanceOf('\think\cache\driver\Redis', $connectionRedis);
  68. // 即便成功切换到其他存储类型,也不影响原先的操作句柄
  69. $this->assertSame($connectionDefault, Cache::store());
  70. }
  71. public function testHas()
  72. {
  73. $key = $this->buildTestKey('Has');
  74. $this->assertFalse(Cache::has($key));
  75. Cache::set($key, 5);
  76. $this->assertTrue(Cache::has($key));
  77. }
  78. public function testGet()
  79. {
  80. $key = $this->buildTestKey('Get');
  81. $this->assertFalse(Cache::get($key));
  82. $this->assertEquals('default', Cache::get($key, 'default'));
  83. Cache::set($key, 5);
  84. $this->assertSame(5, Cache::get($key));
  85. }
  86. public function testSet()
  87. {
  88. $key = $this->buildTestKey('Set');
  89. $this->assertTrue(Cache::set(null, null));
  90. $this->assertTrue(Cache::set($key, 'ThinkPHP3.2'));
  91. $this->assertTrue(Cache::set($key, 'ThinkPHP5.0', null));
  92. $this->assertTrue(Cache::set($key, 'ThinkPHP5.0', -1));
  93. $this->assertTrue(Cache::set($key, 'ThinkPHP5.0', 0));
  94. $this->assertTrue(Cache::set($key, 'ThinkPHP5.0', 7200));
  95. }
  96. public function testInc()
  97. {
  98. $key = $this->buildTestKey('Inc');
  99. Cache::inc($key);
  100. $this->assertEquals(1, Cache::get($key));
  101. Cache::inc($key, '2');
  102. $this->assertEquals(3, Cache::get($key));
  103. Cache::inc($key, -1);
  104. $this->assertEquals(2, Cache::get($key));
  105. Cache::inc($key, null);
  106. $this->assertEquals(2, Cache::get($key));
  107. Cache::inc($key, true);
  108. $this->assertEquals(3, Cache::get($key));
  109. Cache::inc($key, false);
  110. $this->assertEquals(3, Cache::get($key));
  111. Cache::inc($key, 0.789);
  112. $this->assertEquals(3.789, Cache::get($key));
  113. }
  114. public function testDec()
  115. {
  116. $key = $this->buildTestKey('Dec');
  117. Cache::dec($key);
  118. $this->assertEquals(-1, Cache::get($key));
  119. Cache::dec($key, '2');
  120. $this->assertEquals(-3, Cache::get($key));
  121. Cache::dec($key, -1);
  122. $this->assertEquals(-2, Cache::get($key));
  123. Cache::dec($key, null);
  124. $this->assertEquals(-2, Cache::get($key));
  125. Cache::dec($key, true);
  126. $this->assertEquals(-3, Cache::get($key));
  127. Cache::dec($key, false);
  128. $this->assertEquals(-3, Cache::get($key));
  129. Cache::dec($key, 0.359);
  130. $this->assertEquals(-3.359, Cache::get($key));
  131. }
  132. public function testRm()
  133. {
  134. $key = $this->buildTestKey('Rm');
  135. $this->assertFalse(Cache::rm($key));
  136. Cache::set($key, 'ThinkPHP');
  137. $this->assertTrue(Cache::rm($key));
  138. }
  139. public function testClear()
  140. {
  141. $key1 = $this->buildTestKey('Clear1');
  142. $key2 = $this->buildTestKey('Clear2');
  143. Cache::set($key1, 'ThinkPHP3.2');
  144. Cache::set($key2, 'ThinkPHP5.0');
  145. $this->assertEquals('ThinkPHP3.2', Cache::get($key1));
  146. $this->assertEquals('ThinkPHP5.0', Cache::get($key2));
  147. Cache::clear();
  148. $this->assertFalse(Cache::get($key1));
  149. $this->assertFalse(Cache::get($key2));
  150. }
  151. public function testPull()
  152. {
  153. $key = $this->buildTestKey('Pull');
  154. $this->assertNull(Cache::pull($key));
  155. Cache::set($key, 'ThinkPHP');
  156. $this->assertEquals('ThinkPHP', Cache::pull($key));
  157. $this->assertFalse(Cache::get($key));
  158. }
  159. public function testRemember()
  160. {
  161. $key1 = $this->buildTestKey('Remember1');
  162. $key2 = $this->buildTestKey('Remember2');
  163. $this->assertEquals('ThinkPHP3.2', Cache::remember($key1, 'ThinkPHP3.2'));
  164. $this->assertEquals('ThinkPHP3.2', Cache::remember($key1, 'ThinkPHP5.0'));
  165. $this->assertEquals('ThinkPHP5.0', Cache::remember($key2, function () {
  166. return 'ThinkPHP5.0';
  167. }));
  168. $this->assertEquals('ThinkPHP5.0', Cache::remember($key2, function () {
  169. return 'ThinkPHP3.2';
  170. }));
  171. }
  172. public function testTag()
  173. {
  174. $key = $this->buildTestKey('Tag');
  175. $cacheTagWithoutName = Cache::tag(null);
  176. $this->assertInstanceOf('think\cache\Driver', $cacheTagWithoutName);
  177. $cacheTagWithKey = Cache::tag($key, [1, 2, 3]);
  178. $this->assertSame($cacheTagWithoutName, $cacheTagWithKey);
  179. }
  180. protected function getPropertyVal($name)
  181. {
  182. static $reflectionClass;
  183. if (empty($reflectionClass)) {
  184. $reflectionClass = new \ReflectionClass('\think\Cache');
  185. }
  186. $property = $reflectionClass->getProperty($name);
  187. $property->setAccessible(true);
  188. return $property->getValue();
  189. }
  190. public function provideTestConnect()
  191. {
  192. $provideData = [];
  193. $options = ['type' => null];
  194. $expected = '\think\cache\driver\File';
  195. $provideData[] = [$options, $expected];
  196. $options = ['type' => 'File'];
  197. $expected = '\think\cache\driver\File';
  198. $provideData[] = [$options, $expected];
  199. $options = ['type' => 'Lite'];
  200. $expected = '\think\cache\driver\Lite';
  201. $provideData[] = [$options, $expected];
  202. $options = ['type' => 'Memcached'];
  203. $expected = '\think\cache\driver\Memcached';
  204. $provideData[] = [$options, $expected];
  205. $options = ['type' => 'Redis'];
  206. $expected = '\think\cache\driver\Redis';
  207. $provideData[] = [$options, $expected];
  208. // TODO
  209. // $options = ['type' => 'Memcache'];
  210. // $expected = '\think\cache\driver\Memcache';
  211. // $provideData[] = [$options, $expected];
  212. //
  213. // $options = ['type' => 'Wincache'];
  214. // $expected = '\think\cache\driver\Wincache';
  215. // $provideData[] = [$options, $expected];
  216. // $options = ['type' => 'Sqlite'];
  217. // $expected = '\think\cache\driver\Sqlite';
  218. // $provideData[] = [$options, $expected];
  219. //
  220. // $options = ['type' => 'Xcache'];
  221. // $expected = '\think\cache\driver\Xcache';
  222. // $provideData[] = [$options, $expected];
  223. return $provideData;
  224. }
  225. public function provideTestInit()
  226. {
  227. $provideData = [];
  228. $options = [];
  229. $expected = '\think\cache\driver\File';
  230. $provideData[] = [$options, $expected];
  231. $options = ['type' => 'File'];
  232. $expected = '\think\cache\driver\File';
  233. $provideData[] = [$options, $expected];
  234. $options = ['type' => 'Lite'];
  235. $expected = '\think\cache\driver\Lite';
  236. $provideData[] = [$options, $expected];
  237. return $provideData;
  238. }
  239. protected function buildTestKey($tag)
  240. {
  241. return sprintf('ThinkPHP_Test_%s_%d_%d', $tag, time(), rand(0, 10000));
  242. }
  243. }