/ZendFramework/tests/Zend/Cache/FileFrontendTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base · PHP · 234 lines · 175 code · 22 blank · 37 comment · 16 complexity · 435d4d20260e8972f29e98823b6de250 MD5 · raw file

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