FileFrontendTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  23. * Zend_Cache
  24. */
  25. require_once 'Zend/Cache.php';
  26. require_once 'Zend/Cache/Frontend/File.php';
  27. require_once 'Zend/Cache/Backend/Test.php';
  28. /**
  29. * PHPUnit test case
  30. */
  31. require_once 'PHPUnit/Framework/TestCase.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Cache
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Cache
  39. */
  40. class Zend_Cache_FileFrontendTest extends PHPUnit_Framework_TestCase {
  41. private $_instance1;
  42. private $_instance2;
  43. private $_instance3;
  44. private $_instance4;
  45. private $_masterFile;
  46. private $_masterFile1;
  47. private $_masterFile2;
  48. public function setUp()
  49. {
  50. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  51. $this->_masterFile = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master';
  52. $this->_masterFile1 = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master1';
  53. $this->_masterFile2 = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master2';
  54. } else {
  55. $this->_masterFile = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master';
  56. $this->_masterFile1 = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master1';
  57. $this->_masterFile2 = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master2';
  58. }
  59. if (!$this->_instance1) {
  60. touch($this->_masterFile, 123455);
  61. $this->_instance1 = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile));
  62. $this->_backend = new Zend_Cache_Backend_Test();
  63. $this->_instance1->setBackend($this->_backend);
  64. }
  65. if (!$this->_instance2) {
  66. touch($this->_masterFile);
  67. $this->_instance2 = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile));
  68. $this->_backend = new Zend_Cache_Backend_Test();
  69. $this->_instance2->setBackend($this->_backend);
  70. }
  71. if (!$this->_instance3) {
  72. touch($this->_masterFile1, 123455);
  73. touch($this->_masterFile2, 123455);
  74. $this->_instance3 = new Zend_Cache_Frontend_File(
  75. array(
  76. 'master_files' => array(
  77. // ZF-10682: test Undefined offset: 0
  78. 'file1' => $this->_masterFile1,
  79. 'file2' => $this->_masterFile2
  80. )
  81. )
  82. );
  83. $this->_backend = new Zend_Cache_Backend_Test();
  84. $this->_instance3->setBackend($this->_backend);
  85. }
  86. if (!$this->_instance4) {
  87. touch($this->_masterFile1);
  88. touch($this->_masterFile2);
  89. $this->_instance4 = new Zend_Cache_Frontend_File(array('master_files' => array($this->_masterFile1, $this->_masterFile2)));
  90. $this->_backend = new Zend_Cache_Backend_Test();
  91. $this->_instance4->setBackend($this->_backend);
  92. }
  93. }
  94. public function tearDown()
  95. {
  96. unset($this->_instance1);
  97. unlink($this->_masterFile);
  98. unlink($this->_masterFile1);
  99. unlink($this->_masterFile2);
  100. }
  101. private function _getTmpDirWindows()
  102. {
  103. if (isset($_ENV['TEMP'])) {
  104. return $_ENV['TEMP'];
  105. }
  106. if (isset($_ENV['TMP'])) {
  107. return $_ENV['TMP'];
  108. }
  109. if (isset($_ENV['windir'])) {
  110. return $_ENV['windir'] . '\\temp';
  111. }
  112. if (isset($_ENV['SystemRoot'])) {
  113. return $_ENV['SystemRoot'] . '\\temp';
  114. }
  115. if (isset($_SERVER['TEMP'])) {
  116. return $_SERVER['TEMP'];
  117. }
  118. if (isset($_SERVER['TMP'])) {
  119. return $_SERVER['TMP'];
  120. }
  121. if (isset($_SERVER['windir'])) {
  122. return $_SERVER['windir'] . '\\temp';
  123. }
  124. if (isset($_SERVER['SystemRoot'])) {
  125. return $_SERVER['SystemRoot'] . '\\temp';
  126. }
  127. return '\temp';
  128. }
  129. private function _getTmpDirUnix()
  130. {
  131. if (isset($_ENV['TMPDIR'])) {
  132. return $_ENV['TMPDIR'];
  133. }
  134. if (isset($_SERVER['TMPDIR'])) {
  135. return $_SERVER['TMPDIR'];
  136. }
  137. return '/tmp';
  138. }
  139. public function testConstructorCorrectCall()
  140. {
  141. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 'lifetime' => 3600, 'caching' => true));
  142. }
  143. public function testConstructorBadCall1()
  144. {
  145. # no masterfile
  146. try {
  147. $test = new Zend_Cache_Frontend_File(array('lifetime' => 3600, 'caching' => true));
  148. } catch (Zend_Cache_Exception $e) {
  149. return;
  150. }
  151. $this->fail('Zend_Cache_Exception was expected but not thrown');
  152. }
  153. public function testConstructorBadCall2()
  154. {
  155. # incorrect option
  156. try {
  157. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 0 => 3600));
  158. } catch (Zend_Cache_Exception $e) {
  159. return;
  160. }
  161. $this->fail('Zend_Cache_Exception was expected but not thrown');
  162. }
  163. public function testTestCorrectCall1()
  164. {
  165. $this->assertFalse($this->_instance1->test('false'));
  166. }
  167. public function testTestCorrectCall2()
  168. {
  169. $this->assertTrue($this->_instance1->test('cache_id') > 1);
  170. }
  171. public function testTestCorrectCall3()
  172. {
  173. $this->assertFalse($this->_instance2->test('cache_id'));
  174. }
  175. public function testGetCorrectCall1()
  176. {
  177. $this->assertFalse($this->_instance1->load('false'));
  178. }
  179. public function testGetCorrectCall2()
  180. {
  181. $this->assertEquals('foo', $this->_instance1->load('cache_id'));
  182. }
  183. public function testTestCorrectCall4()
  184. {
  185. $this->assertFalse($this->_instance4->test('cache_id'));
  186. }
  187. public function testTestCorrectCall5()
  188. {
  189. $this->assertFalse($this->_instance3->load('false'));
  190. }
  191. public function testGetCorrectCall3()
  192. {
  193. $this->assertFalse($this->_instance2->load('cache_id'));
  194. }
  195. public function testConstructorWithABadMasterFile()
  196. {
  197. try {
  198. $instance = new Zend_Cache_Frontend_File(array('master_file' => '/foo/bar/ljhfdjh/qhskldhqjk'));
  199. } catch (Zend_Cache_Exception $e) {
  200. return;
  201. }
  202. $this->fail('Zend_Cache_Exception was expected but not thrown');
  203. }
  204. public function testGetWithDoNotTestCacheValidity()
  205. {
  206. $this->assertEquals('foo', $this->_instance1->load('cache_id', true));
  207. }
  208. }