CacheTest.php 15 KB

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