/tests/unit/suite/libraries/joomla/cache/JCacheTest.php

https://github.com/mathc/joomla-cms · PHP · 632 lines · 437 code · 23 blank · 172 comment · 0 complexity · 6a0d348bdf408359b6b128c007b0f909 MD5 · raw file

  1. <?php
  2. /**
  3. * JCacheTest class -- testing framework for JCache
  4. *
  5. * @version $Id$
  6. * @package Joomla.UnitTest
  7. * @copyright Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
  8. * @license GNU General Public License version 2 or later; see LICENSE.txt
  9. */
  10. /**
  11. * Test class for JCache.
  12. * Generated by PHPUnit on 2009-10-08 at 21:39:47.
  13. *
  14. * @package Joomla.UnitTest
  15. * @subpackage Cache
  16. *
  17. */
  18. class JCacheTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var JCache
  22. * @access protected
  23. */
  24. protected $object;
  25. /**
  26. * Sets up the fixture, for example, opens a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. * @access protected
  31. */
  32. protected function setUp()
  33. {
  34. include_once JPATH_BASE.'/libraries/joomla/cache/cache.php';
  35. include_once JPATH_BASE.'/libraries/joomla/cache/controller.php';
  36. include_once JPATH_BASE.'/libraries/joomla/cache/storage.php';
  37. //$this->object = new JCache;
  38. }
  39. /**
  40. * Tears down the fixture, for example, closes a network connection.
  41. * This method is called after a test is executed.
  42. *
  43. * @return void
  44. * @access protected
  45. */
  46. protected function tearDown()
  47. {
  48. }
  49. /**
  50. * Test Cases for getInstance
  51. *
  52. * @return array
  53. */
  54. function casesGetInstance()
  55. {
  56. return array(
  57. 'simple' => array(
  58. 'output',
  59. array('storage' => 'file'),
  60. 'JCacheControllerOutput',
  61. ),
  62. 'complexOutput' => array(
  63. 'output',
  64. array(
  65. 'defaultgroup' => '',
  66. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  67. 'lifetime' => 15 * 60, // minutes to seconds
  68. 'storage' => 'file',
  69. ),
  70. 'JCacheControllerOutput',
  71. ),
  72. 'complexPage' => array(
  73. 'page',
  74. array(
  75. 'defaultgroup' => '',
  76. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  77. 'lifetime' => 15 * 60, // minutes to seconds
  78. 'storage' => 'file',
  79. ),
  80. 'JCacheControllerPage',
  81. ),
  82. 'complexView' => array(
  83. 'view',
  84. array(
  85. 'defaultgroup' => '',
  86. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  87. 'lifetime' => 15 * 60, // minutes to seconds
  88. 'storage' => 'file',
  89. ),
  90. 'JCacheControllerView',
  91. ),
  92. 'complexCallback' => array(
  93. 'callback',
  94. array(
  95. 'defaultgroup' => '',
  96. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  97. 'lifetime' => 15 * 60, // minutes to seconds
  98. 'storage' => 'file',
  99. ),
  100. 'JCacheControllerCallback',
  101. ),
  102. );
  103. }
  104. /**
  105. * Testing getInstance, set_state, setCaching, and setLifeTime
  106. *
  107. * @param string cache handler
  108. * @param array options for cache handler
  109. * @param string name of expected cache class
  110. *
  111. * @return void
  112. * @dataProvider casesGetInstance
  113. */
  114. public function testGetInstance( $handler, $options, $expClass)
  115. {
  116. $this->object = JCache::getInstance($handler, $options);
  117. $this->assertThat(
  118. $this->object,
  119. $this->isInstanceOf($expClass)
  120. );
  121. //$state = $this->object->__set_state((array)$this->object);
  122. //$this->assertThat(
  123. // $state,
  124. // $this->equalTo($this->object)
  125. //);
  126. }
  127. /**
  128. * Test Cases for setCaching
  129. *
  130. * @return array
  131. */
  132. function casesSetCaching()
  133. {
  134. return array(
  135. 'simple' => array(
  136. 'output',
  137. array('storage' => 'file'),
  138. ),
  139. 'complexOutput' => array(
  140. 'output',
  141. array(
  142. 'defaultgroup' => '',
  143. 'cachebase' => JPATH_BASE . '/unittest/cache',
  144. 'lifetime' => 15 * 60, // minutes to seconds
  145. 'storage' => 'file',
  146. ),
  147. ),
  148. 'complexPage' => array(
  149. 'page',
  150. array(
  151. 'defaultgroup' => '',
  152. 'cachebase' => JPATH_BASE . '/unittest/cache',
  153. 'lifetime' => 15 * 60, // minutes to seconds
  154. 'storage' => 'file',
  155. ),
  156. ),
  157. 'complexView' => array(
  158. 'view',
  159. array(
  160. 'defaultgroup' => '',
  161. 'cachebase' => JPATH_BASE . '/unittest/cache',
  162. 'lifetime' => 15 * 60, // minutes to seconds
  163. 'storage' => 'file',
  164. ),
  165. ),
  166. 'complexCallback' => array(
  167. 'callback',
  168. array(
  169. 'defaultgroup' => '',
  170. 'cachebase' => JPATH_BASE . '/unittest/cache',
  171. 'lifetime' => 15 * 60, // minutes to seconds
  172. 'storage' => 'file',
  173. ),
  174. ),
  175. );
  176. }
  177. /**
  178. * Testing setCaching
  179. *
  180. * @param string cache handler
  181. * @param array options for cache handler
  182. *
  183. * @return void
  184. * @dataProvider casesSetCaching
  185. */
  186. public function testSetCaching( $handler, $options )
  187. {
  188. $this->object = JCache::getInstance($handler, $options);
  189. $caching = (bool)$this->object->options['caching'];
  190. $this->object->setCaching(!$caching);
  191. $this->assertThat(
  192. $this->object->options['caching'],
  193. $this->equalTo(!$caching)
  194. );
  195. }
  196. /**
  197. * Test Cases for setLifetime
  198. *
  199. * @return array
  200. */
  201. function casesSetLifetime()
  202. {
  203. return array(
  204. 'simple' => array(
  205. 'output',
  206. array('storage' => 'file'),
  207. 900,
  208. ),
  209. 'complexOutput' => array(
  210. 'output',
  211. array(
  212. 'defaultgroup' => '',
  213. 'cachebase' => JPATH_BASE . '/unittest/cache',
  214. 'lifetime' => 15 * 60, // minutes to seconds
  215. 'storage' => 'file',
  216. ),
  217. 15*60,
  218. ),
  219. 'complexPage' => array(
  220. 'page',
  221. array(
  222. 'defaultgroup' => '',
  223. 'cachebase' => JPATH_BASE . '/unittest/cache',
  224. 'lifetime' => 15 * 60, // minutes to seconds
  225. 'storage' => 'file',
  226. ),
  227. 15*60,
  228. ),
  229. 'complexView' => array(
  230. 'view',
  231. array(
  232. 'defaultgroup' => '',
  233. 'cachebase' => JPATH_BASE . '/unittest/cache',
  234. 'lifetime' => 15 * 60, // minutes to seconds
  235. 'storage' => 'file',
  236. ),
  237. 15*60,
  238. ),
  239. 'complexCallback' => array(
  240. 'callback',
  241. array(
  242. 'defaultgroup' => '',
  243. 'cachebase' => JPATH_BASE . '/unittest/cache',
  244. 'lifetime' => 15 * 60, // minutes to seconds
  245. 'storage' => 'file',
  246. ),
  247. 15*60,
  248. ),
  249. );
  250. }
  251. /**
  252. * Testing setLifeTime
  253. *
  254. * @param string cache handler
  255. * @param array options for cache handler
  256. * @param integer lifetime of cache to be set
  257. *
  258. * @return void
  259. * @dataProvider casesSetLifetime
  260. */
  261. public function testSetLifeTime( $handler, $options, $lifetime)
  262. {
  263. $this->object = JCache::getInstance($handler, $options);
  264. $this->object->setLifeTime($lifetime);
  265. $this->assertThat(
  266. $this->object->options['lifetime'],
  267. $this->equalTo($lifetime)
  268. );
  269. }
  270. /**
  271. * Test Cases for getStores
  272. *
  273. * @return array
  274. */
  275. function casesGetStores()
  276. {
  277. return array(
  278. 'simple' => array(
  279. 'output',
  280. array('storage' => 'file'),
  281. 'file',
  282. ),
  283. 'complexOutput' => array(
  284. 'output',
  285. array(
  286. 'defaultgroup' => '',
  287. 'cachebase' => JPATH_BASE . '/unittest/cache',
  288. 'lifetime' => 15 * 60, // minutes to seconds
  289. 'storage' => 'file',
  290. ),
  291. 'file' ,
  292. ),
  293. 'complexPage' => array(
  294. 'page',
  295. array(
  296. 'defaultgroup' => '',
  297. 'cachebase' => JPATH_BASE . '/unittest/cache',
  298. 'lifetime' => 15 * 60, // minutes to seconds
  299. 'storage' => 'file',
  300. ),
  301. 'file',
  302. ),
  303. 'complexView' => array(
  304. 'view',
  305. array(
  306. 'defaultgroup' => '',
  307. 'cachebase' => JPATH_BASE . '/unittest/cache',
  308. 'lifetime' => 15 * 60, // minutes to seconds
  309. 'storage' => 'file',
  310. ),
  311. 'file',
  312. ),
  313. 'complexCallback' => array(
  314. 'callback',
  315. array(
  316. 'defaultgroup' => '',
  317. 'cachebase' => JPATH_BASE . '/unittest/cache',
  318. 'lifetime' => 15 * 60, // minutes to seconds
  319. 'storage' => 'file',
  320. ),
  321. 'file',
  322. ),
  323. );
  324. }
  325. /**
  326. * Testing getStores
  327. *
  328. * @param string cache handler
  329. * @param array options for cache handler
  330. * @param string returned stores
  331. *
  332. * @return void
  333. * @dataProvider casesGetStores
  334. */
  335. public function testGetStores( $handler, $options, $expected )
  336. {
  337. $this->object = JCache::getInstance($handler, $options);
  338. $this->assertThat(
  339. $this->object->options['storage'],
  340. $this->equalTo($expected)
  341. );
  342. }
  343. /**
  344. * Test Cases for get() / store()
  345. *
  346. * @return array
  347. */
  348. function casesStore()
  349. {
  350. return array(
  351. 'simple' => array(
  352. 'output',
  353. array( 'lifetime' => 600, 'storage' => 'file'),
  354. 42,
  355. '',
  356. 'And this is the cache that tries men\'s souls',
  357. false,
  358. ),
  359. 'complexOutput' => array(
  360. 'output',
  361. array(
  362. 'defaultgroup' => '',
  363. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  364. 'lifetime' => 15 * 60, // minutes to seconds
  365. 'storage' => 'file',
  366. ),
  367. 42,
  368. '',
  369. 'And this is the cache that tries men\'s souls',
  370. false,
  371. ),
  372. /** This does not work since JCacheControllerPage retrieves the page-body and does not work with a parameter
  373. 'complexPage' => array(
  374. 'page',
  375. array(
  376. 'defaultgroup' => '',
  377. 'cachebase' => JPATH_BASE . '/tests/unit/cache',
  378. 'lifetime' => 20 * 60, // minutes to seconds
  379. 'storage' => 'file',
  380. ),
  381. 42,
  382. '',
  383. 'And this is the cache that tries men\'s souls',
  384. false,
  385. ),**/
  386. );
  387. }
  388. /**
  389. * Testing store() and get()
  390. *
  391. * @param string cache handler
  392. * @param array options for cache handler
  393. * @param string cache element ID
  394. * @param string cache group
  395. * @param string data to be cached
  396. * @param string expected return
  397. *
  398. * @return void
  399. * @dataProvider casesStore
  400. */
  401. public function testStoreAndGet( $handler, $options, $id, $group, $data, $expected )
  402. {
  403. $this->object = JCache::getInstance($handler, $options);
  404. $this->object->setCaching(true);
  405. $this->assertThat(
  406. $this->object->store($data, $id, $group),
  407. $this->isTrue(),
  408. 'Should store the data properly'
  409. );
  410. $this->assertThat(
  411. $this->object->get($id, $group),
  412. $this->equalTo($data),
  413. 'Should retrieve the data properly'
  414. );
  415. }
  416. /**
  417. * Testing remove().
  418. *
  419. * @return void
  420. */
  421. public function testRemove()
  422. {
  423. $options = array('storage' => 'file');
  424. $this->object = JCache::getInstance('output',$options);
  425. $this->object->setCaching(true);
  426. $this->object->store(
  427. 'Now is the time for all good people to throw a party.',
  428. 42,
  429. ''
  430. );
  431. $this->object->store(
  432. 'And this is the cache that tries men\'s souls',
  433. 43,
  434. ''
  435. );
  436. $this->assertThat(
  437. $this->object->get(43, ''),
  438. $this->equalTo('And this is the cache that tries men\'s souls'),
  439. 'Should retrieve the data properly'
  440. );
  441. $this->assertThat(
  442. $this->object->remove(43, ''),
  443. $this->isTrue(),
  444. 'Should remove cached data'
  445. );
  446. $this->assertThat(
  447. $this->object->get(43, ''),
  448. $this->isFalse(),
  449. 'Should not retrieve the data properly'
  450. );
  451. $this->assertThat(
  452. $this->object->get(42, ''),
  453. $this->equalTo('Now is the time for all good people to throw a party.'),
  454. 'Should retrieve the data properly'
  455. );
  456. }
  457. /**
  458. * Testing clean().
  459. *
  460. * @return void
  461. */
  462. public function testClean()
  463. {
  464. $options = array('storage' => 'file');
  465. $this->object = JCache::getInstance('output',$options);
  466. $this->object->setCaching(true);
  467. $this->object->store(
  468. 'Now is the time for all good people to throw a party.',
  469. 42,
  470. ''
  471. );
  472. $this->object->store(
  473. 'And this is the cache that tries men\'s souls',
  474. 43,
  475. ''
  476. );
  477. $this->assertThat(
  478. $this->object->get(43, ''),
  479. $this->equalTo('And this is the cache that tries men\'s souls'),
  480. 'Should retrieve the data properly'
  481. );
  482. $this->assertThat(
  483. $this->object->clean(''),
  484. $this->isTrue(),
  485. 'Should remove cached data'
  486. );
  487. $this->assertThat(
  488. $this->object->get(43, ''),
  489. $this->isFalse(),
  490. 'Should not retrieve the data properly'
  491. );
  492. $this->assertThat(
  493. $this->object->get(42, ''),
  494. $this->isFalse(),
  495. 'Should not retrieve the data properly'
  496. );
  497. }
  498. /**
  499. * Testing Gc().
  500. *
  501. * @return void
  502. */
  503. public function testGc()
  504. {
  505. $this->object = JCache::getInstance('output', array('lifetime' => 2, 'defaultgroup' => '' ));
  506. $this->object->store(
  507. 'Now is the time for all good people to throw a party.',
  508. 42,
  509. ''
  510. );
  511. $this->object->store(
  512. 'And this is the cache that tries men\'s souls',
  513. 42,
  514. ''
  515. );
  516. sleep(5);
  517. $this->object->gc();
  518. $this->assertThat(
  519. $this->object->get(42, ''),
  520. $this->isFalse(),
  521. 'Should not retrieve the data properly'
  522. );
  523. $this->assertThat(
  524. $this->object->get(42, ''),
  525. $this->isFalse(),
  526. 'Should not retrieve the data properly'
  527. );
  528. }
  529. /**
  530. * Test Cases for getStorage
  531. *
  532. * @return array
  533. */
  534. function casesGetStorage()
  535. {
  536. return array(
  537. 'file' => array(
  538. 'output',
  539. array(
  540. 'defaultgroup' => '',
  541. 'cachebase' => JPATH_BASE . '/unittest/cache',
  542. 'lifetime' => 15 * 60, // minutes to seconds
  543. 'storage' => 'file',
  544. ),
  545. 'JCacheStorageFile',
  546. ),
  547. 'apc' => array(
  548. 'output',
  549. array(
  550. 'defaultgroup' => '',
  551. 'cachebase' => JPATH_BASE . '/unittest/cache',
  552. 'lifetime' => 15 * 60, // minutes to seconds
  553. 'storage' => 'apc',
  554. ),
  555. 'JCacheStorageApc',
  556. ),
  557. 'xcache' => array(
  558. 'output',
  559. array(
  560. 'defaultgroup' => '',
  561. 'cachebase' => JPATH_BASE . '/unittest/cache',
  562. 'lifetime' => 15 * 60, // minutes to seconds
  563. 'storage' => 'xcache',
  564. ),
  565. 'JCacheStorageXcache',
  566. ),
  567. 'memcache' => array(
  568. 'output',
  569. array(
  570. 'defaultgroup' => '',
  571. 'cachebase' => JPATH_BASE . '/unittest/cache',
  572. 'lifetime' => 15 * 60, // minutes to seconds
  573. 'storage' => 'memcache',
  574. ),
  575. 'JCacheStorageMemcache',
  576. )/*,
  577. 'eaccelerator' => array(
  578. 'output',
  579. array(
  580. 'defaultgroup' => '',
  581. 'cachebase' => JPATH_BASE . '/unittest/cache',
  582. 'lifetime' => 15 * 60, // minutes to seconds
  583. 'storage' => 'eaccelerator',
  584. ),
  585. 'JCacheStorageEaccelerator',
  586. )*/
  587. );
  588. }
  589. /**
  590. * Testing getStorage
  591. *
  592. * @param string cache handler
  593. * @param array options for cache handler
  594. * @param string expected storage class
  595. *
  596. * @return void
  597. * @dataProvider casesGetStorage
  598. * @todo Implement test_getStorage().
  599. */
  600. public function testGetStorage( $handler, $options, $expected )
  601. {
  602. $this->object = JCache::getInstance($handler, $options);
  603. $this->assertThat(
  604. $this->object->cache->_getStorage(),
  605. $this->isInstanceOf($expected)
  606. );
  607. }
  608. }
  609. ?>