CoreTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. /**
  30. * PHPUnit test case
  31. */
  32. require_once 'PHPUnit/Framework/TestCase.php';
  33. require_once 'Zend/Config.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Cache
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Cache
  41. */
  42. class Zend_Cache_CoreTest extends PHPUnit_Framework_TestCase
  43. {
  44. private $_instance;
  45. public function setUp()
  46. {
  47. if (!$this->_instance) {
  48. $this->_instance = new Zend_Cache_Core(array());
  49. $this->_backend = new Zend_Cache_Backend_Test();
  50. $this->_instance->setBackend($this->_backend);
  51. }
  52. }
  53. public function tearDown()
  54. {
  55. unset($this->_instance);
  56. }
  57. public function testConstructorCorrectCall()
  58. {
  59. $test = new Zend_Cache_Core(array('lifetime' => 3600, 'caching' => true));
  60. }
  61. /**
  62. * @issue ZF-7568
  63. */
  64. public function testConstructorCorrectCallWithZendConfig()
  65. {
  66. $test = new Zend_Cache_Core(
  67. new Zend_Config(array('lifetime' => 3600, 'caching' => true))
  68. );
  69. }
  70. /**
  71. * @issue ZF-7568
  72. */
  73. public function testSettingOptionsWithZendConfig()
  74. {
  75. $config = new Zend_Config(array('lifetime' => 3600, 'caching' => true));
  76. $test = new Zend_Cache_Core();
  77. $test->setConfig($config);
  78. $this->assertEquals(3600, $test->getOption('lifetime'));
  79. }
  80. /**
  81. * @group ZF-9092
  82. */
  83. public function testSettingLifetimeAsEmptyIsInterpretedAsNull()
  84. {
  85. $config = new Zend_Config(array('lifetime' => '', 'caching' => true));
  86. $test = new Zend_Cache_Core();
  87. $test->setConfig($config);
  88. $this->assertSame(NULL, $test->getOption('lifetime'));
  89. }
  90. public function testConstructorBadOption()
  91. {
  92. try {
  93. $test = new Zend_Cache_Core(array(0 => 'bar', 'lifetime' => 3600));
  94. } catch (Zend_Cache_Exception $e) {
  95. return;
  96. }
  97. $this->fail('Zend_Cache_Exception was expected but not thrown');
  98. }
  99. public function testSetLifeTime()
  100. {
  101. $this->_instance->setLifeTime(3600);
  102. }
  103. public function testSetBackendCorrectCall1()
  104. {
  105. $backend = new Zend_Cache_Backend_File(array());
  106. $this->_instance->setBackend($backend);
  107. }
  108. public function testSetBackendCorrectCall2()
  109. {
  110. $backend = new Zend_Cache_Backend_Test(array());
  111. $this->_instance->setBackend($backend);
  112. $log = $backend->getLastLog();
  113. $this->assertEquals('setDirectives', $log['methodName']);
  114. $this->assertType('array', $log['args'][0]);
  115. }
  116. public function testSetOptionCorrectCall()
  117. {
  118. $this->_instance->setOption('caching', false);
  119. }
  120. public function testSetOptionBadCall()
  121. {
  122. try {
  123. $this->_instance->setOption(array('lifetime'), 1200);
  124. } catch (Zend_Cache_Exception $e) {
  125. return;
  126. }
  127. $this->fail('Zend_Cache_Exception was expected but not thrown');
  128. }
  129. /**
  130. * Unknown options are okay and should be silently ignored. Non-string
  131. * options, however, should throw exceptions.
  132. *
  133. * @group ZF-5034
  134. */
  135. public function testSetOptionUnknownOption()
  136. {
  137. try {
  138. $this->_instance->setOption(0, 1200);
  139. $this->fail('Zend_Cache_Exception was expected but not thrown');
  140. } catch (Zend_Cache_Exception $e) {
  141. }
  142. try {
  143. $this->_instance->setOption('foo', 1200);
  144. } catch (Zend_Cache_Exception $e) {
  145. $this->fail('Zend_Cache_Exception was thrown but should not have been');
  146. }
  147. }
  148. public function testSaveCorrectBadCall1()
  149. {
  150. try {
  151. $this->_instance->save('data', 'foo bar');
  152. } catch (Zend_Cache_Exception $e) {
  153. return;
  154. }
  155. $this->fail('Zend_Cache_Exception was expected but not thrown');
  156. }
  157. public function testSaveCorrectBadCall2()
  158. {
  159. try {
  160. $this->_instance->save('data', 'foobar', array('tag1', 'foo bar'));
  161. } catch (Zend_Cache_Exception $e) {
  162. return;
  163. }
  164. $this->fail('Zend_Cache_Exception was expected but not thrown');
  165. }
  166. public function testSaveCorrectBadCall3()
  167. {
  168. try {
  169. $this->_instance->save(array('data'), 'foobar');
  170. } catch (Zend_Cache_Exception $e) {
  171. return;
  172. }
  173. $this->fail('Zend_Cache_Exception was expected but not thrown');
  174. }
  175. public function testSaveWithABadCacheId()
  176. {
  177. try {
  178. $this->_instance->save(array('data'), true);
  179. } catch (Zend_Cache_Exception $e) {
  180. return;
  181. }
  182. $this->fail('Zend_Cache_Exception was expected but not thrown');
  183. }
  184. public function testSaveWithABadCacheId2()
  185. {
  186. try {
  187. $this->_instance->save(array('data'), 'internal_foo');
  188. } catch (Zend_Cache_Exception $e) {
  189. return;
  190. }
  191. $this->fail('Zend_Cache_Exception was expected but not thrown');
  192. }
  193. public function testSaveWithABadTags()
  194. {
  195. try {
  196. $this->_instance->save(array('data'), 'foo', 'foobar');
  197. } catch (Zend_Cache_Exception $e) {
  198. return;
  199. }
  200. $this->fail('Zend_Cache_Exception was expected but not thrown');
  201. }
  202. public function testSaveCorrectCallNoCaching()
  203. {
  204. $i1 = $this->_backend->getLogIndex();
  205. $this->_instance->setOption('caching', false);
  206. $res = $this->_instance->save('data', 'foo');
  207. $i2 = $this->_backend->getLogIndex();
  208. $this->assertTrue($res);
  209. $this->assertEquals($i1, $i2);
  210. }
  211. public function testSaveCorrectCallNoWriteControl()
  212. {
  213. $this->_instance->setOption('write_control', false);
  214. $res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
  215. $log = $this->_backend->getLastLog();
  216. $expected = array(
  217. 'methodName' => 'save',
  218. 'args' => array(
  219. 0 => 'data',
  220. 1 => 'foo',
  221. 2 => array(
  222. 0 => 'tag1',
  223. 1 => 'tag2'
  224. )
  225. )
  226. );
  227. $this->assertEquals($expected, $log);
  228. }
  229. public function testSaveCorrectCall()
  230. {
  231. $res = $this->_instance->save('data', 'foo', array('tag1', 'tag2'));
  232. $logs = $this->_backend->getAllLogs();
  233. $expected1 = array(
  234. 'methodName' => 'save',
  235. 'args' => array(
  236. 0 => 'data',
  237. 1 => 'foo',
  238. 2 => array(
  239. 0 => 'tag1',
  240. 1 => 'tag2'
  241. )
  242. )
  243. );
  244. $expected2 = array(
  245. 'methodName' => 'get',
  246. 'args' => array(
  247. 0 => 'foo',
  248. 1 => true
  249. )
  250. );
  251. $expected3 = array(
  252. 'methodName' => 'remove',
  253. 'args' => array(
  254. 0 => 'foo'
  255. )
  256. );
  257. $this->assertFalse($res);
  258. $this->assertEquals($expected1, $logs[count($logs) - 3]);
  259. $this->assertEquals($expected2, $logs[count($logs) - 2]);
  260. $this->assertEquals($expected3, $logs[count($logs) - 1]);
  261. }
  262. public function testSaveCorrectCallButFileCorruption()
  263. {
  264. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  265. $logs = $this->_backend->getAllLogs();
  266. $expected1 = array(
  267. 'methodName' => 'save',
  268. 'args' => array(
  269. 0 => 'data',
  270. 1 => 'false',
  271. 2 => array(
  272. 0 => 'tag1',
  273. 1 => 'tag2'
  274. )
  275. )
  276. );
  277. $expected2 = array(
  278. 'methodName' => 'remove',
  279. 'args' => array(
  280. 0 => 'false'
  281. )
  282. );
  283. $this->assertFalse($res);
  284. $this->assertEquals($expected1, $logs[count($logs) - 2]);
  285. $this->assertEquals($expected2, $logs[count($logs) - 1]);
  286. }
  287. public function testSaveCorrectCallWithAutomaticCleaning()
  288. {
  289. $this->_instance->setOption('automatic_cleaning_factor', 1);
  290. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  291. $logs = $this->_backend->getAllLogs();
  292. $expected = array(
  293. 'methodName' => 'clean',
  294. 'args' => array(
  295. 0 => 'old',
  296. 1 => array()
  297. )
  298. );
  299. $this->assertFalse($res);
  300. $this->assertEquals($expected, $logs[count($logs) - 3]);
  301. }
  302. public function testTestCorrectCallNoCaching()
  303. {
  304. $i1 = $this->_backend->getLogIndex();
  305. $this->_instance->setOption('caching', false);
  306. $res = $this->_instance->test('foo');
  307. $i2 = $this->_backend->getLogIndex();
  308. $this->assertFalse($res);
  309. $this->assertEquals($i1, $i2);
  310. }
  311. public function testTestBadCall()
  312. {
  313. try {
  314. $this->_instance->test('foo bar');
  315. } catch (Zend_Cache_Exception $e) {
  316. return;
  317. }
  318. $this->fail('Zend_Cache_Exception was expected but not thrown');
  319. }
  320. public function testTestCorrectCall1()
  321. {
  322. $res = $this->_instance->test('foo');
  323. $log = $this->_backend->getLastLog();
  324. $expected = array(
  325. 'methodName' => 'test',
  326. 'args' => array(
  327. 0 => 'foo'
  328. )
  329. );
  330. $this->assertEquals(123456, $res);
  331. $this->assertEquals($expected, $log);
  332. }
  333. public function testTestCorrectCall2()
  334. {
  335. $res = $this->_instance->test('false');
  336. $this->assertFalse($res);
  337. }
  338. public function testGetCorrectCallNoCaching()
  339. {
  340. $i1 = $this->_backend->getLogIndex();
  341. $this->_instance->setOption('caching', false);
  342. $res = $this->_instance->load('foo');
  343. $i2 = $this->_backend->getLogIndex();
  344. $this->assertFalse($res);
  345. $this->assertEquals($i1, $i2);
  346. }
  347. public function testGetBadCall()
  348. {
  349. try {
  350. $res = $this->_instance->load('foo bar');
  351. } catch (Zend_Cache_Exception $e) {
  352. return;
  353. }
  354. $this->fail('Zend_Cache_Exception was expected but not thrown');
  355. }
  356. public function testGetCorrectCall1()
  357. {
  358. $res = $this->_instance->load('false');
  359. $this->assertFalse($res);
  360. }
  361. public function testGetCorrectCall2()
  362. {
  363. $res = $this->_instance->load('bar');
  364. $this->assertEquals('foo', 'foo');
  365. }
  366. public function testGetCorrectCallWithAutomaticSerialization()
  367. {
  368. $this->_instance->setOption('automatic_serialization', true);
  369. $res = $this->_instance->load('serialized');
  370. $this->assertEquals(array('foo'), $res);
  371. }
  372. public function testRemoveBadCall()
  373. {
  374. try {
  375. $res = $this->_instance->remove('foo bar');
  376. } catch (Zend_Cache_Exception $e) {
  377. return;
  378. }
  379. $this->fail('Zend_Cache_Exception was expected but not thrown');
  380. }
  381. public function testRemoveCorrectCallNoCaching()
  382. {
  383. $i1 = $this->_backend->getLogIndex();
  384. $this->_instance->setOption('caching', false);
  385. $res = $this->_instance->remove('foo');
  386. $i2 = $this->_backend->getLogIndex();
  387. $this->assertTrue($res);
  388. $this->assertEquals($i1, $i2);
  389. }
  390. public function testRemoveCorrectCall()
  391. {
  392. $res = $this->_instance->remove('foo');
  393. $log = $this->_backend->getLastLog();
  394. $expected = array(
  395. 'methodName' => 'remove',
  396. 'args' => array(
  397. 0 => 'foo'
  398. )
  399. );
  400. $this->assertTrue($res);
  401. $this->assertEquals($expected, $log);
  402. }
  403. public function testCleanBadCall1()
  404. {
  405. try {
  406. $res = $this->_instance->clean('matchingTag', array('foo bar', 'foo'));
  407. } catch (Zend_Cache_Exception $e) {
  408. return;
  409. }
  410. $this->fail('Zend_Cache_Exception was expected but not thrown');
  411. }
  412. public function testCleanBadCall2()
  413. {
  414. try {
  415. $res = $this->_instance->clean('foo');
  416. } catch (Zend_Cache_Exception $e) {
  417. return;
  418. }
  419. $this->fail('Zend_Cache_Exception was expected but not thrown');
  420. }
  421. public function testCleanCorrectCallNoCaching()
  422. {
  423. $i1 = $this->_backend->getLogIndex();
  424. $this->_instance->setOption('caching', false);
  425. $res = $this->_instance->clean('all');
  426. $i2 = $this->_backend->getLogIndex();
  427. $this->assertTrue($res);
  428. $this->assertEquals($i1, $i2);
  429. }
  430. public function testCleanCorrectCall()
  431. {
  432. $res = $this->_instance->clean('matchingTag', array('tag1', 'tag2'));
  433. $log = $this->_backend->getLastLog();
  434. $expected = array(
  435. 'methodName' => 'clean',
  436. 'args' => array(
  437. 0 => 'matchingTag',
  438. 1 => array(
  439. 0 => 'tag1',
  440. 1 => 'tag2'
  441. )
  442. )
  443. );
  444. $this->assertTrue($res);
  445. $this->assertEquals($expected, $log);
  446. }
  447. }