CacheTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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::drop('test');
  234. Cache::config('latest', [
  235. 'duration' => 300,
  236. 'engine' => 'File',
  237. 'groups' => ['posts', 'comments'],
  238. ]);
  239. $expected = [
  240. 'posts' => ['latest'],
  241. 'comments' => ['latest'],
  242. ];
  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. $result = Cache::groupConfigs();
  253. $expected = [
  254. 'posts' => ['latest', 'page'],
  255. 'comments' => ['latest'],
  256. 'archive' => ['page']
  257. ];
  258. $this->assertEquals($expected, $result);
  259. $result = Cache::groupConfigs('archive');
  260. $this->assertEquals(['archive' => ['page']], $result);
  261. Cache::config('archive', [
  262. 'duration' => 86400 * 30,
  263. 'engine' => 'File',
  264. 'groups' => ['posts', 'archive', 'comments'],
  265. ]);
  266. $result = Cache::groupConfigs('archive');
  267. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  268. }
  269. /**
  270. * testGroupConfigsThrowsException method
  271. * @expectedException \InvalidArgumentException
  272. */
  273. public function testGroupConfigsThrowsException()
  274. {
  275. Cache::groupConfigs('bogus');
  276. }
  277. /**
  278. * test that configured returns an array of the currently configured cache
  279. * config
  280. *
  281. * @return void
  282. */
  283. public function testConfigured()
  284. {
  285. Cache::drop('default');
  286. $result = Cache::configured();
  287. $this->assertContains('_cake_core_', $result);
  288. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  289. }
  290. /**
  291. * test that drop removes cache configs, and that further attempts to use that config
  292. * do not work.
  293. *
  294. * @return void
  295. */
  296. public function testDrop()
  297. {
  298. Configure::write('App.namespace', 'TestApp');
  299. $result = Cache::drop('some_config_that_does_not_exist');
  300. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  301. Cache::config('unconfigTest', [
  302. 'engine' => 'TestAppCache'
  303. ]);
  304. $this->assertInstanceOf(
  305. 'TestApp\Cache\Engine\TestAppCacheEngine',
  306. Cache::engine('unconfigTest')
  307. );
  308. $this->assertTrue(Cache::drop('unconfigTest'));
  309. }
  310. /**
  311. * testWriteEmptyValues method
  312. *
  313. * @return void
  314. */
  315. public function testWriteEmptyValues()
  316. {
  317. $this->_configCache();
  318. Cache::write('App.falseTest', false, 'tests');
  319. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  320. Cache::write('App.trueTest', true, 'tests');
  321. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  322. Cache::write('App.nullTest', null, 'tests');
  323. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  324. Cache::write('App.zeroTest', 0, 'tests');
  325. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  326. Cache::write('App.zeroTest2', '0', 'tests');
  327. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  328. }
  329. /**
  330. * testWriteEmptyValues method
  331. *
  332. * @expectedException \InvalidArgumentException
  333. * @expectedExceptionMessage An empty value is not valid as a cache key
  334. * @return void
  335. */
  336. public function testWriteEmptyKey()
  337. {
  338. $this->_configCache();
  339. Cache::write(null, 'not null', 'tests');
  340. }
  341. /**
  342. * testReadWriteMany method
  343. *
  344. * @return void
  345. */
  346. public function testReadWriteMany()
  347. {
  348. $this->_configCache();
  349. $data = [
  350. 'App.falseTest' => false,
  351. 'App.trueTest' => true,
  352. 'App.nullTest' => null,
  353. 'App.zeroTest' => 0,
  354. 'App.zeroTest2' => '0'
  355. ];
  356. Cache::writeMany($data, 'tests');
  357. $read = Cache::readMany(array_keys($data), 'tests');
  358. $this->assertSame($read['App.falseTest'], false);
  359. $this->assertSame($read['App.trueTest'], true);
  360. $this->assertSame($read['App.nullTest'], null);
  361. $this->assertSame($read['App.zeroTest'], 0);
  362. $this->assertSame($read['App.zeroTest2'], '0');
  363. }
  364. /**
  365. * testDeleteMany method
  366. *
  367. * @return void
  368. */
  369. public function testDeleteMany()
  370. {
  371. $this->_configCache();
  372. $data = [
  373. 'App.falseTest' => false,
  374. 'App.trueTest' => true,
  375. 'App.nullTest' => null,
  376. 'App.zeroTest' => 0,
  377. 'App.zeroTest2' => '0'
  378. ];
  379. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  380. Cache::deleteMany(array_keys($data), 'tests');
  381. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  382. $this->assertSame($read['App.falseTest'], false);
  383. $this->assertSame($read['App.trueTest'], false);
  384. $this->assertSame($read['App.nullTest'], false);
  385. $this->assertSame($read['App.zeroTest'], false);
  386. $this->assertSame($read['App.zeroTest2'], false);
  387. $this->assertSame($read['App.keepTest'], 'keepMe');
  388. }
  389. /**
  390. * Test that failed writes cause errors to be triggered.
  391. *
  392. * @expectedException \PHPUnit_Framework_Error
  393. * @return void
  394. */
  395. public function testWriteTriggerError()
  396. {
  397. Configure::write('App.namespace', 'TestApp');
  398. Cache::config('test_trigger', [
  399. 'engine' => 'TestAppCache',
  400. 'prefix' => ''
  401. ]);
  402. Cache::write('fail', 'value', 'test_trigger');
  403. }
  404. /**
  405. * testCacheDisable method
  406. *
  407. * Check that the "Cache.disable" configuration and a change to it
  408. * (even after a cache config has been setup) is taken into account.
  409. *
  410. * @return void
  411. */
  412. public function testCacheDisable()
  413. {
  414. Cache::enable();
  415. Cache::config('test_cache_disable_1', [
  416. 'engine' => 'File',
  417. 'path' => TMP . 'tests'
  418. ]);
  419. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  420. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  421. Cache::disable();
  422. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  423. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  424. Cache::enable();
  425. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  426. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  427. Cache::clear(false, 'test_cache_disable_1');
  428. Cache::disable();
  429. Cache::config('test_cache_disable_2', [
  430. 'engine' => 'File',
  431. 'path' => TMP . 'tests'
  432. ]);
  433. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  434. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  435. Cache::enable();
  436. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  437. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  438. Cache::disable();
  439. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  440. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  441. Cache::enable();
  442. Cache::clear(false, 'test_cache_disable_2');
  443. }
  444. /**
  445. * Test toggling enabled state of cache.
  446. *
  447. * @return void
  448. */
  449. public function testEnableDisableEnabled()
  450. {
  451. $this->assertNull(Cache::enable());
  452. $this->assertTrue(Cache::enabled(), 'Should be on');
  453. $this->assertNull(Cache::disable());
  454. $this->assertFalse(Cache::enabled(), 'Should be off');
  455. }
  456. /**
  457. * test remember method.
  458. *
  459. * @return void
  460. */
  461. public function testRemember()
  462. {
  463. $this->_configCache();
  464. $counter = 0;
  465. $cacher = function () use ($counter) {
  466. return 'This is some data ' . $counter;
  467. };
  468. $expected = 'This is some data 0';
  469. $result = Cache::remember('test_key', $cacher, 'tests');
  470. $this->assertEquals($expected, $result);
  471. $counter = 1;
  472. $result = Cache::remember('test_key', $cacher, 'tests');
  473. $this->assertEquals($expected, $result);
  474. }
  475. }