CoreTest.php 12 KB

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