CommonBackendTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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-2010 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-2010 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. abstract 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. if ($this->_instance) {
  91. $this->_instance->clean();
  92. }
  93. $this->rmdir();
  94. }
  95. public function testConstructorCorrectCall()
  96. {
  97. $this->fail('PLEASE IMPLEMENT A testConstructorCorrectCall !!!');
  98. }
  99. public function testConstructorBadOption()
  100. {
  101. try {
  102. $class = $this->_className;
  103. $test = new $class(array(1 => 'bar'));
  104. } catch (Zend_Cache_Exception $e) {
  105. return;
  106. }
  107. $this->fail('Zend_Cache_Exception was expected but not thrown');
  108. }
  109. public function testSetDirectivesCorrectCall()
  110. {
  111. $this->_instance->setDirectives(array('lifetime' => 3600, 'logging' => true));
  112. }
  113. public function testSetDirectivesBadArgument()
  114. {
  115. try {
  116. $this->_instance->setDirectives('foo');
  117. } catch (Zend_Cache_Exception $e) {
  118. return;
  119. }
  120. $this->fail('Zend_Cache_Exception was expected but not thrown');
  121. }
  122. public function testSetDirectivesBadDirective()
  123. {
  124. // A bad directive (not known by a specific backend) is possible
  125. // => so no exception here
  126. $this->_instance->setDirectives(array('foo' => true, 'lifetime' => 3600));
  127. }
  128. public function testSetDirectivesBadDirective2()
  129. {
  130. try {
  131. $this->_instance->setDirectives(array('foo' => true, 12 => 3600));
  132. } catch (Zend_Cache_Exception $e) {
  133. return;
  134. }
  135. $this->fail('Zend_Cache_Exception was expected but not thrown');
  136. }
  137. public function testSaveCorrectCall()
  138. {
  139. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  140. $this->assertTrue($res);
  141. }
  142. public function testSaveWithNullLifeTime()
  143. {
  144. $this->_instance->setDirectives(array('lifetime' => null));
  145. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  146. $this->assertTrue($res);
  147. }
  148. public function testSaveWithSpecificLifeTime()
  149. {
  150. $this->_instance->setDirectives(array('lifetime' => 3600));
  151. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'), 10);
  152. $this->assertTrue($res);
  153. }
  154. public function testRemoveCorrectCall()
  155. {
  156. $this->assertTrue($this->_instance->remove('bar'));
  157. $this->assertFalse($this->_instance->test('bar'));
  158. $this->assertFalse($this->_instance->remove('barbar'));
  159. $this->assertFalse($this->_instance->test('barbar'));
  160. }
  161. public function testTestWithAnExistingCacheId()
  162. {
  163. $res = $this->_instance->test('bar');
  164. if (!$res) {
  165. $this->fail('test() return false');
  166. }
  167. if (!($res > 999999)) {
  168. $this->fail('test() return an incorrect integer');
  169. }
  170. return;
  171. }
  172. public function testTestWithANonExistingCacheId()
  173. {
  174. $this->assertFalse($this->_instance->test('barbar'));
  175. }
  176. public function testTestWithAnExistingCacheIdAndANullLifeTime()
  177. {
  178. $this->_instance->setDirectives(array('lifetime' => null));
  179. $res = $this->_instance->test('bar');
  180. if (!$res) {
  181. $this->fail('test() return false');
  182. }
  183. if (!($res > 999999)) {
  184. $this->fail('test() return an incorrect integer');
  185. }
  186. return;
  187. }
  188. public function testGetWithANonExistingCacheId()
  189. {
  190. $this->assertFalse($this->_instance->load('barbar'));
  191. }
  192. public function testGetWithAnExistingCacheId()
  193. {
  194. $this->assertEquals('bar : data to cache', $this->_instance->load('bar'));
  195. }
  196. public function testGetWithAnExistingCacheIdAndUTFCharacters()
  197. {
  198. $data = '"""""' . "'" . '\n' . 'ééééé';
  199. $this->_instance->save($data, 'foo');
  200. $this->assertEquals($data, $this->_instance->load('foo'));
  201. }
  202. public function testGetWithAnExpiredCacheId()
  203. {
  204. $this->_instance->___expire('bar');
  205. $this->_instance->setDirectives(array('lifetime' => -1));
  206. $this->assertFalse($this->_instance->load('bar'));
  207. $this->assertEquals('bar : data to cache', $this->_instance->load('bar', true));
  208. }
  209. public function testCleanModeAll()
  210. {
  211. $this->assertTrue($this->_instance->clean('all'));
  212. $this->assertFalse($this->_instance->test('bar'));
  213. $this->assertFalse($this->_instance->test('bar2'));
  214. }
  215. public function testCleanModeOld()
  216. {
  217. $this->_instance->___expire('bar2');
  218. $this->assertTrue($this->_instance->clean('old'));
  219. $this->assertTrue($this->_instance->test('bar') > 999999);
  220. $this->assertFalse($this->_instance->test('bar2'));
  221. }
  222. public function testCleanModeMatchingTags()
  223. {
  224. $this->assertTrue($this->_instance->clean('matchingTag', array('tag3')));
  225. $this->assertFalse($this->_instance->test('bar'));
  226. $this->assertFalse($this->_instance->test('bar2'));
  227. }
  228. public function testCleanModeMatchingTags2()
  229. {
  230. $this->assertTrue($this->_instance->clean('matchingTag', array('tag3', 'tag4')));
  231. $this->assertFalse($this->_instance->test('bar'));
  232. $this->assertTrue($this->_instance->test('bar2') > 999999);
  233. }
  234. public function testCleanModeNotMatchingTags()
  235. {
  236. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag3')));
  237. $this->assertTrue($this->_instance->test('bar') > 999999);
  238. $this->assertTrue($this->_instance->test('bar2') > 999999);
  239. }
  240. public function testCleanModeNotMatchingTags2()
  241. {
  242. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4')));
  243. $this->assertTrue($this->_instance->test('bar') > 999999);
  244. $this->assertFalse($this->_instance->test('bar2'));
  245. }
  246. public function testCleanModeNotMatchingTags3()
  247. {
  248. $this->assertTrue($this->_instance->clean('notMatchingTag', array('tag4', 'tag1')));
  249. $this->assertTrue($this->_instance->test('bar') > 999999);
  250. $this->assertTrue($this->_instance->test('bar2') > 999999);
  251. $this->assertFalse($this->_instance->test('bar3'));
  252. }
  253. }