PageRenderTime 84ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/storage/CacheTest.php

http://github.com/UnionOfRAD/lithium
PHP | 665 lines | 520 code | 138 blank | 7 comment | 0 complexity | 3c41554a132321da0cc6408c12e25d35 MD5 | raw file
  1. <?php
  2. /**
  3. * li₃: the most RAD framework for PHP (http://li3.me)
  4. *
  5. * Copyright 2009, Union of RAD. All rights reserved. This source
  6. * code is distributed under the terms of the BSD 3-Clause License.
  7. * The full license text can be found in the LICENSE.txt file.
  8. */
  9. namespace lithium\tests\cases\storage;
  10. use lithium\storage\Cache;
  11. class CacheTest extends \lithium\test\Unit {
  12. public function setUp() {
  13. Cache::reset();
  14. }
  15. public function tearDown() {
  16. Cache::reset();
  17. }
  18. public function testBasicCacheConfig() {
  19. $result = Cache::config();
  20. $this->assertEmpty($result);
  21. $config = ['default' => ['adapter' => '\some\adapter', 'filters' => []]];
  22. $result = Cache::config($config);
  23. $this->assertNull($result);
  24. $expected = $config;
  25. $result = Cache::config();
  26. $this->assertEqual($expected, $result);
  27. $result = Cache::reset();
  28. $this->assertNull($result);
  29. $config = ['default' => ['adapter' => '\some\adapter', 'filters' => []]];
  30. Cache::config($config);
  31. $result = Cache::config();
  32. $expected = $config;
  33. $this->assertEqual($expected, $result);
  34. $result = Cache::reset();
  35. $this->assertNull($result);
  36. $config = ['default' => [
  37. 'adapter' => '\some\adapter',
  38. 'filters' => []
  39. ]];
  40. Cache::config($config);
  41. $result = Cache::config();
  42. $expected = $config;
  43. $this->assertEqual($expected, $result);
  44. }
  45. public function testKeyNoop() {
  46. Cache::config(['default' => ['adapter' => 'Memory']]);
  47. $key = 'this is a cache key';
  48. $result = Cache::key('default', $key);
  49. $expected = 'this is a cache key';
  50. $this->assertIdentical($expected, $result);
  51. $key = '1120-cache éë';
  52. $result = Cache::key('default', $key);
  53. $expected = '1120-cache éë';
  54. $this->assertIdentical($expected, $result);
  55. $result = Cache::key('default', 'foo');
  56. $expected = 'foo';
  57. $this->assertIdentical($expected, $result);
  58. $result = Cache::key('default', ['foo', 'bar']);
  59. $expected = ['foo', 'bar'];
  60. $this->assertIdentical($expected, $result);
  61. $result = Cache::key('default', ['foo' => 'bar', 'baz' => 'boo']);
  62. $expected = ['foo' => 'bar', 'baz' => 'boo'];
  63. $this->assertIdentical($expected, $result);
  64. }
  65. public function testKeyWithDataHash() {
  66. Cache::config(['default' => ['adapter' => 'Memory']]);
  67. $result = Cache::key('default', 'post', 2);
  68. $expected = 'post:1ad5be0d';
  69. $this->assertIdentical($expected, $result);
  70. $result = Cache::key('default', 'post', [2, 'json']);
  71. $expected = 'post:723f0e19';
  72. $this->assertIdentical($expected, $result);
  73. $result = Cache::key('default', ['posts', 'banners'], 'json');
  74. $expected = [
  75. 'posts:6b072545',
  76. 'banners:6b072545'
  77. ];
  78. $this->assertIdentical($expected, $result);
  79. $result = Cache::key('default', ['posts' => 'foo', 'banners' => 'bar'], 'json');
  80. $expected = [
  81. 'posts:6b072545' => 'foo',
  82. 'banners:6b072545' => 'bar'
  83. ];
  84. $this->assertIdentical($expected, $result);
  85. }
  86. public function testKeyWithGeneratorLambda() {
  87. Cache::config(['default' => ['adapter' => 'Memory']]);
  88. $key = function() {
  89. return 'lambda_key';
  90. };
  91. $result = Cache::key('default', $key);
  92. $expected = 'lambda_key';
  93. $this->assertIdentical($expected, $result);
  94. $key = function() {
  95. return 'lambda key';
  96. };
  97. $result = Cache::key('default', $key);
  98. $expected = 'lambda key';
  99. $this->assertIdentical($expected, $result);
  100. $key = function($data = []) {
  101. $defaults = ['foo' => 'foo', 'bar' => 'bar'];
  102. $data += $defaults;
  103. return 'composed_key_with:' . $data['foo'] . ':' . $data['bar'];
  104. };
  105. $result = Cache::key('default', $key, ['foo' => 'boo', 'bar' => 'far']);
  106. $expected = 'composed_key_with:boo:far';
  107. $this->assertIdentical($expected, $result);
  108. }
  109. public function testKeyWithGeneratorClosure() {
  110. Cache::config(['default' => ['adapter' => 'Memory']]);
  111. $value = 5;
  112. $key = function() use ($value) {
  113. return "closure key {$value}";
  114. };
  115. $result = Cache::key('default', $key);
  116. $expected = 'closure key 5';
  117. $this->assertIdentical($expected, $result);
  118. $reference = 'mutable';
  119. $key = function () use (&$reference) {
  120. $reference .= ' key';
  121. return $reference;
  122. };
  123. $result = Cache::key('default', $key);
  124. $expected = 'mutable key';
  125. $this->assertIdentical($expected, $result);
  126. $this->assertIdentical('mutable key', $reference);
  127. }
  128. public function testKeyWithGeneratorClosureAndArguments() {
  129. Cache::config(['default' => ['adapter' => 'Memory']]);
  130. $value = 'closure argument';
  131. $key = function($value) {
  132. return $value;
  133. };
  134. $result = Cache::key('default', $key($value));
  135. $expected = 'closure argument';
  136. $this->assertIdentical($expected, $result);
  137. }
  138. public function testCacheWrite() {
  139. $config = ['default' => [
  140. 'adapter' => 'Memory', 'filters' => []
  141. ]];
  142. Cache::config($config);
  143. $result = Cache::config();
  144. $expected = $config;
  145. $this->assertEqual($expected, $result);
  146. $result = Cache::write('default', 'some_key', 'some_data', '+1 minute');
  147. $this->assertTrue($result);
  148. $result = Cache::write('non_existing', 'key_value', 'data', '+1 minute');
  149. $this->assertFalse($result);
  150. }
  151. public function testCacheWriteMultipleItems() {
  152. $config = ['default' => [
  153. 'adapter' => 'Memory', 'filters' => [], 'strategies' => []
  154. ]];
  155. Cache::config($config);
  156. $result = Cache::config();
  157. $expected = $config;
  158. $this->assertEqual($expected, $result);
  159. $data = [
  160. 'key1' => 'value1',
  161. 'key2' => 'value2',
  162. 'key3' => 'value3'
  163. ];
  164. $result = Cache::write('default', $data, '+1 minute');
  165. $this->assertTrue($result);
  166. }
  167. public function testCacheReadMultipleItems() {
  168. $config = ['default' => [
  169. 'adapter' => 'Memory', 'filters' => [], 'strategies' => []
  170. ]];
  171. Cache::config($config);
  172. $result = Cache::config();
  173. $expected = $config;
  174. $this->assertEqual($expected, $result);
  175. $data = [
  176. 'read1' => 'value1',
  177. 'read2' => 'value2',
  178. 'read3' => 'value3'
  179. ];
  180. $result = Cache::write('default', $data, '+1 minute');
  181. $this->assertTrue($result);
  182. $keys = array_keys($data);
  183. $result = Cache::read('default', $keys);
  184. $this->assertEqual($data, $result);
  185. }
  186. public function testCacheReadWithConditions() {
  187. $config = ['default' => ['adapter' => 'Memory', 'filters' => []]];
  188. Cache::config($config);
  189. $result = Cache::config();
  190. $expected = $config;
  191. $this->assertEqual($expected, $result);
  192. $result = Cache::read('default', 'some_key', ['conditions' => function() {
  193. return false;
  194. }]);
  195. $this->assertFalse($result);
  196. $conditions = function() use (&$config) {
  197. return (isset($config['default']));
  198. };
  199. Cache::write('default', 'some_key', 'some value', '+1 minute');
  200. $result = Cache::read('default', 'some_key', compact('conditions'));
  201. $this->assertNotEmpty($result);
  202. $this->assertFalse(Cache::read('non_existing', 'key_value', compact('conditions')));
  203. }
  204. public function testCacheIncrementDecrementWithConditions() {
  205. $config = ['default' => [
  206. 'adapter' => 'Memory', 'filters' => []
  207. ]];
  208. Cache::config($config);
  209. $result = Cache::config();
  210. $expected = $config;
  211. $this->assertEqual($expected, $result);
  212. $conditions = function() {
  213. return false;
  214. };
  215. $result = Cache::increment('default', 'some_key', 1, compact('conditions'));
  216. $this->assertFalse($result);
  217. $conditions = function() use (&$config) {
  218. return (isset($config['default']));
  219. };
  220. Cache::write('default', 'some_key', 1, '+1 minute');
  221. $result = Cache::increment('default', 'some_key', 1, compact('conditions'));
  222. $this->assertEqual(2, $result);
  223. $conditions = function() {
  224. return false;
  225. };
  226. $result = Cache::decrement('default', 'decrement_some_key', 1, compact('conditions'));
  227. $this->assertFalse($result);
  228. $conditions = function() use (&$config) {
  229. return (isset($config['default']));
  230. };
  231. Cache::write('default', 'decrement_some_key', 1, '+1 minute');
  232. $result = Cache::decrement('default', 'decrement_some_key', 1, compact('conditions'));
  233. $this->assertEqual(0, $result);
  234. }
  235. public function testCacheWriteWithConditions() {
  236. $config = ['default' => [
  237. 'adapter' => 'Memory', 'filters' => []
  238. ]];
  239. Cache::config($config);
  240. $result = Cache::config();
  241. $expected = $config;
  242. $this->assertEqual($expected, $result);
  243. $conditions = function() {
  244. return false;
  245. };
  246. $result = Cache::write(
  247. 'default', 'some_key', 'some_data', '+1 minute', compact('conditions')
  248. );
  249. $this->assertFalse($result);
  250. $conditions = function() use (&$config) {
  251. return (isset($config['default']));
  252. };
  253. $result = Cache::write(
  254. 'default', 'some_key', 'some_data', '+1 minute', compact('conditions')
  255. );
  256. $this->assertTrue($result);
  257. $result = Cache::write(
  258. 'non_existing', 'key_value', 'data', '+1 minute', compact('conditions')
  259. );
  260. $this->assertFalse($result);
  261. }
  262. public function testCacheReadThroughWrite() {
  263. $config = ['default' => [
  264. 'adapter' => 'Memory', 'filters' => []
  265. ]];
  266. Cache::config($config);
  267. $result = Cache::config();
  268. $expected = $config;
  269. $this->assertEqual($expected, $result);
  270. $write = function() {
  271. return ['+1 minute' => 'read-through write'];
  272. };
  273. $this->assertNull(Cache::read('default', 'read_through'));
  274. $result = Cache::read('default', 'read_through', compact('write'));
  275. $this->assertIdentical('read-through write', $result);
  276. $result = Cache::read('default', 'read_through');
  277. $this->assertIdentical('read-through write', $result);
  278. $write = ['+1 minute' => 'string read-through write'];
  279. $result = Cache::read('default', 'string_read_through', compact('write'));
  280. $this->assertIdentical('string read-through write', $result);
  281. $result = Cache::read('default', 'string_read_through');
  282. $this->assertIdentical('string read-through write', $result);
  283. $this->assertNull(Cache::read('default', 'string_read_through_2'));
  284. $result = Cache::read('default', 'string_read_through_2', ['write' => [
  285. '+1 minute' => function() {
  286. return 'read-through write 2';
  287. }
  288. ]]);
  289. $this->assertIdentical('read-through write 2', $result);
  290. }
  291. public function testCacheReadThroughWriteNoCallWhenHasKey() {
  292. Cache::config(['default' => ['adapter' => 'Memory']]);
  293. $callCount = 0;
  294. Cache::write('default', 'foo', 'bar');
  295. $result = Cache::read('default', 'foo');
  296. $this->assertEqual('bar', $result);
  297. Cache::read('default', 'foo', ['write' => [
  298. '+1 minute' => function() use (&$callCount) {
  299. $callCount++;
  300. return 'baz';
  301. }
  302. ]]);
  303. $this->assertIdentical(0, $callCount);
  304. $result = Cache::read('default', 'foo');
  305. $this->assertEqual('bar', $result);
  306. }
  307. public function testCacheReadAndWrite() {
  308. $config = ['default' => [
  309. 'adapter' => 'Memory', 'filters' => []
  310. ]];
  311. Cache::config($config);
  312. $result = Cache::config();
  313. $expected = $config;
  314. $this->assertEqual($expected, $result);
  315. $result = Cache::read('non_existing', 'key_value');
  316. $this->assertFalse($result);
  317. $result = Cache::write('default', 'keyed', 'some data', '+1 minute');
  318. $this->assertTrue($result);
  319. $result = Cache::read('default', 'keyed');
  320. $expected = 'some data';
  321. $this->assertEqual($expected, $result);
  322. $result = Cache::write('default', 'another', ['data' => 'take two'], '+1 minute');
  323. $this->assertTrue($result);
  324. $result = Cache::read('default', 'another');
  325. $expected = ['data' => 'take two'];
  326. $this->assertEqual($expected, $result);
  327. $result = Cache::write(
  328. 'default', 'another', (object) ['data' => 'take two'], '+1 minute'
  329. );
  330. $this->assertTrue($result);
  331. $result = Cache::read('default', 'another');
  332. $expected = (object) ['data' => 'take two'];
  333. $this->assertEqual($expected, $result);
  334. }
  335. public function testCacheWriteAndReadNull() {
  336. Cache::config([
  337. 'default' => [
  338. 'adapter' => 'Memory'
  339. ]
  340. ]);
  341. $result = Cache::write('default', 'some_key', null);
  342. $this->assertTrue($result);
  343. $result = Cache::read('default', 'some_key');
  344. $this->assertNull($result);
  345. }
  346. public function testCacheWriteAndReadNullMulti() {
  347. Cache::config([
  348. 'default' => [
  349. 'adapter' => 'Memory'
  350. ]
  351. ]);
  352. $keys = [
  353. 'key1' => null,
  354. 'key2' => 'data2'
  355. ];
  356. $result = Cache::write('default', $keys);
  357. $this->assertTrue($result);
  358. $expected = [
  359. 'key1' => null,
  360. 'key2' => 'data2'
  361. ];
  362. $result = Cache::read('default', array_keys($keys));
  363. $this->assertEqual($expected, $result);
  364. $keys = [
  365. 'key1' => null,
  366. 'key2' => null
  367. ];
  368. $result = Cache::write('default', $keys);
  369. $this->assertTrue($result);
  370. $expected = [
  371. 'key1' => null,
  372. 'key2' => null
  373. ];
  374. $result = Cache::read('default', array_keys($keys));
  375. $this->assertEqual($expected, $result);
  376. }
  377. public function testCacheReadAndWriteWithConditions() {
  378. $config = ['default' => [
  379. 'adapter' => 'Memory', 'filters' => []
  380. ]];
  381. Cache::config($config);
  382. $result = Cache::config();
  383. $expected = $config;
  384. $this->assertEqual($expected, $result);
  385. $conditions = function() use (&$config) {
  386. return (isset($config['default']));
  387. };
  388. $result = Cache::read('non_existing', 'key_value', compact('conditions'));
  389. $this->assertFalse($result);
  390. $result = Cache::read('default', 'key_value', compact('conditions'));
  391. $this->assertEmpty($result);
  392. $result = Cache::write('default', 'keyed', 'some data', '+1 minute', compact('conditions'));
  393. $this->assertTrue($result);
  394. $conditions = function() {
  395. return false;
  396. };
  397. $result = Cache::write('default', 'keyed', 'some data', '+1 minute', compact('conditions'));
  398. $this->assertFalse($result);
  399. }
  400. public function testCacheWriteAndDelete() {
  401. $config = ['default' => [
  402. 'adapter' => 'Memory', 'filters' => []
  403. ]];
  404. Cache::config($config);
  405. $result = Cache::config();
  406. $expected = $config;
  407. $this->assertEqual($expected, $result);
  408. $result = Cache::delete('non_existing', 'key_value');
  409. $this->assertFalse($result);
  410. $result = Cache::write('default', 'to delete', 'dead data', '+1 minute');
  411. $this->assertTrue($result);
  412. $result = Cache::delete('default', 'to delete');
  413. $this->assertTrue($result);
  414. $this->assertEmpty(Cache::read('default', 'to delete'));
  415. }
  416. public function testCacheWriteAndDeleteWithConditions() {
  417. $config = ['default' => [
  418. 'adapter' => 'Memory', 'filters' => []
  419. ]];
  420. Cache::config($config);
  421. $result = Cache::config();
  422. $expected = $config;
  423. $this->assertEqual($expected, $result);
  424. $conditions = function() use (&$config) {
  425. return (isset($config['default']));
  426. };
  427. $result = Cache::delete('non_existing', 'key_value', compact('conditions'));
  428. $this->assertFalse($result);
  429. $result = Cache::write('default', 'to delete', 'dead data', '+1 minute');
  430. $this->assertTrue($result);
  431. $result = Cache::delete('default', 'to delete', [
  432. 'conditions' => function() {
  433. return false;
  434. }
  435. ]);
  436. $this->assertFalse($result);
  437. $result = Cache::delete('default', 'to delete', compact('conditions'));
  438. $this->assertTrue($result);
  439. }
  440. public function testCacheWriteAndClear() {
  441. $config = ['default' => [
  442. 'adapter' => 'Memory', 'filters' => []
  443. ]];
  444. Cache::config($config);
  445. $result = Cache::config();
  446. $expected = $config;
  447. $this->assertEqual($expected, $result);
  448. $result = Cache::clear('non_existing');
  449. $this->assertFalse($result);
  450. $result = Cache::write('default', 'to delete', 'dead data', '+1 minute');
  451. $this->assertTrue($result);
  452. $result = Cache::clear('default');
  453. $this->assertTrue($result);
  454. $result = Cache::read('default', 'to delete');
  455. $this->assertEmpty($result);
  456. }
  457. public function testClean() {
  458. $config = ['default' => [
  459. 'adapter' => 'Memory', 'filters' => []
  460. ]];
  461. Cache::config($config);
  462. $result = Cache::config();
  463. $expected = $config;
  464. $this->assertEqual($expected, $result);
  465. $result = Cache::clean('non_existing');
  466. $this->assertFalse($result);
  467. $result = Cache::clean('default');
  468. $this->assertFalse($result);
  469. }
  470. public function testReset() {
  471. $config = ['default' => [
  472. 'adapter' => 'Memory', 'filters' => []
  473. ]];
  474. Cache::config($config);
  475. $result = Cache::config();
  476. $expected = $config;
  477. $this->assertEqual($expected, $result);
  478. $result = Cache::reset();
  479. $this->assertNull($result);
  480. $result = Cache::config();
  481. $this->assertEmpty($result);
  482. }
  483. public function testIncrement() {
  484. $config = ['default' => [
  485. 'adapter' => 'Memory', 'filters' => []
  486. ]];
  487. Cache::config($config);
  488. $result = Cache::config();
  489. $expected = $config;
  490. $this->assertEqual($expected, $result);
  491. $result = Cache::increment('does_not_exist', 'inc');
  492. $this->assertFalse($result);
  493. $result = Cache::write('default', 'increment', 5, '+1 minute');
  494. $this->assertTrue($result);
  495. $result = Cache::increment('default', 'increment');
  496. $this->assertNotEmpty($result);
  497. $result = Cache::read('default', 'increment');
  498. $this->assertEqual(6, $result);
  499. }
  500. public function testDecrement() {
  501. $config = ['default' => [
  502. 'adapter' => 'Memory', 'filters' => []
  503. ]];
  504. Cache::config($config);
  505. $result = Cache::config();
  506. $expected = $config;
  507. $this->assertEqual($expected, $result);
  508. $result = Cache::decrement('does_not_exist', 'dec');
  509. $this->assertFalse($result);
  510. $result = Cache::write('default', 'decrement', 5, '+1 minute');
  511. $this->assertTrue($result);
  512. $result = Cache::decrement('default', 'decrement');
  513. $this->assertNotEmpty($result);
  514. $result = Cache::read('default', 'decrement');
  515. $this->assertEqual(4, $result);
  516. }
  517. public function testNonPortableCacheAdapterMethod() {
  518. $config = ['default' => [
  519. 'adapter' => 'Memory', 'filters' => []
  520. ]];
  521. Cache::config($config);
  522. $result = Cache::config();
  523. $expected = $config;
  524. $this->assertEqual($expected, $result);
  525. }
  526. }
  527. ?>