FileFrontendTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-2009 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-2009 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(array('master_files' => array($this->_masterFile1, $this->_masterFile2)));
  75. $this->_backend = new Zend_Cache_Backend_Test();
  76. $this->_instance3->setBackend($this->_backend);
  77. }
  78. if (!$this->_instance4) {
  79. touch($this->_masterFile1);
  80. touch($this->_masterFile2);
  81. $this->_instance4 = new Zend_Cache_Frontend_File(array('master_files' => array($this->_masterFile1, $this->_masterFile2)));
  82. $this->_backend = new Zend_Cache_Backend_Test();
  83. $this->_instance4->setBackend($this->_backend);
  84. }
  85. }
  86. public function tearDown()
  87. {
  88. unset($this->_instance1);
  89. unlink($this->_masterFile);
  90. unlink($this->_masterFile1);
  91. unlink($this->_masterFile2);
  92. }
  93. private function _getTmpDirWindows()
  94. {
  95. if (isset($_ENV['TEMP'])) {
  96. return $_ENV['TEMP'];
  97. }
  98. if (isset($_ENV['TMP'])) {
  99. return $_ENV['TMP'];
  100. }
  101. if (isset($_ENV['windir'])) {
  102. return $_ENV['windir'] . '\\temp';
  103. }
  104. if (isset($_ENV['SystemRoot'])) {
  105. return $_ENV['SystemRoot'] . '\\temp';
  106. }
  107. if (isset($_SERVER['TEMP'])) {
  108. return $_SERVER['TEMP'];
  109. }
  110. if (isset($_SERVER['TMP'])) {
  111. return $_SERVER['TMP'];
  112. }
  113. if (isset($_SERVER['windir'])) {
  114. return $_SERVER['windir'] . '\\temp';
  115. }
  116. if (isset($_SERVER['SystemRoot'])) {
  117. return $_SERVER['SystemRoot'] . '\\temp';
  118. }
  119. return '\temp';
  120. }
  121. private function _getTmpDirUnix()
  122. {
  123. if (isset($_ENV['TMPDIR'])) {
  124. return $_ENV['TMPDIR'];
  125. }
  126. if (isset($_SERVER['TMPDIR'])) {
  127. return $_SERVER['TMPDIR'];
  128. }
  129. return '/tmp';
  130. }
  131. public function testConstructorCorrectCall()
  132. {
  133. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 'lifetime' => 3600, 'caching' => true));
  134. }
  135. public function testConstructorBadCall1()
  136. {
  137. # no masterfile
  138. try {
  139. $test = new Zend_Cache_Frontend_File(array('lifetime' => 3600, 'caching' => true));
  140. } catch (Zend_Cache_Exception $e) {
  141. return;
  142. }
  143. $this->fail('Zend_Cache_Exception was expected but not thrown');
  144. }
  145. public function testConstructorBadCall2()
  146. {
  147. # incorrect option
  148. try {
  149. $test = new Zend_Cache_Frontend_File(array('master_file' => $this->_masterFile, 0 => 3600));
  150. } catch (Zend_Cache_Exception $e) {
  151. return;
  152. }
  153. $this->fail('Zend_Cache_Exception was expected but not thrown');
  154. }
  155. public function testTestCorrectCall1()
  156. {
  157. $this->assertFalse($this->_instance1->test('false'));
  158. }
  159. public function testTestCorrectCall2()
  160. {
  161. $this->assertTrue($this->_instance1->test('cache_id') > 1);
  162. }
  163. public function testTestCorrectCall3()
  164. {
  165. $this->assertFalse($this->_instance2->test('cache_id'));
  166. }
  167. public function testGetCorrectCall1()
  168. {
  169. $this->assertFalse($this->_instance1->load('false'));
  170. }
  171. public function testGetCorrectCall2()
  172. {
  173. $this->assertEquals('foo', $this->_instance1->load('cache_id'));
  174. }
  175. public function testTestCorrectCall4()
  176. {
  177. $this->assertFalse($this->_instance4->test('cache_id'));
  178. }
  179. public function testTestCorrectCall5()
  180. {
  181. $this->assertFalse($this->_instance3->load('false'));
  182. }
  183. public function testGetCorrectCall3()
  184. {
  185. $this->assertFalse($this->_instance2->load('cache_id'));
  186. }
  187. public function testConstructorWithABadMasterFile()
  188. {
  189. try {
  190. $instance = new Zend_Cache_Frontend_File(array('master_file' => '/foo/bar/ljhfdjh/qhskldhqjk'));
  191. } catch (Zend_Cache_Exception $e) {
  192. return;
  193. }
  194. $this->fail('Zend_Cache_Exception was expected but not thrown');
  195. }
  196. public function testGetWithDoNotTestCacheValidity()
  197. {
  198. $this->assertEquals('foo', $this->_instance1->load('cache_id', true));
  199. }
  200. }