CommonBackendTest.php 8.1 KB

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