2
0

CommonBackendTest.php 9.0 KB

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