CacheTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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\CacheRegistry;
  18. use Cake\Cache\Engine\FileEngine;
  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 an invalid class fails
  89. *
  90. * @expectedException \RuntimeException
  91. * @expectedExceptionMessage Cache engines must use Cake\Cache\CacheEngine
  92. * @return void
  93. */
  94. public function testConfigInvalidClassType()
  95. {
  96. Cache::config('tests', [
  97. 'className' => '\StdClass'
  98. ]);
  99. Cache::engine('tests');
  100. }
  101. /**
  102. * Test engine init failing causes an error
  103. *
  104. * @expectedException \RuntimeException
  105. * @expectedExceptionMessage not properly configured
  106. * @return void
  107. */
  108. public function testConfigFailedInit()
  109. {
  110. $mock = $this->getMockForAbstractClass('Cake\Cache\CacheEngine', [], '', true, true, true, ['init']);
  111. $mock->method('init')->will($this->returnValue(false));
  112. Cache::config('tests', [
  113. 'engine' => $mock
  114. ]);
  115. Cache::engine('tests');
  116. }
  117. /**
  118. * test configuring CacheEngines in App/libs
  119. *
  120. * @return void
  121. */
  122. public function testConfigWithLibAndPluginEngines()
  123. {
  124. Configure::write('App.namespace', 'TestApp');
  125. Plugin::load('TestPlugin');
  126. $config = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  127. Cache::config('libEngine', $config);
  128. $engine = Cache::engine('libEngine');
  129. $this->assertInstanceOf('TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  130. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  131. $result = Cache::config('pluginLibEngine', $config);
  132. $engine = Cache::engine('pluginLibEngine');
  133. $this->assertInstanceOf('TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  134. Cache::drop('libEngine');
  135. Cache::drop('pluginLibEngine');
  136. Plugin::unload();
  137. }
  138. /**
  139. * Test write from a config that is undefined.
  140. *
  141. * @expectedException \InvalidArgumentException
  142. * @return void
  143. */
  144. public function testWriteNonExistingConfig()
  145. {
  146. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  147. }
  148. /**
  149. * Test write from a config that is undefined.
  150. *
  151. * @expectedException \InvalidArgumentException
  152. * @return void
  153. */
  154. public function testIncrementNonExistingConfig()
  155. {
  156. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  157. }
  158. /**
  159. * Test write from a config that is undefined.
  160. *
  161. * @expectedException \InvalidArgumentException
  162. * @return void
  163. */
  164. public function testDecrementNonExistingConfig()
  165. {
  166. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  167. }
  168. /**
  169. * Data provider for valid config data sets.
  170. *
  171. * @return array
  172. */
  173. public static function configProvider()
  174. {
  175. return [
  176. 'Array of data using engine key.' => [[
  177. 'engine' => 'File',
  178. 'path' => TMP . 'tests',
  179. 'prefix' => 'cake_test_'
  180. ]],
  181. 'Array of data using classname key.' => [[
  182. 'className' => 'File',
  183. 'path' => TMP . 'tests',
  184. 'prefix' => 'cake_test_'
  185. ]],
  186. 'Direct instance' => [new FileEngine()],
  187. ];
  188. }
  189. /**
  190. * testConfig method
  191. *
  192. * @dataProvider configProvider
  193. * @return void
  194. */
  195. public function testConfigVariants($config)
  196. {
  197. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  198. Cache::config('tests', $config);
  199. $engine = Cache::engine('tests');
  200. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  201. $this->assertContains('tests', Cache::configured());
  202. }
  203. /**
  204. * testConfigInvalidEngine method
  205. *
  206. * @expectedException \BadMethodCallException
  207. * @return void
  208. */
  209. public function testConfigInvalidEngine()
  210. {
  211. $config = ['engine' => 'Imaginary'];
  212. Cache::config('test', $config);
  213. Cache::engine('test');
  214. }
  215. /**
  216. * test that trying to configure classes that don't extend CacheEngine fail.
  217. *
  218. * @expectedException \BadMethodCallException
  219. * @return void
  220. */
  221. public function testConfigInvalidObject()
  222. {
  223. $this->getMockBuilder(\StdClass::class)
  224. ->setMockClassName('RubbishEngine')
  225. ->getMock();
  226. Cache::config('test', [
  227. 'engine' => '\RubbishEngine'
  228. ]);
  229. Cache::engine('tests');
  230. }
  231. /**
  232. * Ensure you cannot reconfigure a cache adapter.
  233. *
  234. * @expectedException \BadMethodCallException
  235. * @return void
  236. */
  237. public function testConfigErrorOnReconfigure()
  238. {
  239. Cache::config('tests', ['engine' => 'File', 'path' => TMP]);
  240. Cache::config('tests', ['engine' => 'Apc']);
  241. }
  242. /**
  243. * Test reading configuration.
  244. *
  245. * @return void
  246. */
  247. public function testConfigRead()
  248. {
  249. $config = [
  250. 'engine' => 'File',
  251. 'path' => TMP,
  252. 'prefix' => 'cake_'
  253. ];
  254. Cache::config('tests', $config);
  255. $expected = $config;
  256. $expected['className'] = $config['engine'];
  257. unset($expected['engine']);
  258. $this->assertEquals($expected, Cache::config('tests'));
  259. }
  260. /**
  261. * test config() with dotted name
  262. *
  263. * @return void
  264. */
  265. public function testConfigDottedAlias()
  266. {
  267. Cache::config('cache.dotted', [
  268. 'className' => 'File',
  269. 'path' => TMP,
  270. 'prefix' => 'cache_value_'
  271. ]);
  272. $engine = Cache::engine('cache.dotted');
  273. $this->assertContains('cache.dotted', Cache::configured());
  274. $this->assertNotContains('dotted', Cache::configured());
  275. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  276. Cache::drop('cache.dotted');
  277. }
  278. /**
  279. * testGroupConfigs method
  280. */
  281. public function testGroupConfigs()
  282. {
  283. Cache::drop('test');
  284. Cache::config('latest', [
  285. 'duration' => 300,
  286. 'engine' => 'File',
  287. 'groups' => ['posts', 'comments'],
  288. ]);
  289. $expected = [
  290. 'posts' => ['latest'],
  291. 'comments' => ['latest'],
  292. ];
  293. $result = Cache::groupConfigs();
  294. $this->assertEquals($expected, $result);
  295. $result = Cache::groupConfigs('posts');
  296. $this->assertEquals(['posts' => ['latest']], $result);
  297. Cache::config('page', [
  298. 'duration' => 86400,
  299. 'engine' => 'File',
  300. 'groups' => ['posts', 'archive'],
  301. ]);
  302. $result = Cache::groupConfigs();
  303. $expected = [
  304. 'posts' => ['latest', 'page'],
  305. 'comments' => ['latest'],
  306. 'archive' => ['page']
  307. ];
  308. $this->assertEquals($expected, $result);
  309. $result = Cache::groupConfigs('archive');
  310. $this->assertEquals(['archive' => ['page']], $result);
  311. Cache::config('archive', [
  312. 'duration' => 86400 * 30,
  313. 'engine' => 'File',
  314. 'groups' => ['posts', 'archive', 'comments'],
  315. ]);
  316. $result = Cache::groupConfigs('archive');
  317. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  318. }
  319. /**
  320. * testGroupConfigsThrowsException method
  321. * @expectedException \InvalidArgumentException
  322. */
  323. public function testGroupConfigsThrowsException()
  324. {
  325. Cache::groupConfigs('bogus');
  326. }
  327. /**
  328. * test that configured returns an array of the currently configured cache
  329. * config
  330. *
  331. * @return void
  332. */
  333. public function testConfigured()
  334. {
  335. Cache::drop('default');
  336. $result = Cache::configured();
  337. $this->assertContains('_cake_core_', $result);
  338. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  339. }
  340. /**
  341. * test that drop removes cache configs, and that further attempts to use that config
  342. * do not work.
  343. *
  344. * @return void
  345. */
  346. public function testDrop()
  347. {
  348. Configure::write('App.namespace', 'TestApp');
  349. $result = Cache::drop('some_config_that_does_not_exist');
  350. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  351. Cache::config('unconfigTest', [
  352. 'engine' => 'TestAppCache'
  353. ]);
  354. $this->assertInstanceOf(
  355. 'TestApp\Cache\Engine\TestAppCacheEngine',
  356. Cache::engine('unconfigTest')
  357. );
  358. $this->assertTrue(Cache::drop('unconfigTest'));
  359. }
  360. /**
  361. * testWriteEmptyValues method
  362. *
  363. * @return void
  364. */
  365. public function testWriteEmptyValues()
  366. {
  367. $this->_configCache();
  368. Cache::write('App.falseTest', false, 'tests');
  369. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  370. Cache::write('App.trueTest', true, 'tests');
  371. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  372. Cache::write('App.nullTest', null, 'tests');
  373. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  374. Cache::write('App.zeroTest', 0, 'tests');
  375. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  376. Cache::write('App.zeroTest2', '0', 'tests');
  377. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  378. }
  379. /**
  380. * testWriteEmptyValues method
  381. *
  382. * @expectedException \InvalidArgumentException
  383. * @expectedExceptionMessage An empty value is not valid as a cache key
  384. * @return void
  385. */
  386. public function testWriteEmptyKey()
  387. {
  388. $this->_configCache();
  389. Cache::write(null, 'not null', 'tests');
  390. }
  391. /**
  392. * testReadWriteMany method
  393. *
  394. * @return void
  395. */
  396. public function testReadWriteMany()
  397. {
  398. $this->_configCache();
  399. $data = [
  400. 'App.falseTest' => false,
  401. 'App.trueTest' => true,
  402. 'App.nullTest' => null,
  403. 'App.zeroTest' => 0,
  404. 'App.zeroTest2' => '0'
  405. ];
  406. Cache::writeMany($data, 'tests');
  407. $read = Cache::readMany(array_keys($data), 'tests');
  408. $this->assertSame($read['App.falseTest'], false);
  409. $this->assertSame($read['App.trueTest'], true);
  410. $this->assertSame($read['App.nullTest'], null);
  411. $this->assertSame($read['App.zeroTest'], 0);
  412. $this->assertSame($read['App.zeroTest2'], '0');
  413. }
  414. /**
  415. * testDeleteMany method
  416. *
  417. * @return void
  418. */
  419. public function testDeleteMany()
  420. {
  421. $this->_configCache();
  422. $data = [
  423. 'App.falseTest' => false,
  424. 'App.trueTest' => true,
  425. 'App.nullTest' => null,
  426. 'App.zeroTest' => 0,
  427. 'App.zeroTest2' => '0'
  428. ];
  429. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  430. Cache::deleteMany(array_keys($data), 'tests');
  431. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  432. $this->assertSame($read['App.falseTest'], false);
  433. $this->assertSame($read['App.trueTest'], false);
  434. $this->assertSame($read['App.nullTest'], false);
  435. $this->assertSame($read['App.zeroTest'], false);
  436. $this->assertSame($read['App.zeroTest2'], false);
  437. $this->assertSame($read['App.keepTest'], 'keepMe');
  438. }
  439. /**
  440. * Test that failed writes cause errors to be triggered.
  441. *
  442. * @expectedException \PHPUnit_Framework_Error
  443. * @return void
  444. */
  445. public function testWriteTriggerError()
  446. {
  447. Configure::write('App.namespace', 'TestApp');
  448. Cache::config('test_trigger', [
  449. 'engine' => 'TestAppCache',
  450. 'prefix' => ''
  451. ]);
  452. Cache::write('fail', 'value', 'test_trigger');
  453. }
  454. /**
  455. * testCacheDisable method
  456. *
  457. * Check that the "Cache.disable" configuration and a change to it
  458. * (even after a cache config has been setup) is taken into account.
  459. *
  460. * @return void
  461. */
  462. public function testCacheDisable()
  463. {
  464. Cache::enable();
  465. Cache::config('test_cache_disable_1', [
  466. 'engine' => 'File',
  467. 'path' => TMP . 'tests'
  468. ]);
  469. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  470. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  471. Cache::disable();
  472. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  473. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  474. Cache::enable();
  475. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  476. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  477. Cache::clear(false, 'test_cache_disable_1');
  478. Cache::disable();
  479. Cache::config('test_cache_disable_2', [
  480. 'engine' => 'File',
  481. 'path' => TMP . 'tests'
  482. ]);
  483. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  484. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  485. Cache::enable();
  486. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  487. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  488. Cache::disable();
  489. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  490. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  491. Cache::enable();
  492. Cache::clear(false, 'test_cache_disable_2');
  493. }
  494. /**
  495. * test clearAll() method
  496. *
  497. * @return void
  498. */
  499. public function testClearAll()
  500. {
  501. Cache::config('configTest', [
  502. 'engine' => 'File',
  503. 'path' => TMP . 'tests'
  504. ]);
  505. Cache::config('anotherConfigTest', [
  506. 'engine' => 'File',
  507. 'path' => TMP . 'tests'
  508. ]);
  509. Cache::write('key_1', 'hello', 'configTest');
  510. Cache::write('key_2', 'hello again', 'anotherConfigTest');
  511. $this->assertSame(Cache::read('key_1', 'configTest'), 'hello');
  512. $this->assertSame(Cache::read('key_2', 'anotherConfigTest'), 'hello again');
  513. $result = Cache::clearAll();
  514. $this->assertTrue($result['configTest']);
  515. $this->assertTrue($result['anotherConfigTest']);
  516. $this->assertFalse(Cache::read('key_1', 'configTest'));
  517. $this->assertFalse(Cache::read('key_2', 'anotherConfigTest'));
  518. Cache::drop('configTest');
  519. Cache::drop('anotherConfigTest');
  520. }
  521. /**
  522. * Test toggling enabled state of cache.
  523. *
  524. * @return void
  525. */
  526. public function testEnableDisableEnabled()
  527. {
  528. $this->assertNull(Cache::enable());
  529. $this->assertTrue(Cache::enabled(), 'Should be on');
  530. $this->assertNull(Cache::disable());
  531. $this->assertFalse(Cache::enabled(), 'Should be off');
  532. }
  533. /**
  534. * test remember method.
  535. *
  536. * @return void
  537. */
  538. public function testRemember()
  539. {
  540. $this->_configCache();
  541. $counter = 0;
  542. $cacher = function () use ($counter) {
  543. return 'This is some data ' . $counter;
  544. };
  545. $expected = 'This is some data 0';
  546. $result = Cache::remember('test_key', $cacher, 'tests');
  547. $this->assertEquals($expected, $result);
  548. $counter = 1;
  549. $result = Cache::remember('test_key', $cacher, 'tests');
  550. $this->assertEquals($expected, $result);
  551. }
  552. /**
  553. * Test add method.
  554. *
  555. * @return void
  556. */
  557. public function testAdd()
  558. {
  559. $this->_configCache();
  560. Cache::delete('test_add_key', 'tests');
  561. $result = Cache::add('test_add_key', 'test data', 'tests');
  562. $this->assertTrue($result);
  563. $expected = 'test data';
  564. $result = Cache::read('test_add_key', 'tests');
  565. $this->assertEquals($expected, $result);
  566. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  567. $this->assertFalse($result);
  568. }
  569. /**
  570. * test registry method
  571. *
  572. * @return void
  573. */
  574. public function testRegistry()
  575. {
  576. $this->assertInstanceOf('Cake\Cache\CacheRegistry', Cache::registry());
  577. }
  578. /**
  579. * test registry method setting
  580. *
  581. * @return void
  582. */
  583. public function testRegistrySet()
  584. {
  585. $registry = new CacheRegistry();
  586. Cache::registry($registry);
  587. $this->assertSame($registry, Cache::registry());
  588. }
  589. }