CoreTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Cache
  24. */
  25. require_once 'Zend/Cache.php';
  26. require_once 'Zend/Cache/Core.php';
  27. require_once 'Zend/Cache/Backend/File.php'; // TODO : use only Test backend ?
  28. require_once 'Zend/Cache/Backend/Test.php';
  29. require_once 'Zend/Config.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Cache
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Cache
  37. */
  38. class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
  39. {
  40. private $_instance;
  41. public function setUp()
  42. {
  43. if (!$this->_instance) {
  44. $this->_instance = new Zend_Cache_Core(array());
  45. $this->_backend = new Zend_Cache_Backend_Test();
  46. $this->_instance->setBackend($this->_backend);
  47. }
  48. }
  49. public function tearDown()
  50. {
  51. unset($this->_instance);
  52. }
  53. public function testConstructorCorrectCall()
  54. {
  55. $test = new Zend_Cache_Core(array('lifetime' => 3600, 'caching' => true));
  56. }
  57. /**
  58. * @group ZF-7568
  59. */
  60. public function testConstructorCorrectCallWithZendConfig()
  61. {
  62. $test = new Zend_Cache_Core(
  63. new Zend_Config(array('lifetime' => 3600, 'caching' => true))
  64. );
  65. }
  66. /**
  67. * @group ZF-7568
  68. */
  69. public function testSettingOptionsWithZendConfig()
  70. {
  71. $config = new Zend_Config(array('lifetime' => 3600, 'caching' => true));
  72. $test = new Zend_Cache_Core();
  73. $test->setConfig($config);
  74. $this->assertEquals(3600, $test->getOption('lifetime'));
  75. }
  76. /**
  77. * @group ZF-9092
  78. */
  79. public function testSettingLifetimeAsEmptyIsInterpretedAsNull()
  80. {
  81. $config = new Zend_Config(array('lifetime' => '', 'caching' => true));
  82. $test = new Zend_Cache_Core();
  83. $test->setConfig($config);
  84. $this->assertSame(NULL, $test->getOption('lifetime'));
  85. }
  86. public function testConstructorBadOption()
  87. {
  88. try {
  89. $test = new Zend_Cache_Core(array(0 => 'bar', 'lifetime' => 3600));
  90. } catch (Zend_Cache_Exception $e) {
  91. return;
  92. }
  93. $this->fail('Zend_Cache_Exception was expected but not thrown');
  94. }
  95. public function testSetLifeTime()
  96. {
  97. $this->_instance->setLifeTime(3600);
  98. }
  99. public function testSetBackendCorrectCall1()
  100. {
  101. $backend = new Zend_Cache_Backend_File(array());
  102. $this->_instance->setBackend($backend);
  103. }
  104. public function testSetBackendCorrectCall2()
  105. {
  106. $backend = new Zend_Cache_Backend_Test(array());
  107. $this->_instance->setBackend($backend);
  108. $log = $backend->getLastLog();
  109. $this->assertEquals('setDirectives', $log['methodName']);
  110. $this->assertType('array', $log['args'][0]);
  111. }
  112. public function testSetOptionCorrectCall()
  113. {
  114. $this->_instance->setOption('caching', false);
  115. }
  116. public function testSetOptionBadCall()
  117. {
  118. try {
  119. $this->_instance->setOption(array('lifetime'), 1200);
  120. } catch (Zend_Cache_Exception $e) {
  121. return;
  122. }
  123. $this->fail('Zend_Cache_Exception was expected but not thrown');
  124. }
  125. /**
  126. * Unknown options are okay and should be silently ignored. Non-string
  127. * options, however, should throw exceptions.
  128. *
  129. * @group ZF-5034
  130. */
  131. public function testSetOptionUnknownOption()
  132. {
  133. try {
  134. $this->_instance->setOption(0, 1200);
  135. $this->fail('Zend_Cache_Exception was expected but not thrown');
  136. } catch (Zend_Cache_Exception $e) {
  137. }
  138. try {
  139. $this->_instance->setOption('foo', 1200);
  140. } catch (Zend_Cache_Exception $e) {
  141. $this->fail('Zend_Cache_Exception was thrown but should not have been');
  142. }
  143. }
  144. public function testSaveCorrectBadCall1()
  145. {
  146. try {
  147. $this->_instance->save('data', 'foo bar');
  148. } catch (Zend_Cache_Exception $e) {
  149. return;
  150. }
  151. $this->fail('Zend_Cache_Exception was expected but not thrown');
  152. }
  153. public function testSaveCorrectBadCall2()
  154. {
  155. try {
  156. $this->_instance->save('data', 'foobar', array('tag1', 'foo bar'));
  157. } catch (Zend_Cache_Exception $e) {
  158. return;
  159. }
  160. $this->fail('Zend_Cache_Exception was expected but not thrown');
  161. }
  162. public function testSaveCorrectBadCall3()
  163. {
  164. try {
  165. $this->_instance->save(array('data'), 'foobar');
  166. } catch (Zend_Cache_Exception $e) {
  167. return;
  168. }
  169. $this->fail('Zend_Cache_Exception was expected but not thrown');
  170. }
  171. public function testSaveWithABadCacheId()
  172. {
  173. try {
  174. $this->_instance->save(array('data'), true);
  175. } catch (Zend_Cache_Exception $e) {
  176. return;
  177. }
  178. $this->fail('Zend_Cache_Exception was expected but not thrown');
  179. }
  180. public function testSaveWithABadCacheId2()
  181. {
  182. try {
  183. $this->_instance->save(array('data'), 'internal_foo');
  184. } catch (Zend_Cache_Exception $e) {
  185. return;
  186. }
  187. $this->fail('Zend_Cache_Exception was expected but not thrown');
  188. }
  189. public function testSaveWithABadTags()
  190. {
  191. try {
  192. $this->_instance->save(array('data'), 'foo', 'foobar');
  193. } catch (Zend_Cache_Exception $e) {
  194. return;
  195. }
  196. $this->fail('Zend_Cache_Exception was expected but not thrown');
  197. }
  198. public function testSaveCorrectCallNoCaching()
  199. {
  200. $i1 = $this->_backend->getLogIndex();
  201. $this->_instance->setOption('caching', false);
  202. $res = $this->_instance->save('data', 'foo');
  203. $i2 = $this->_backend->getLogIndex();
  204. $this->assertTrue($res);
  205. $this->assertEquals($i1, $i2);
  206. }
  207. public function testSaveCorrectCallNoWriteControl()
  208. {
  209. $this->_instance->setOption('write_control', false);
  210. $res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
  211. $log = $this->_backend->getLastLog();
  212. $expected = array(
  213. 'methodName' => 'save',
  214. 'args' => array(
  215. 0 => 'data',
  216. 1 => 'foo',
  217. 2 => array(
  218. 0 => 'tag1',
  219. 1 => 'tag2'
  220. )
  221. )
  222. );
  223. $this->assertEquals($expected, $log);
  224. }
  225. public function testSaveCorrectCall()
  226. {
  227. $res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
  228. $logs = $this->_backend->getAllLogs();
  229. $expected1 = array(
  230. 'methodName' => 'save',
  231. 'args' => array(
  232. 0 => 'data',
  233. 1 => 'foo',
  234. 2 => array(
  235. 0 => 'tag1',
  236. 1 => 'tag2'
  237. )
  238. )
  239. );
  240. $expected2 = array(
  241. 'methodName' => 'get',
  242. 'args' => array(
  243. 0 => 'foo',
  244. 1 => true
  245. )
  246. );
  247. $expected3 = array(
  248. 'methodName' => 'remove',
  249. 'args' => array(
  250. 0 => 'foo'
  251. )
  252. );
  253. $this->assertFalse($res);
  254. $this->assertEquals($expected1, $logs[count($logs) - 3]);
  255. $this->assertEquals($expected2, $logs[count($logs) - 2]);
  256. $this->assertEquals($expected3, $logs[count($logs) - 1]);
  257. }
  258. public function testSaveCorrectCallButFileCorruption()
  259. {
  260. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  261. $logs = $this->_backend->getAllLogs();
  262. $expected1 = array(
  263. 'methodName' => 'save',
  264. 'args' => array(
  265. 0 => 'data',
  266. 1 => 'false',
  267. 2 => array(
  268. 0 => 'tag1',
  269. 1 => 'tag2'
  270. )
  271. )
  272. );
  273. $expected2 = array(
  274. 'methodName' => 'remove',
  275. 'args' => array(
  276. 0 => 'false'
  277. )
  278. );
  279. $this->assertFalse($res);
  280. $this->assertEquals($expected1, $logs[count($logs) - 2]);
  281. $this->assertEquals($expected2, $logs[count($logs) - 1]);
  282. }
  283. public function testSaveCorrectCallWithAutomaticCleaning()
  284. {
  285. $this->_instance->setOption('automatic_cleaning_factor', 1);
  286. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  287. $logs = $this->_backend->getAllLogs();
  288. $expected = array(
  289. 'methodName' => 'clean',
  290. 'args' => array(
  291. 0 => 'old',
  292. 1 => array()
  293. )
  294. );
  295. $this->assertFalse($res);
  296. $this->assertEquals($expected, $logs[count($logs) - 3]);
  297. }
  298. public function testTestCorrectCallNoCaching()
  299. {
  300. $i1 = $this->_backend->getLogIndex();
  301. $this->_instance->setOption('caching', false);
  302. $res = $this->_instance->test('foo');
  303. $i2 = $this->_backend->getLogIndex();
  304. $this->assertFalse($res);
  305. $this->assertEquals($i1, $i2);
  306. }
  307. public function testTestBadCall()
  308. {
  309. try {
  310. $this->_instance->test('foo bar');
  311. } catch (Zend_Cache_Exception $e) {
  312. return;
  313. }
  314. $this->fail('Zend_Cache_Exception was expected but not thrown');
  315. }
  316. public function testTestCorrectCall1()
  317. {
  318. $res = $this->_instance->test('foo');
  319. $log = $this->_backend->getLastLog();
  320. $expected = array(
  321. 'methodName' => 'test',
  322. 'args' => array(
  323. 0 => 'foo'
  324. )
  325. );
  326. $this->assertEquals(123456, $res);
  327. $this->assertEquals($expected, $log);
  328. }
  329. public function testTestCorrectCall2()
  330. {
  331. $res = $this->_instance->test('false');
  332. $this->assertFalse($res);
  333. }
  334. public function testGetCorrectCallNoCaching()
  335. {
  336. $i1 = $this->_backend->getLogIndex();
  337. $this->_instance->setOption('caching', false);
  338. $res = $this->_instance->load('foo');
  339. $i2 = $this->_backend->getLogIndex();
  340. $this->assertFalse($res);
  341. $this->assertEquals($i1, $i2);
  342. }
  343. public function testGetBadCall()
  344. {
  345. try {
  346. $res = $this->_instance->load('foo bar');
  347. } catch (Zend_Cache_Exception $e) {
  348. return;
  349. }
  350. $this->fail('Zend_Cache_Exception was expected but not thrown');
  351. }
  352. public function testGetCorrectCall1()
  353. {
  354. $res = $this->_instance->load('false');
  355. $this->assertFalse($res);
  356. }
  357. public function testGetCorrectCall2()
  358. {
  359. $res = $this->_instance->load('bar');
  360. $this->assertEquals('foo', 'foo');
  361. }
  362. public function testGetCorrectCallWithAutomaticSerialization()
  363. {
  364. $this->_instance->setOption('automatic_serialization', true);
  365. $res = $this->_instance->load('serialized');
  366. $this->assertEquals(array('foo'), $res);
  367. }
  368. public function testRemoveBadCall()
  369. {
  370. try {
  371. $res = $this->_instance->remove('foo bar');
  372. } catch (Zend_Cache_Exception $e) {
  373. return;
  374. }
  375. $this->fail('Zend_Cache_Exception was expected but not thrown');
  376. }
  377. public function testRemoveCorrectCallNoCaching()
  378. {
  379. $i1 = $this->_backend->getLogIndex();
  380. $this->_instance->setOption('caching', false);
  381. $res = $this->_instance->remove('foo');
  382. $i2 = $this->_backend->getLogIndex();
  383. $this->assertTrue($res);
  384. $this->assertEquals($i1, $i2);
  385. }
  386. public function testRemoveCorrectCall()
  387. {
  388. $res = $this->_instance->remove('foo');
  389. $log = $this->_backend->getLastLog();
  390. $expected = array(
  391. 'methodName' => 'remove',
  392. 'args' => array(
  393. 0 => 'foo'
  394. )
  395. );
  396. $this->assertTrue($res);
  397. $this->assertEquals($expected, $log);
  398. }
  399. public function testCleanBadCall1()
  400. {
  401. try {
  402. $res = $this->_instance->clean('matchingTag', array('foo bar', 'foo'));
  403. } catch (Zend_Cache_Exception $e) {
  404. return;
  405. }
  406. $this->fail('Zend_Cache_Exception was expected but not thrown');
  407. }
  408. public function testCleanBadCall2()
  409. {
  410. try {
  411. $res = $this->_instance->clean('foo');
  412. } catch (Zend_Cache_Exception $e) {
  413. return;
  414. }
  415. $this->fail('Zend_Cache_Exception was expected but not thrown');
  416. }
  417. public function testCleanCorrectCallNoCaching()
  418. {
  419. $i1 = $this->_backend->getLogIndex();
  420. $this->_instance->setOption('caching', false);
  421. $res = $this->_instance->clean('all');
  422. $i2 = $this->_backend->getLogIndex();
  423. $this->assertTrue($res);
  424. $this->assertEquals($i1, $i2);
  425. }
  426. public function testCleanCorrectCall()
  427. {
  428. $res = $this->_instance->clean('matchingTag', array('tag1', 'tag2'));
  429. $log = $this->_backend->getLastLog();
  430. $expected = array(
  431. 'methodName' => 'clean',
  432. 'args' => array(
  433. 0 => 'matchingTag',
  434. 1 => array(
  435. 0 => 'tag1',
  436. 1 => 'tag2'
  437. )
  438. )
  439. );
  440. $this->assertTrue($res);
  441. $this->assertEquals($expected, $log);
  442. }
  443. public function testGetIds()
  444. {
  445. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  446. $ids = $this->_instance->getIds();
  447. $this->assertContains('id1', $ids);
  448. $this->assertContains('id2', $ids);
  449. }
  450. public function testGetIdsMatchingTags()
  451. {
  452. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  453. $ids = $this->_instance->getIdsMatchingTags(array('tag1', 'tag2'));
  454. $this->assertContains('id1', $ids);
  455. $this->assertContains('id2', $ids);
  456. }
  457. public function testGetIdsNotMatchingTags()
  458. {
  459. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  460. $ids = $this->_instance->getIdsNotMatchingTags(array('tag3', 'tag4'));
  461. $this->assertContains('id3', $ids);
  462. $this->assertContains('id4', $ids);
  463. }
  464. public function testGetIdsMatchingAnyTags()
  465. {
  466. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  467. $ids = $this->_instance->getIdsMatchingAnyTags(array('tag5', 'tag6'));
  468. $this->assertContains('id5', $ids);
  469. $this->assertContains('id6', $ids);
  470. }
  471. /**
  472. * @group ZF-10189
  473. */
  474. public function testIfFileZendLogWasIncluded()
  475. {
  476. if (class_exists('Zend_Log', false)) {
  477. $this->markTestSkipped('File Zend/Log.php already included');
  478. }
  479. $cacheCore = new Zend_Cache_Core(
  480. array('logging' => true)
  481. );
  482. $this->assertTrue($cacheCore->getOption('logger') instanceof Zend_Log);
  483. }
  484. }