CacheTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.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\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * CacheTest class
  24. *
  25. */
  26. class CacheTest extends TestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Cache::enable();
  35. }
  36. /**
  37. * tearDown method
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. parent::tearDown();
  43. Cache::drop('tests');
  44. Cache::drop('test_trigger');
  45. }
  46. /**
  47. * Configure cache settings for test
  48. *
  49. * @return void
  50. */
  51. protected function _configCache() {
  52. Cache::config('tests', [
  53. 'engine' => 'File',
  54. 'path' => TMP,
  55. 'prefix' => 'test_'
  56. ]);
  57. }
  58. /**
  59. * Check that no fatal errors are issued doing normal things when Cache.disable is true.
  60. *
  61. * @return void
  62. */
  63. public function testNonFatalErrorsWithCacheDisable() {
  64. Cache::disable();
  65. $this->_configCache();
  66. $this->assertNull(Cache::write('no_save', 'Noooo!', 'tests'));
  67. $this->assertFalse(Cache::read('no_save', 'tests'));
  68. $this->assertNull(Cache::delete('no_save', 'tests'));
  69. }
  70. /**
  71. * Check that a null instance is returned from engine() when caching is disabled.
  72. *
  73. * @return void
  74. */
  75. public function testNullEngineWhenCacheDisable() {
  76. $this->_configCache();
  77. Cache::disable();
  78. $result = Cache::engine('tests');
  79. $this->assertInstanceOf('Cake\Cache\Engine\NullEngine', $result);
  80. }
  81. /**
  82. * test configuring CacheEngines in App/libs
  83. *
  84. * @return void
  85. */
  86. public function testConfigWithLibAndPluginEngines() {
  87. Configure::write('App.namespace', 'TestApp');
  88. Plugin::load('TestPlugin');
  89. $config = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  90. Cache::config('libEngine', $config);
  91. $engine = Cache::engine('libEngine');
  92. $this->assertInstanceOf('\TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  93. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  94. $result = Cache::config('pluginLibEngine', $config);
  95. $engine = Cache::engine('pluginLibEngine');
  96. $this->assertInstanceOf('\TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  97. Cache::drop('libEngine');
  98. Cache::drop('pluginLibEngine');
  99. Plugin::unload();
  100. }
  101. /**
  102. * Test write from a config that is undefined.
  103. *
  104. * @expectedException InvalidArgumentException
  105. * @return void
  106. */
  107. public function testWriteNonExistingConfig() {
  108. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  109. }
  110. /**
  111. * Test write from a config that is undefined.
  112. *
  113. * @expectedException InvalidArgumentException
  114. * @return void
  115. */
  116. public function testIncrementNonExistingConfig() {
  117. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  118. }
  119. /**
  120. * Test write from a config that is undefined.
  121. *
  122. * @expectedException InvalidArgumentException
  123. * @return void
  124. */
  125. public function testDecrementNonExistingConfig() {
  126. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  127. }
  128. /**
  129. * Data provider for valid config data sets.
  130. *
  131. * @return array
  132. */
  133. public static function configProvider() {
  134. return [
  135. 'Array of data using engine key.' => [[
  136. 'engine' => 'File',
  137. 'path' => TMP . 'tests',
  138. 'prefix' => 'cake_test_'
  139. ]],
  140. 'Array of data using classname key.' => [[
  141. 'className' => 'File',
  142. 'path' => TMP . 'tests',
  143. 'prefix' => 'cake_test_'
  144. ]],
  145. 'Direct instance' => [new FileEngine()],
  146. ];
  147. }
  148. /**
  149. * testConfig method
  150. *
  151. * @dataProvider configProvider
  152. * @return void
  153. */
  154. public function testConfigVariants($config) {
  155. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  156. Cache::config('tests', $config);
  157. $engine = Cache::engine('tests');
  158. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  159. $this->assertContains('tests', Cache::configured());
  160. }
  161. /**
  162. * testConfigInvalidEngine method
  163. *
  164. * @expectedException BadMethodCallException
  165. * @return void
  166. */
  167. public function testConfigInvalidEngine() {
  168. $config = array('engine' => 'Imaginary');
  169. Cache::config('test', $config);
  170. Cache::engine('test');
  171. }
  172. /**
  173. * test that trying to configure classes that don't extend CacheEngine fail.
  174. *
  175. * @expectedException BadMethodCallException
  176. * @return void
  177. */
  178. public function testConfigInvalidObject() {
  179. $this->getMock('\StdClass', array(), array(), 'RubbishEngine');
  180. Cache::config('test', array(
  181. 'engine' => '\RubbishEngine'
  182. ));
  183. Cache::engine('tests');
  184. }
  185. /**
  186. * Ensure you cannot reconfigure a cache adapter.
  187. *
  188. * @expectedException BadMethodCallException
  189. * @return void
  190. */
  191. public function testConfigErrorOnReconfigure() {
  192. Cache::config('tests', ['engine' => 'File', 'path' => TMP]);
  193. Cache::config('tests', ['engine' => 'Apc']);
  194. }
  195. /**
  196. * Test reading configuration.
  197. *
  198. * @return void
  199. */
  200. public function testConfigRead() {
  201. $config = [
  202. 'engine' => 'File',
  203. 'path' => TMP,
  204. 'prefix' => 'cake_'
  205. ];
  206. Cache::config('tests', $config);
  207. $expected = $config;
  208. $expected['className'] = $config['engine'];
  209. unset($expected['engine']);
  210. $this->assertEquals($expected, Cache::config('tests'));
  211. }
  212. /**
  213. * testGroupConfigs method
  214. */
  215. public function testGroupConfigs() {
  216. Cache::config('latest', [
  217. 'duration' => 300,
  218. 'engine' => 'File',
  219. 'groups' => ['posts', 'comments'],
  220. ]);
  221. $expected = [
  222. 'posts' => ['latest'],
  223. 'comments' => ['latest'],
  224. ];
  225. $engine = Cache::engine('latest');
  226. $result = Cache::groupConfigs();
  227. $this->assertEquals($expected, $result);
  228. $result = Cache::groupConfigs('posts');
  229. $this->assertEquals(['posts' => ['latest']], $result);
  230. Cache::config('page', [
  231. 'duration' => 86400,
  232. 'engine' => 'File',
  233. 'groups' => ['posts', 'archive'],
  234. ]);
  235. $engine = Cache::engine('page');
  236. $result = Cache::groupConfigs();
  237. $expected = [
  238. 'posts' => ['latest', 'page'],
  239. 'comments' => ['latest'],
  240. 'archive' => ['page']
  241. ];
  242. $this->assertEquals($expected, $result);
  243. $result = Cache::groupConfigs('archive');
  244. $this->assertEquals(['archive' => ['page']], $result);
  245. Cache::config('archive', [
  246. 'duration' => 86400 * 30,
  247. 'engine' => 'File',
  248. 'groups' => ['posts', 'archive', 'comments'],
  249. ]);
  250. $engine = Cache::engine('archive');
  251. $result = Cache::groupConfigs('archive');
  252. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  253. }
  254. /**
  255. * testGroupConfigsThrowsException method
  256. * @expectedException InvalidArgumentException
  257. */
  258. public function testGroupConfigsThrowsException() {
  259. Cache::groupConfigs('bogus');
  260. }
  261. /**
  262. * test that configured returns an array of the currently configured cache
  263. * config
  264. *
  265. * @return void
  266. */
  267. public function testConfigured() {
  268. Cache::drop('default');
  269. $result = Cache::configured();
  270. $this->assertContains('_cake_core_', $result);
  271. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  272. }
  273. /**
  274. * test that drop removes cache configs, and that further attempts to use that config
  275. * do not work.
  276. *
  277. * @return void
  278. */
  279. public function testDrop() {
  280. Configure::write('App.namespace', 'TestApp');
  281. $result = Cache::drop('some_config_that_does_not_exist');
  282. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  283. Cache::config('unconfigTest', [
  284. 'engine' => 'TestAppCache'
  285. ]);
  286. $this->assertInstanceOf(
  287. 'TestApp\Cache\Engine\TestAppCacheEngine',
  288. Cache::engine('unconfigTest')
  289. );
  290. $this->assertTrue(Cache::drop('unconfigTest'));
  291. }
  292. /**
  293. * testWriteEmptyValues method
  294. *
  295. * @return void
  296. */
  297. public function testWriteEmptyValues() {
  298. $this->_configCache();
  299. Cache::write('App.falseTest', false, 'tests');
  300. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  301. Cache::write('App.trueTest', true, 'tests');
  302. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  303. Cache::write('App.nullTest', null, 'tests');
  304. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  305. Cache::write('App.zeroTest', 0, 'tests');
  306. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  307. Cache::write('App.zeroTest2', '0', 'tests');
  308. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  309. }
  310. /**
  311. * testWriteEmptyValues method
  312. *
  313. * @expectedException InvalidArgumentException
  314. * @expectedExceptionMessage An empty value is not valid as a cache key
  315. * @return void
  316. */
  317. public function testWriteEmptyKey() {
  318. $this->_configCache();
  319. Cache::write(null, 'not null', 'tests');
  320. }
  321. /**
  322. * testReadWriteMany method
  323. *
  324. * @return void
  325. */
  326. public function testReadWriteMany() {
  327. $this->_configCache();
  328. $data = array(
  329. 'App.falseTest' => false,
  330. 'App.trueTest' => true,
  331. 'App.nullTest' => null,
  332. 'App.zeroTest' => 0,
  333. 'App.zeroTest2' => '0'
  334. );
  335. Cache::writeMany($data, 'tests');
  336. $read = Cache::readMany(array_keys($data), 'tests');
  337. $this->assertSame($read['App.falseTest'], false);
  338. $this->assertSame($read['App.trueTest'], true);
  339. $this->assertSame($read['App.nullTest'], null);
  340. $this->assertSame($read['App.zeroTest'], 0);
  341. $this->assertSame($read['App.zeroTest2'], '0');
  342. }
  343. /**
  344. * testDeleteMany method
  345. *
  346. * @return void
  347. */
  348. public function testDeleteMany() {
  349. $this->_configCache();
  350. $data = array(
  351. 'App.falseTest' => false,
  352. 'App.trueTest' => true,
  353. 'App.nullTest' => null,
  354. 'App.zeroTest' => 0,
  355. 'App.zeroTest2' => '0'
  356. );
  357. Cache::writeMany(array_merge($data, array('App.keepTest' => 'keepMe')), 'tests');
  358. Cache::deleteMany(array_keys($data), 'tests');
  359. $read = Cache::readMany(array_merge(array_keys($data), array('App.keepTest')), 'tests');
  360. $this->assertSame($read['App.falseTest'], false);
  361. $this->assertSame($read['App.trueTest'], false);
  362. $this->assertSame($read['App.nullTest'], false);
  363. $this->assertSame($read['App.zeroTest'], false);
  364. $this->assertSame($read['App.zeroTest2'], false);
  365. $this->assertSame($read['App.keepTest'], 'keepMe');
  366. }
  367. /**
  368. * Test that failed writes cause errors to be triggered.
  369. *
  370. * @expectedException PHPUnit_Framework_Error
  371. * @return void
  372. */
  373. public function testWriteTriggerError() {
  374. Configure::write('App.namespace', 'TestApp');
  375. Cache::config('test_trigger', [
  376. 'engine' => 'TestAppCache',
  377. 'prefix' => ''
  378. ]);
  379. Cache::write('fail', 'value', 'test_trigger');
  380. }
  381. /**
  382. * testCacheDisable method
  383. *
  384. * Check that the "Cache.disable" configuration and a change to it
  385. * (even after a cache config has been setup) is taken into account.
  386. *
  387. * @return void
  388. */
  389. public function testCacheDisable() {
  390. Cache::enable();
  391. Cache::config('test_cache_disable_1', [
  392. 'engine' => 'File',
  393. 'path' => TMP . 'tests'
  394. ]);
  395. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  396. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  397. Cache::disable();
  398. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  399. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  400. Cache::enable();
  401. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  402. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  403. Cache::clear(false, 'test_cache_disable_1');
  404. Cache::disable();
  405. Cache::config('test_cache_disable_2', [
  406. 'engine' => 'File',
  407. 'path' => TMP . 'tests'
  408. ]);
  409. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  410. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  411. Cache::enable();
  412. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  413. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  414. Cache::disable();
  415. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  416. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  417. Cache::enable();
  418. Cache::clear(false, 'test_cache_disable_2');
  419. }
  420. /**
  421. * Test toggling enabled state of cache.
  422. *
  423. * @return void
  424. */
  425. public function testEnableDisableEnabled() {
  426. $this->assertNull(Cache::enable());
  427. $this->assertTrue(Cache::enabled(), 'Should be on');
  428. $this->assertNull(Cache::disable());
  429. $this->assertFalse(Cache::enabled(), 'Should be off');
  430. }
  431. /**
  432. * test remember method.
  433. *
  434. * @return void
  435. */
  436. public function testRemember() {
  437. $this->_configCache();
  438. $counter = 0;
  439. $cacher = function () use ($counter) {
  440. return 'This is some data ' . $counter;
  441. };
  442. $expected = 'This is some data 0';
  443. $result = Cache::remember('test_key', $cacher, 'tests');
  444. $this->assertEquals($expected, $result);
  445. $counter = 1;
  446. $result = Cache::remember('test_key', $cacher, 'tests');
  447. $this->assertEquals($expected, $result);
  448. }
  449. }