PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Cache/CoreTest.php

https://bitbucket.org/ksekar/campus
PHP | 547 lines | 446 code | 52 blank | 49 comment | 2 complexity | a495e8f52654450e6ffdca8dfb796952 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: CoreTest.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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. $cacheIdPrefix = 'cacheIdPrefix';
  261. $this->_instance->setOption('cache_id_prefix', $cacheIdPrefix);
  262. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  263. $logs = $this->_backend->getAllLogs();
  264. $expected1 = array(
  265. 'methodName' => 'save',
  266. 'args' => array(
  267. 0 => 'data',
  268. 1 => $cacheIdPrefix . 'false',
  269. 2 => array(
  270. 0 => 'tag1',
  271. 1 => 'tag2'
  272. )
  273. )
  274. );
  275. $expected2 = array(
  276. 'methodName' => 'remove',
  277. 'args' => array(
  278. 0 => $cacheIdPrefix.'false'
  279. )
  280. );
  281. $this->assertFalse($res);
  282. $this->assertEquals($expected1, $logs[count($logs) - 2]);
  283. $this->assertEquals($expected2, $logs[count($logs) - 1]);
  284. }
  285. public function testSaveCorrectCallWithAutomaticCleaning()
  286. {
  287. $this->_instance->setOption('automatic_cleaning_factor', 1);
  288. $res = $this->_instance->save('data', 'false', array('tag1', 'tag2'));
  289. $logs = $this->_backend->getAllLogs();
  290. $expected = array(
  291. 'methodName' => 'clean',
  292. 'args' => array(
  293. 0 => 'old',
  294. 1 => array()
  295. )
  296. );
  297. $this->assertFalse($res);
  298. $this->assertEquals($expected, $logs[count($logs) - 3]);
  299. }
  300. public function testTestCorrectCallNoCaching()
  301. {
  302. $i1 = $this->_backend->getLogIndex();
  303. $this->_instance->setOption('caching', false);
  304. $res = $this->_instance->test('foo');
  305. $i2 = $this->_backend->getLogIndex();
  306. $this->assertFalse($res);
  307. $this->assertEquals($i1, $i2);
  308. }
  309. public function testTestBadCall()
  310. {
  311. try {
  312. $this->_instance->test('foo bar');
  313. } catch (Zend_Cache_Exception $e) {
  314. return;
  315. }
  316. $this->fail('Zend_Cache_Exception was expected but not thrown');
  317. }
  318. public function testTestCorrectCall1()
  319. {
  320. $res = $this->_instance->test('foo');
  321. $log = $this->_backend->getLastLog();
  322. $expected = array(
  323. 'methodName' => 'test',
  324. 'args' => array(
  325. 0 => 'foo'
  326. )
  327. );
  328. $this->assertEquals(123456, $res);
  329. $this->assertEquals($expected, $log);
  330. }
  331. public function testTestCorrectCall2()
  332. {
  333. $res = $this->_instance->test('false');
  334. $this->assertFalse($res);
  335. }
  336. public function testGetCorrectCallNoCaching()
  337. {
  338. $i1 = $this->_backend->getLogIndex();
  339. $this->_instance->setOption('caching', false);
  340. $res = $this->_instance->load('foo');
  341. $i2 = $this->_backend->getLogIndex();
  342. $this->assertFalse($res);
  343. $this->assertEquals($i1, $i2);
  344. }
  345. public function testGetBadCall()
  346. {
  347. try {
  348. $res = $this->_instance->load('foo bar');
  349. } catch (Zend_Cache_Exception $e) {
  350. return;
  351. }
  352. $this->fail('Zend_Cache_Exception was expected but not thrown');
  353. }
  354. public function testGetCorrectCall1()
  355. {
  356. $res = $this->_instance->load('false');
  357. $this->assertFalse($res);
  358. }
  359. public function testGetCorrectCall2()
  360. {
  361. $res = $this->_instance->load('bar');
  362. $this->assertEquals('foo', 'foo');
  363. }
  364. public function testGetCorrectCallWithAutomaticSerialization()
  365. {
  366. $this->_instance->setOption('automatic_serialization', true);
  367. $res = $this->_instance->load('serialized');
  368. $this->assertEquals(array('foo'), $res);
  369. }
  370. public function testRemoveBadCall()
  371. {
  372. try {
  373. $res = $this->_instance->remove('foo bar');
  374. } catch (Zend_Cache_Exception $e) {
  375. return;
  376. }
  377. $this->fail('Zend_Cache_Exception was expected but not thrown');
  378. }
  379. public function testRemoveCorrectCallNoCaching()
  380. {
  381. $i1 = $this->_backend->getLogIndex();
  382. $this->_instance->setOption('caching', false);
  383. $res = $this->_instance->remove('foo');
  384. $i2 = $this->_backend->getLogIndex();
  385. $this->assertTrue($res);
  386. $this->assertEquals($i1, $i2);
  387. }
  388. public function testRemoveCorrectCall()
  389. {
  390. $res = $this->_instance->remove('foo');
  391. $log = $this->_backend->getLastLog();
  392. $expected = array(
  393. 'methodName' => 'remove',
  394. 'args' => array(
  395. 0 => 'foo'
  396. )
  397. );
  398. $this->assertTrue($res);
  399. $this->assertEquals($expected, $log);
  400. }
  401. public function testCleanBadCall1()
  402. {
  403. try {
  404. $res = $this->_instance->clean('matchingTag', array('foo bar', 'foo'));
  405. } catch (Zend_Cache_Exception $e) {
  406. return;
  407. }
  408. $this->fail('Zend_Cache_Exception was expected but not thrown');
  409. }
  410. public function testCleanBadCall2()
  411. {
  412. try {
  413. $res = $this->_instance->clean('foo');
  414. } catch (Zend_Cache_Exception $e) {
  415. return;
  416. }
  417. $this->fail('Zend_Cache_Exception was expected but not thrown');
  418. }
  419. public function testCleanCorrectCallNoCaching()
  420. {
  421. $i1 = $this->_backend->getLogIndex();
  422. $this->_instance->setOption('caching', false);
  423. $res = $this->_instance->clean('all');
  424. $i2 = $this->_backend->getLogIndex();
  425. $this->assertTrue($res);
  426. $this->assertEquals($i1, $i2);
  427. }
  428. public function testCleanCorrectCall()
  429. {
  430. $res = $this->_instance->clean('matchingTag', array('tag1', 'tag2'));
  431. $log = $this->_backend->getLastLog();
  432. $expected = array(
  433. 'methodName' => 'clean',
  434. 'args' => array(
  435. 0 => 'matchingTag',
  436. 1 => array(
  437. 0 => 'tag1',
  438. 1 => 'tag2'
  439. )
  440. )
  441. );
  442. $this->assertTrue($res);
  443. $this->assertEquals($expected, $log);
  444. }
  445. public function testGetIds()
  446. {
  447. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  448. $ids = $this->_instance->getIds();
  449. $this->assertContains('id1', $ids);
  450. $this->assertContains('id2', $ids);
  451. }
  452. public function testGetIdsMatchingTags()
  453. {
  454. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  455. $ids = $this->_instance->getIdsMatchingTags(array('tag1', 'tag2'));
  456. $this->assertContains('id1', $ids);
  457. $this->assertContains('id2', $ids);
  458. }
  459. public function testGetIdsNotMatchingTags()
  460. {
  461. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  462. $ids = $this->_instance->getIdsNotMatchingTags(array('tag3', 'tag4'));
  463. $this->assertContains('id3', $ids);
  464. $this->assertContains('id4', $ids);
  465. }
  466. public function testGetIdsMatchingAnyTags()
  467. {
  468. $this->_instance->setOption('cache_id_prefix', 'prefix_');
  469. $ids = $this->_instance->getIdsMatchingAnyTags(array('tag5', 'tag6'));
  470. $this->assertContains('id5', $ids);
  471. $this->assertContains('id6', $ids);
  472. }
  473. public function testLoggerSanity()
  474. {
  475. $this->_instance = new Zend_Cache_Core(array(
  476. 'logging' => true
  477. ));
  478. $this->_instance->setBackend($this->_backend);
  479. $logger = $this->_instance->getOption('logger');
  480. $this->assertType('Zend_Log', $logger);
  481. }
  482. /**
  483. * @group ZF-10189
  484. */
  485. public function testIfFileZendLogWasIncluded()
  486. {
  487. if (class_exists('Zend_Log', false)) {
  488. $this->markTestSkipped('File Zend/Log.php already included');
  489. }
  490. $cacheCore = new Zend_Cache_Core(
  491. array('logging' => true)
  492. );
  493. $this->assertTrue($cacheCore->getOption('logger') instanceof Zend_Log);
  494. }
  495. }