FileFrontendTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /**
  7. * Zend_Cache
  8. */
  9. require_once 'Zend/Cache.php';
  10. require_once 'Zend/Cache/Frontend/File.php';
  11. require_once 'Zend/Cache/Backend/Test.php';
  12. /**
  13. * PHPUnit test case
  14. */
  15. require_once 'PHPUnit/Framework/TestCase.php';
  16. /**
  17. * @package Zend_Cache
  18. * @subpackage UnitTests
  19. */
  20. class Zend_Cache_FileFrontendTest extends PHPUnit_Framework_TestCase {
  21. private $_instance1;
  22. private $_instance2;
  23. private $_instance3;
  24. private $_instance4;
  25. private $_masterFile;
  26. private $_masterFile1;
  27. private $_masterFile2;
  28. public function setUp()
  29. {
  30. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  31. $this->_masterFile = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master';
  32. $this->_masterFile1 = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master1';
  33. $this->_masterFile2 = $this->_getTmpDirWindows() . DIRECTORY_SEPARATOR . 'zend_cache_master2';
  34. } else {
  35. $this->_masterFile = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master';
  36. $this->_masterFile1 = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master1';
  37. $this->_masterFile2 = $this->_getTmpDirUnix() . DIRECTORY_SEPARATOR . 'zend_cache_master2';
  38. }
  39. if (!$this->_instance1) {
  40. touch($this->_masterFile, 123455);
  41. $this->_instance1 = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile));
  42. $this->_backend = new Zend_Cache_Backend_Test();
  43. $this->_instance1->setBackend($this->_backend);
  44. }
  45. if (!$this->_instance2) {
  46. touch($this->_masterFile);
  47. $this->_instance2 = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile));
  48. $this->_backend = new Zend_Cache_Backend_Test();
  49. $this->_instance2->setBackend($this->_backend);
  50. }
  51. if (!$this->_instance3) {
  52. touch($this->_masterFile1, 123455);
  53. touch($this->_masterFile2, 123455);
  54. $this->_instance3 = new Zend_Cache_Frontend_File(array('master_files' => array($this->_masterFile1, $this->_masterFile2)));
  55. $this->_backend = new Zend_Cache_Backend_Test();
  56. $this->_instance3->setBackend($this->_backend);
  57. }
  58. if (!$this->_instance4) {
  59. touch($this->_masterFile1);
  60. touch($this->_masterFile2);
  61. $this->_instance4 = new Zend_Cache_Frontend_File(array('master_files' => array($this->_masterFile1, $this->_masterFile2)));
  62. $this->_backend = new Zend_Cache_Backend_Test();
  63. $this->_instance4->setBackend($this->_backend);
  64. }
  65. }
  66. public function tearDown()
  67. {
  68. unset($this->_instance1);
  69. unlink($this->_masterFile);
  70. unlink($this->_masterFile1);
  71. unlink($this->_masterFile2);
  72. }
  73. private function _getTmpDirWindows()
  74. {
  75. if (isset($_ENV['TEMP'])) {
  76. return $_ENV['TEMP'];
  77. }
  78. if (isset($_ENV['TMP'])) {
  79. return $_ENV['TMP'];
  80. }
  81. if (isset($_ENV['windir'])) {
  82. return $_ENV['windir'] . '\\temp';
  83. }
  84. if (isset($_ENV['SystemRoot'])) {
  85. return $_ENV['SystemRoot'] . '\\temp';
  86. }
  87. if (isset($_SERVER['TEMP'])) {
  88. return $_SERVER['TEMP'];
  89. }
  90. if (isset($_SERVER['TMP'])) {
  91. return $_SERVER['TMP'];
  92. }
  93. if (isset($_SERVER['windir'])) {
  94. return $_SERVER['windir'] . '\\temp';
  95. }
  96. if (isset($_SERVER['SystemRoot'])) {
  97. return $_SERVER['SystemRoot'] . '\\temp';
  98. }
  99. return '\temp';
  100. }
  101. private function _getTmpDirUnix()
  102. {
  103. if (isset($_ENV['TMPDIR'])) {
  104. return $_ENV['TMPDIR'];
  105. }
  106. if (isset($_SERVER['TMPDIR'])) {
  107. return $_SERVER['TMPDIR'];
  108. }
  109. return '/tmp';
  110. }
  111. public function testConstructorCorrectCall()
  112. {
  113. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 'lifetime' => 3600, 'caching' => true));
  114. }
  115. public function testConstructorBadCall1()
  116. {
  117. # no masterfile
  118. try {
  119. $test = new Zend_Cache_Frontend_File(array('lifetime' => 3600, 'caching' => true));
  120. } catch (Zend_Cache_Exception $e) {
  121. return;
  122. }
  123. $this->fail('Zend_Cache_Exception was expected but not thrown');
  124. }
  125. public function testConstructorBadCall2()
  126. {
  127. # incorrect option
  128. try {
  129. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 0 => 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 testTestCorrectCall1()
  136. {
  137. $this->assertFalse($this->_instance1->test('false'));
  138. }
  139. public function testTestCorrectCall2()
  140. {
  141. $this->assertTrue($this->_instance1->test('cache_id') > 1);
  142. }
  143. public function testTestCorrectCall3()
  144. {
  145. $this->assertFalse($this->_instance2->test('cache_id'));
  146. }
  147. public function testGetCorrectCall1()
  148. {
  149. $this->assertFalse($this->_instance1->load('false'));
  150. }
  151. public function testGetCorrectCall2()
  152. {
  153. $this->assertEquals('foo', $this->_instance1->load('cache_id'));
  154. }
  155. public function testTestCorrectCall4()
  156. {
  157. $this->assertFalse($this->_instance4->test('cache_id'));
  158. }
  159. public function testTestCorrectCall5()
  160. {
  161. $this->assertFalse($this->_instance3->load('false'));
  162. }
  163. public function testGetCorrectCall3()
  164. {
  165. $this->assertFalse($this->_instance2->load('cache_id'));
  166. }
  167. public function testConstructorWithABadMasterFile()
  168. {
  169. try {
  170. $instance = new Zend_Cache_Frontend_File(array('master_file' => '/foo/bar/ljhfdjh/qhskldhqjk'));
  171. } catch (Zend_Cache_Exception $e) {
  172. return;
  173. }
  174. $this->fail('Zend_Cache_Exception was expected but not thrown');
  175. }
  176. public function testGetWithDoNotTestCacheValidity()
  177. {
  178. $this->assertEquals('foo', $this->_instance1->load('cache_id', true));
  179. }
  180. }