CommonBackendTest.php 9.1 KB

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