PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/unit-tests/CacheTest.php

https://github.com/codeanu/cphalcon
PHP | 357 lines | 217 code | 112 blank | 28 comment | 15 complexity | 8f469db176ae6e3d8aabc034883b5cd3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. +------------------------------------------------------------------------+
  4. | Phalcon Framework |
  5. +------------------------------------------------------------------------+
  6. | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
  7. +------------------------------------------------------------------------+
  8. | This source file is subject to the New BSD License that is bundled |
  9. | with this package in the file docs/LICENSE.txt. |
  10. | |
  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@phalconphp.com so we can send you a copy immediately. |
  14. +------------------------------------------------------------------------+
  15. | Authors: Andres Gutierrez <andres@phalconphp.com> |
  16. | Eduar Carvajal <eduar@phalconphp.com> |
  17. +------------------------------------------------------------------------+
  18. */
  19. class CacheTest extends PHPUnit_Framework_TestCase
  20. {
  21. public function setUp()
  22. {
  23. $iterator = new DirectoryIterator('unit-tests/cache/');
  24. foreach ($iterator as $item) {
  25. if (!$item->isDir()) {
  26. unlink($item->getPathname());
  27. }
  28. }
  29. }
  30. public function testOutputFileCache()
  31. {
  32. $time = date('H:i:s');
  33. $frontCache = new Phalcon\Cache\Frontend\Output(array(
  34. 'lifetime' => 2
  35. ));
  36. $cache = new Phalcon\Cache\Backend\File($frontCache, array(
  37. 'cacheDir' => 'unit-tests/cache/'
  38. ));
  39. $this->assertFalse($cache->isStarted());
  40. ob_start();
  41. //First time cache
  42. $content = $cache->start('testoutput');
  43. $this->assertTrue($cache->isStarted());
  44. if ($content !== null) {
  45. $this->assertTrue(false);
  46. }
  47. echo $time;
  48. $cache->save(null, null, null, true);
  49. $obContent = ob_get_contents();
  50. ob_end_clean();
  51. $this->assertEquals($time, $obContent);
  52. $this->assertTrue(file_exists('unit-tests/cache/testoutput'));
  53. //Same cache
  54. $content = $cache->start('testoutput');
  55. $this->assertTrue($cache->isStarted());
  56. if ($content === null) {
  57. $this->assertTrue(false);
  58. }
  59. $this->assertEquals($time, $obContent);
  60. //Refresh cache
  61. sleep(3);
  62. $time2 = date('H:i:s');
  63. ob_start();
  64. $content = $cache->start('testoutput');
  65. $this->assertTrue($cache->isStarted());
  66. if ($content !== null) {
  67. $this->assertTrue(false);
  68. }
  69. echo $time2;
  70. $cache->save(null, null, null, true);
  71. $obContent2 = ob_get_contents();
  72. ob_end_clean();
  73. $this->assertNotEquals($time, $obContent2);
  74. $this->assertEquals($time2, $obContent2);
  75. $keys = $cache->queryKeys();
  76. $this->assertEquals($keys, array(
  77. 0 => 'testoutput',
  78. ));
  79. $this->assertTrue($cache->delete('testoutput'));
  80. }
  81. public function testDataFileCache()
  82. {
  83. $frontCache = new Phalcon\Cache\Frontend\Data();
  84. $cache = new Phalcon\Cache\Backend\File($frontCache, array(
  85. 'cacheDir' => 'unit-tests/cache/'
  86. ));
  87. $this->assertFalse($cache->isStarted());
  88. $cache->save('test-data', "nothing interesting");
  89. $this->assertTrue(file_exists('unit-tests/cache/testdata'));
  90. $cachedContent = $cache->get('test-data');
  91. $this->assertEquals($cachedContent, "nothing interesting");
  92. $cache->save('test-data', "sure, nothing interesting");
  93. $cachedContent = $cache->get('test-data');
  94. $this->assertEquals($cachedContent, "sure, nothing interesting");
  95. $this->assertTrue($cache->delete('test-data'));
  96. }
  97. private function _prepareMemcached()
  98. {
  99. if (!extension_loaded('memcache')) {
  100. $this->markTestAsSkipped('Warning: memcache extension is not loaded');
  101. return false;
  102. }
  103. $memcache = new Memcache();
  104. $this->assertFalse(!$memcache->connect('localhost', 11211));
  105. return $memcache;
  106. }
  107. public function testOutputMemcacheCache()
  108. {
  109. $memcache = $this->_prepareMemcached();
  110. if (!$memcache) {
  111. return false;
  112. }
  113. $memcache->delete('test-output');
  114. $time = date('H:i:s');
  115. $frontCache = new Phalcon\Cache\Frontend\Output(array(
  116. 'lifetime' => 2
  117. ));
  118. $cache = new Phalcon\Cache\Backend\Memcache($frontCache);
  119. ob_start();
  120. //First time cache
  121. $content = $cache->start('test-output');
  122. if ($content !== null) {
  123. $this->assertTrue(false);
  124. }
  125. echo $time;
  126. $cache->save(null, null, null, true);
  127. $obContent = ob_get_contents();
  128. ob_end_clean();
  129. $this->assertEquals($time, $obContent);
  130. $this->assertEquals($time, $memcache->get('test-output'));
  131. //Expect same cache
  132. $content = $cache->start('test-output');
  133. if ($content === null) {
  134. $this->assertTrue(false);
  135. }
  136. $this->assertEquals($time, $obContent);
  137. //Refresh cache
  138. sleep(3);
  139. $time2 = date('H:i:s');
  140. ob_start();
  141. $content = $cache->start('test-output');
  142. if($content!==null){
  143. $this->assertTrue(false);
  144. }
  145. echo $time2;
  146. $cache->save(null, null, null, true);
  147. $obContent2 = ob_get_contents();
  148. ob_end_clean();
  149. $this->assertNotEquals($time, $obContent2);
  150. $this->assertEquals($time2, $obContent2);
  151. $this->assertEquals($time2, $memcache->get('test-output'));
  152. //Delete entry from cache
  153. $this->assertTrue($cache->delete('test-output'));
  154. $memcache->close();
  155. }
  156. public function testDataMemcachedCache()
  157. {
  158. $memcache = $this->_prepareMemcached();
  159. if (!$memcache) {
  160. return false;
  161. }
  162. $memcache->delete('test-data');
  163. $frontCache = new Phalcon\Cache\Frontend\Data();
  164. $cache = new Phalcon\Cache\Backend\Memcache($frontCache, array(
  165. 'host' => 'localhost',
  166. 'port' => '11211'
  167. ));
  168. $data = array(1, 2, 3, 4, 5);
  169. $cache->save('test-data', $data);
  170. $cachedContent = $cache->get('test-data');
  171. $this->assertEquals($cachedContent, $data);
  172. $cache->save('test-data', "sure, nothing interesting");
  173. $cachedContent = $cache->get('test-data');
  174. $this->assertEquals($cachedContent, "sure, nothing interesting");
  175. $this->assertEquals($cache->queryKeys(), array(
  176. 0 => 'test-data',
  177. ));
  178. $this->assertTrue($cache->delete('test-data'));
  179. }
  180. protected function _prepareApc()
  181. {
  182. if (!extension_loaded('apc')) {
  183. $this->markTestAsSkipped('apc extension is not loaded');
  184. return false;
  185. }
  186. return true;
  187. }
  188. public function testOutputApcCache()
  189. {
  190. $ready = $this->_prepareApc();
  191. if (!$ready) {
  192. return false;
  193. }
  194. apc_delete('_PHCAtest-output');
  195. $time = date('H:i:s');
  196. $frontCache = new Phalcon\Cache\Frontend\Output(array(
  197. 'lifetime' => 2
  198. ));
  199. $cache = new Phalcon\Cache\Backend\Apc($frontCache);
  200. ob_start();
  201. //First time cache
  202. $content = $cache->start('test-output');
  203. if ($content !== null) {
  204. $this->assertTrue(false);
  205. }
  206. echo $time;
  207. $cache->save(null, null, null, true);
  208. $obContent = ob_get_contents();
  209. ob_end_clean();
  210. $this->assertEquals($time, $obContent);
  211. $this->assertEquals($time, apc_fetch('_PHCAtest-output'));
  212. //Expect same cache
  213. $content = $cache->start('test-output');
  214. if ($content === null) {
  215. $this->assertTrue(false);
  216. }
  217. $this->assertEquals($content, $obContent);
  218. $this->assertEquals($content, apc_fetch('_PHCAtest-output'));
  219. //Query keys
  220. $keys = $cache->queryKeys();
  221. $this->assertEquals($keys, array(
  222. 0 => '_PHCAtest-output',
  223. ));
  224. //Delete entry from cache
  225. $this->assertTrue($cache->delete('test-output'));
  226. }
  227. public function testDataApcCache()
  228. {
  229. $ready = $this->_prepareApc();
  230. if (!$ready) {
  231. return false;
  232. }
  233. apc_delete('_PHCAtest-data');
  234. $frontCache = new Phalcon\Cache\Frontend\Data();
  235. $cache = new Phalcon\Cache\Backend\Apc($frontCache);
  236. $data = array(1, 2, 3, 4, 5);
  237. $cache->save('test-data', $data);
  238. $cachedContent = $cache->get('test-data');
  239. $this->assertEquals($cachedContent, $data);
  240. $cache->save('test-data', "sure, nothing interesting");
  241. $cachedContent = $cache->get('test-data');
  242. $this->assertEquals($cachedContent, "sure, nothing interesting");
  243. $this->assertTrue($cache->delete('test-data'));
  244. }
  245. }