CoreTest.php 14 KB

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