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

https://github.com/pjwiseman/joomla-cms · PHP · 502 lines · 485 code · 3 blank · 14 comment · 1 complexity · 5f2068ed63d56847fece1d06a6a5baf6 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. /**
  10. * Test class for JCache.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Cache
  14. */
  15. class JCacheTest extends PHPUnit_Framework_TestCase
  16. {
  17. /** @var JCache */
  18. protected $object;
  19. private $available = array();
  20. private $testData_A = "Now is the time for all good people to throw a party.";
  21. private $testData_B = "And this is the cache that tries men's souls";
  22. private $defaultOptions;
  23. /**
  24. * Sets up the fixture, for example, opens a network connection.
  25. * This method is called before a test is executed.
  26. *
  27. * @return void
  28. */
  29. protected function setUp()
  30. {
  31. parent::setUp();
  32. include_once JPATH_PLATFORM . '/joomla/cache/cache.php';
  33. include_once JPATH_PLATFORM . '/joomla/cache/controller.php';
  34. include_once JPATH_PLATFORM . '/joomla/cache/storage.php';
  35. $this->checkAvailability();
  36. }
  37. /**
  38. * Check availability of all cache handlers
  39. *
  40. * @return void
  41. */
  42. private function checkAvailability()
  43. {
  44. $config = JFactory::getConfig();
  45. $host = $config->get('memcache_server_host', 'localhost');
  46. $port = $config->get('memcache_server_port', 11211);
  47. $memcacheServerAvailable = @fsockopen($host, $port, $errNo, $errStr, 0.01);
  48. $this->available['file'] = true;
  49. $this->available['apc'] = extension_loaded('apc');
  50. $this->available['eaccelerator'] = extension_loaded('eaccelerator') && function_exists('eaccelerator_get');
  51. $this->available['memcache'] =
  52. extension_loaded('memcache')
  53. && class_exists('Memcache')
  54. && $memcacheServerAvailable;
  55. $this->available['xcache'] = extension_loaded('xcache');
  56. }
  57. private function setDefaultOptions()
  58. {
  59. $this->defaultOptions = array(
  60. 'defaultgroup' => '',
  61. 'cachebase' => JPATH_BASE . '/cache',
  62. 'lifetime' => 15 * 60, // Minutes to seconds
  63. 'storage' => 'file',
  64. );
  65. }
  66. /**
  67. * Test Cases for getInstance
  68. *
  69. * @return array
  70. */
  71. public function casesGetInstance()
  72. {
  73. $this->setDefaultOptions();
  74. return array(
  75. 'simple' => array(
  76. 'output',
  77. array('storage' => 'file'),
  78. 'JCacheControllerOutput',
  79. ),
  80. 'complexOutput' => array(
  81. 'output',
  82. $this->defaultOptions,
  83. 'JCacheControllerOutput',
  84. ),
  85. 'complexPage' => array(
  86. 'page',
  87. $this->defaultOptions,
  88. 'JCacheControllerPage',
  89. ),
  90. 'complexView' => array(
  91. 'view',
  92. $this->defaultOptions,
  93. 'JCacheControllerView',
  94. ),
  95. 'complexCallback' => array(
  96. 'callback',
  97. $this->defaultOptions,
  98. 'JCacheControllerCallback',
  99. ),
  100. );
  101. }
  102. /**
  103. * Testing getInstance
  104. *
  105. * @param string $handler cache handler
  106. * @param array $options options for cache handler
  107. * @param string $expClass name of expected cache class
  108. *
  109. * @return void
  110. *
  111. * @dataProvider casesGetInstance
  112. */
  113. public function testGetInstance($handler, $options, $expClass)
  114. {
  115. $this->object = JCache::getInstance($handler, $options);
  116. $this->assertInstanceOf(
  117. $expClass,
  118. $this->object
  119. );
  120. }
  121. /**
  122. * Test Cases for setCaching
  123. *
  124. * @return array
  125. */
  126. public function casesSetCaching()
  127. {
  128. $this->setDefaultOptions();
  129. return array(
  130. 'simple' => array(
  131. 'output',
  132. array('storage' => 'file'),
  133. ),
  134. 'complexOutput' => array(
  135. 'output',
  136. $this->defaultOptions,
  137. ),
  138. 'complexPage' => array(
  139. 'page',
  140. $this->defaultOptions,
  141. ),
  142. 'complexView' => array(
  143. 'view',
  144. $this->defaultOptions,
  145. ),
  146. 'complexCallback' => array(
  147. 'callback',
  148. $this->defaultOptions,
  149. ),
  150. );
  151. }
  152. /**
  153. * Testing setCaching
  154. *
  155. * @param string $handler cache handler
  156. * @param array $options options for cache handler
  157. *
  158. * @return void
  159. *
  160. * @dataProvider casesSetCaching
  161. */
  162. public function testSetCaching($handler, $options)
  163. {
  164. $this->object = JCache::getInstance($handler, $options);
  165. $caching = (bool) $this->object->options['caching'];
  166. $toggled = !$caching;
  167. $this->object->setCaching($toggled);
  168. $this->assertEquals(
  169. $toggled,
  170. $this->object->options['caching']
  171. );
  172. }
  173. /**
  174. * Test Cases for setLifetime
  175. *
  176. * @return array
  177. */
  178. public function casesSetLifetime()
  179. {
  180. $this->setDefaultOptions();
  181. return array(
  182. 'simple' => array(
  183. 'output',
  184. array('storage' => 'file'),
  185. 900,
  186. ),
  187. 'complexOutput' => array(
  188. 'output',
  189. $this->defaultOptions,
  190. 15 * 60,
  191. ),
  192. 'complexPage' => array(
  193. 'page',
  194. $this->defaultOptions,
  195. 15 * 60,
  196. ),
  197. 'complexView' => array(
  198. 'view',
  199. $this->defaultOptions,
  200. 15 * 60,
  201. ),
  202. 'complexCallback' => array(
  203. 'callback',
  204. $this->defaultOptions,
  205. 15 * 60,
  206. ),
  207. );
  208. }
  209. /**
  210. * Testing setLifeTime
  211. *
  212. * @param string $handler cache handler
  213. * @param array $options options for cache handler
  214. * @param integer $lifetime lifetime of cache to be set
  215. *
  216. * @return void
  217. *
  218. * @dataProvider casesSetLifetime
  219. */
  220. public function testSetLifeTime($handler, $options, $lifetime)
  221. {
  222. $this->object = JCache::getInstance($handler, $options);
  223. $this->object->setLifeTime($lifetime);
  224. $this->assertEquals(
  225. $lifetime,
  226. $this->object->options['lifetime']
  227. );
  228. }
  229. /**
  230. * Test Cases for getStores
  231. *
  232. * @return array
  233. */
  234. public function casesGetStores()
  235. {
  236. $this->setDefaultOptions();
  237. return array(
  238. 'simple' => array(
  239. 'output',
  240. array('storage' => 'file'),
  241. 'file',
  242. ),
  243. 'complexOutput' => array(
  244. 'output',
  245. $this->defaultOptions,
  246. 'file',
  247. ),
  248. 'complexPage' => array(
  249. 'page',
  250. $this->defaultOptions,
  251. 'file',
  252. ),
  253. 'complexView' => array(
  254. 'view',
  255. $this->defaultOptions,
  256. 'file',
  257. ),
  258. 'complexCallback' => array(
  259. 'callback',
  260. $this->defaultOptions,
  261. 'file',
  262. ),
  263. );
  264. }
  265. /**
  266. * Testing getStores
  267. *
  268. * @param string $handler cache handler
  269. * @param array $options options for cache handler
  270. * @param string $expected returned stores
  271. *
  272. * @return void
  273. *
  274. * @dataProvider casesGetStores
  275. */
  276. public function testGetStores($handler, $options, $expected)
  277. {
  278. $this->object = JCache::getInstance($handler, $options);
  279. $this->assertEquals(
  280. $expected,
  281. $this->object->options['storage']
  282. );
  283. }
  284. /**
  285. * Test Cases for get() / store()
  286. *
  287. * @return array
  288. */
  289. public function casesStore()
  290. {
  291. $this->setDefaultOptions();
  292. return array(
  293. 'simple' => array(
  294. 'output',
  295. array('lifetime' => 600, 'storage' => 'file'),
  296. 42,
  297. '',
  298. $this->testData_B,
  299. false,
  300. ),
  301. 'complexOutput' => array(
  302. 'output',
  303. $this->defaultOptions,
  304. 42,
  305. '',
  306. $this->testData_B,
  307. false,
  308. ),
  309. );
  310. }
  311. /**
  312. * Testing store() and get()
  313. *
  314. * @param string $handler cache handler
  315. * @param array $options options for cache handler
  316. * @param string $id cache element ID
  317. * @param string $group cache group
  318. * @param string $data data to be cached
  319. * @param string $expected expected return
  320. *
  321. * @return void
  322. *
  323. * @dataProvider casesStore
  324. */
  325. public function testStoreAndGet($handler, $options, $id, $group, $data, $expected)
  326. {
  327. $this->object = JCache::getInstance($handler, $options);
  328. $this->object->setCaching(true);
  329. $this->assertTrue(
  330. $this->object->store($data, $id, $group)
  331. );
  332. $this->assertEquals(
  333. $data,
  334. $this->object->get($id, $group)
  335. );
  336. }
  337. /**
  338. * Testing remove().
  339. *
  340. * @return void
  341. */
  342. public function testRemove()
  343. {
  344. $options = array('storage' => 'file');
  345. $this->object = JCache::getInstance('output', $options);
  346. $this->object->setCaching(true);
  347. $this->object->store($this->testData_A, 42, '');
  348. $this->object->store($this->testData_B, 43, '');
  349. $this->assertEquals(
  350. $this->testData_B,
  351. $this->object->get(43, '')
  352. );
  353. $this->assertTrue(
  354. $this->object->remove(43, '')
  355. );
  356. $this->assertFalse(
  357. $this->object->get(43, '')
  358. );
  359. $this->assertEquals(
  360. $this->testData_A,
  361. $this->object->get(42, '')
  362. );
  363. }
  364. /**
  365. * Testing clean().
  366. *
  367. * @return void
  368. */
  369. public function testClean()
  370. {
  371. $options = array('storage' => 'file');
  372. $this->object = JCache::getInstance('output', $options);
  373. $this->object->setCaching(true);
  374. $this->object->store($this->testData_A, 42, '');
  375. $this->object->store($this->testData_B, 43, '');
  376. $this->assertEquals(
  377. $this->testData_B,
  378. $this->object->get(43, '')
  379. );
  380. $this->assertTrue(
  381. $this->object->clean('')
  382. );
  383. $this->assertFalse(
  384. $this->object->get(43, '')
  385. );
  386. $this->assertFalse(
  387. $this->object->get(42, '')
  388. );
  389. }
  390. /**
  391. * Testing Gc().
  392. *
  393. * @medium
  394. *
  395. * @return void
  396. */
  397. public function testGc()
  398. {
  399. $this->object = JCache::getInstance('output', array('lifetime' => 2, 'defaultgroup' => ''));
  400. $this->object->store($this->testData_A, 42, '');
  401. $this->object->store($this->testData_B, 43, '');
  402. sleep(5);
  403. $this->object->gc();
  404. $this->assertFalse(
  405. $this->object->get(42, '')
  406. );
  407. $this->assertFalse(
  408. $this->object->get(43, '')
  409. );
  410. }
  411. /**
  412. * Test Cases for getStorage
  413. *
  414. * @return array
  415. */
  416. public function casesGetStorage()
  417. {
  418. $this->setDefaultOptions();
  419. $storages = array(
  420. 'file' => 'JCacheStorageFile',
  421. 'apc' => 'JCacheStorageApc',
  422. 'xcache' => 'JCacheStorageXcache',
  423. 'memcache' => 'JCacheStorageMemcache',
  424. 'eaccelerator' => 'JCacheStorageEaccelerator',
  425. );
  426. $cases = array();
  427. foreach ($storages as $key => $class)
  428. {
  429. $options = $this->defaultOptions;
  430. $options['storage'] = $key;
  431. $cases[$key] = array('output', $options, $class);
  432. }
  433. return $cases;
  434. }
  435. /**
  436. * Testing getStorage
  437. *
  438. * @param string $handler cache handler
  439. * @param array $options options for cache handler
  440. * @param string $expected expected storage class
  441. *
  442. * @return void
  443. *
  444. * @dataProvider casesGetStorage
  445. */
  446. public function testGetStorage($handler, $options, $expected)
  447. {
  448. if (!$this->available[$options['storage']])
  449. {
  450. $this->markTestSkipped("The {$options['storage']} storage handler is currently not available");
  451. }
  452. $this->object = JCache::getInstance($handler, $options);
  453. $this->assertThat(
  454. $this->object->cache->_getStorage(),
  455. $this->isInstanceOf($expected)
  456. );
  457. }
  458. }