PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Cache/CommonBackendTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 290 lines | 233 code | 27 blank | 30 comment | 12 complexity | 010f729b2e3c81d2195566108480eac0 MD5 | raw file
  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: CommonBackendTest.php 24989 2012-06-21 07:24:13Z mabe $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Cache
  25. * @subpackage UnitTests
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @group Zend_Cache
  29. */
  30. abstract class Zend_Cache_CommonBackendTest extends PHPUnit_Framework_TestCase {
  31. protected $_instance;
  32. protected $_className;
  33. protected $_root;
  34. public function __construct($name = null, array $data = array(), $dataName = '')
  35. {
  36. $this->_className = $name;
  37. $this->_root = dirname(__FILE__);
  38. date_default_timezone_set('UTC');
  39. parent::__construct($name, $data, $dataName);
  40. }
  41. public function setUp($notag = false)
  42. {
  43. $this->mkdir();
  44. $this->_instance->setDirectives(array('logging' => true));
  45. if ($notag) {
  46. $this->_instance->save('bar : data to cache', 'bar');
  47. $this->_instance->save('bar2 : data to cache', 'bar2');
  48. $this->_instance->save('bar3 : data to cache', 'bar3');
  49. } else {
  50. $this->_instance->save('bar : data to cache', 'bar', array('tag3', 'tag4'));
  51. $this->_instance->save('bar2 : data to cache', 'bar2', array('tag3', 'tag1'));
  52. $this->_instance->save('bar3 : data to cache', 'bar3', array('tag2', 'tag3'));
  53. }
  54. }
  55. public function mkdir()
  56. {
  57. @mkdir($this->getTmpDir());
  58. }
  59. public function rmdir()
  60. {
  61. $tmpDir = $this->getTmpDir(false);
  62. foreach (glob("$tmpDir*") as $dirname) {
  63. @rmdir($dirname);
  64. }
  65. }
  66. public function getTmpDir($date = true)
  67. {
  68. $suffix = '';
  69. if ($date) {
  70. $suffix = date('mdyHis');
  71. }
  72. if (is_writeable($this->_root)) {
  73. return $this->_root . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
  74. } else {
  75. if (getenv('TMPDIR')){
  76. return getenv('TMPDIR') . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
  77. } else {
  78. die("no writable tmpdir found");
  79. }
  80. }
  81. }
  82. public function tearDown()
  83. {
  84. if ($this->_instance) {
  85. $this->_instance->clean();
  86. }
  87. $this->rmdir();
  88. }
  89. public function testConstructorCorrectCall()
  90. {
  91. $this->fail('PLEASE IMPLEMENT A testConstructorCorrectCall !!!');
  92. }
  93. public function testConstructorBadOption()
  94. {
  95. try {
  96. $class = $this->_className;
  97. $test = new $class(array(1 => 'bar'));
  98. } catch (Zend_Cache_Exception $e) {
  99. return;
  100. }
  101. $this->fail('Zend_Cache_Exception was expected but not thrown');
  102. }
  103. public function testSetDirectivesCorrectCall()
  104. {
  105. $this->_instance->setDirectives(array('lifetime' => 3600, 'logging' => true));
  106. }
  107. public function testSetDirectivesBadArgument()
  108. {
  109. try {
  110. $this->_instance->setDirectives('foo');
  111. } catch (Zend_Cache_Exception $e) {
  112. return;
  113. }
  114. $this->fail('Zend_Cache_Exception was expected but not thrown');
  115. }
  116. public function testSetDirectivesBadDirective()
  117. {
  118. // A bad directive (not known by a specific backend) is possible
  119. // => so no exception here
  120. $this->_instance->setDirectives(array('foo' => true, 'lifetime' => 3600));
  121. }
  122. public function testSetDirectivesBadDirective2()
  123. {
  124. try {
  125. $this->_instance->setDirectives(array('foo' => true, 12 => 3600));
  126. } catch (Zend_Cache_Exception $e) {
  127. return;
  128. }
  129. $this->fail('Zend_Cache_Exception was expected but not thrown');
  130. }
  131. public function testSaveCorrectCall()
  132. {
  133. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  134. $this->assertTrue($res);
  135. }
  136. public function testSaveWithNullLifeTime()
  137. {
  138. $this->_instance->setDirectives(array('lifetime' => null));
  139. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  140. $this->assertTrue($res);
  141. }
  142. public function testSaveWithSpecificLifeTime()
  143. {
  144. $this->_instance->setDirectives(array('lifetime' => 3600));
  145. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'), 10);
  146. $this->assertTrue($res);
  147. }
  148. public function testRemoveCorrectCall()
  149. {
  150. $this->assertTrue($this->_instance->remove('bar'));
  151. $this->assertFalse($this->_instance->test('bar'));
  152. $this->assertFalse($this->_instance->remove('barbar'));
  153. $this->assertFalse($this->_instance->test('barbar'));
  154. }
  155. public function testTestWithAnExistingCacheId()
  156. {
  157. $res = $this->_instance->test('bar');
  158. if (!$res) {
  159. $this->fail('test() return false');
  160. }
  161. if (!($res > 999999)) {
  162. $this->fail('test() return an incorrect integer');
  163. }
  164. return;
  165. }
  166. public function testTestWithANonExistingCacheId()
  167. {
  168. $this->assertFalse($this->_instance->test('barbar'));
  169. }
  170. public function testTestWithAnExistingCacheIdAndANullLifeTime()
  171. {
  172. $this->_instance->setDirectives(array('lifetime' => null));
  173. $res = $this->_instance->test('bar');
  174. if (!$res) {
  175. $this->fail('test() return false');
  176. }
  177. if (!($res > 999999)) {
  178. $this->fail('test() return an incorrect integer');
  179. }
  180. return;
  181. }
  182. public function testGetWithANonExistingCacheId()
  183. {
  184. $this->assertFalse($this->_instance->load('barbar'));
  185. }
  186. public function testGetWithAnExistingCacheId()
  187. {
  188. $this->assertEquals('bar : data to cache', $this->_instance->load('bar'));
  189. }
  190. public function testGetWithAnExistingCacheIdAndUTFCharacters()
  191. {
  192. $data = '"""""' . "'" . '\n' . 'ééééé';
  193. $this->_instance->save($data, 'foo');
  194. $this->assertEquals($data, $this->_instance->load('foo'));
  195. }
  196. public function testGetWithAnExpiredCacheId()
  197. {
  198. $this->_instance->___expire('bar');
  199. $this->_instance->setDirectives(array('lifetime' => -1));
  200. $this->assertFalse($this->_instance->load('bar'));
  201. $this->assertEquals('bar : data to cache', $this->_instance->load('bar', true));
  202. }
  203. public function testCleanModeAll()
  204. {
  205. $this->assertTrue($this->_instance->clean('all'));
  206. $this->assertFalse($this->_instance->test('bar'));
  207. $this->assertFalse($this->_instance->test('bar2'));
  208. }
  209. public function testCleanModeOld()
  210. {
  211. $this->_instance->___expire('bar2');
  212. $this->assertTrue($this->_instance->clean('old'));
  213. $this->assertTrue($this->_instance->test('bar') > 999999);
  214. $this->assertFalse($this->_instance->test('bar2'));
  215. }
  216. public function testCleanModeMatchingTags()
  217. {
  218. $this->assertTrue($this->_instance->clean('matchingTag', array('tag3')));
  219. $this->assertFalse($this->_instance->test('bar'));
  220. $this->assertFalse($this->_instance->test('bar2'));
  221. }
  222. public function testCleanModeMatchingTags2()
  223. {
  224. $this->assertTrue($this->_instance->clean('matchingTag', array('tag3', 'tag4')));
  225. $this->assertFalse($this->_instance->test('bar'));
  226. $this->assertTrue($this->_instance->test('bar2') > 999999);
  227. }
  228. public function testCleanModeNotMatchingTags()
  229. {
  230. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag3')));
  231. $this->assertTrue($this->_instance->test('bar') > 999999);
  232. $this->assertTrue($this->_instance->test('bar2') > 999999);
  233. }
  234. public function testCleanModeNotMatchingTags2()
  235. {
  236. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4')));
  237. $this->assertTrue($this->_instance->test('bar') > 999999);
  238. $this->assertFalse($this->_instance->test('bar2'));
  239. }
  240. public function testCleanModeNotMatchingTags3()
  241. {
  242. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4', 'tag1')));
  243. $this->assertTrue($this->_instance->test('bar') > 999999);
  244. $this->assertTrue($this->_instance->test('bar2') > 999999);
  245. $this->assertFalse($this->_instance->test('bar3'));
  246. }
  247. public function testGetOption()
  248. {
  249. $this->assertTrue(is_numeric($this->_instance->getOption('LifeTime')));
  250. $this->setExpectedException('Zend_Cache_Exception');
  251. $this->_instance->getOption('unknown');
  252. }
  253. }