CoreTest.php 13 KB

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