FileBackendTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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-2015 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/Backend/File.php';
  27. /**
  28. * Zend_Log
  29. */
  30. require_once 'Zend/Log.php';
  31. require_once 'Zend/Log/Writer/Null.php';
  32. /**
  33. * Common tests for backends
  34. */
  35. require_once 'CommonExtendedBackendTest.php';
  36. /**
  37. * @category Zend
  38. * @package Zend_Cache
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Cache
  43. */
  44. class Zend_Cache_FileBackendTest extends Zend_Cache_CommonExtendedBackendTest {
  45. protected $_instance;
  46. protected $_instance2;
  47. protected $_cache_dir;
  48. public function __construct($name = null, array $data = array(), $dataName = '')
  49. {
  50. parent::__construct('Zend_Cache_Backend_File', $data, $dataName);
  51. }
  52. public function setUp($notag = false)
  53. {
  54. $this->mkdir();
  55. $this->_cache_dir = $this->getTmpDir() . DIRECTORY_SEPARATOR;
  56. $this->_instance = new Zend_Cache_Backend_File(array(
  57. 'cache_dir' => $this->_cache_dir,
  58. ));
  59. $logger = new Zend_Log(new Zend_Log_Writer_Null());
  60. $this->_instance->setDirectives(array('logger' => $logger));
  61. parent::setUp($notag);
  62. }
  63. public function tearDown()
  64. {
  65. parent::tearDown();
  66. unset($this->_instance);
  67. }
  68. public function testSetDeprecatedHashedDirectoryUmask()
  69. {
  70. try {
  71. $cache = new Zend_Cache_Backend_File(array(
  72. 'cache_dir' => $this->_cache_dir,
  73. 'hashed_directory_umask' => 0700,
  74. ));
  75. $this->fail("Missing expected E_USER_NOTICE error");
  76. } catch (PHPUnit_Framework_Error $e) {
  77. if ($e->getCode() != E_USER_NOTICE) {
  78. throw $e;
  79. }
  80. $this->assertContains('hashed_directory_umask', $e->getMessage());
  81. }
  82. }
  83. public function testSetDeprecatedCacheFileUmask()
  84. {
  85. try {
  86. $cache = new Zend_Cache_Backend_File(array(
  87. 'cache_dir' => $this->_cache_dir,
  88. 'cache_file_umask' => 0700,
  89. ));
  90. $this->fail("Missing expected E_USER_NOTICE error");
  91. } catch (PHPUnit_Framework_Error $e) {
  92. if ($e->getCode() != E_USER_NOTICE) {
  93. throw $e;
  94. }
  95. $this->assertContains('cache_file_umask', $e->getMessage());
  96. }
  97. }
  98. public function testConstructorCorrectCall()
  99. {
  100. $test = new Zend_Cache_Backend_File(array());
  101. }
  102. public function testConstructorWithABadFileNamePrefix()
  103. {
  104. try {
  105. $class = new Zend_Cache_Backend_File(array(
  106. 'file_name_prefix' => 'foo bar'
  107. ));
  108. } catch (Zend_Cache_Exception $e) {
  109. return;
  110. }
  111. $this->fail('Zend_Cache_Exception was expected but not thrown');
  112. }
  113. public function testGetWithANonExistingCacheIdAndANullLifeTime()
  114. {
  115. $this->_instance->setDirectives(array('lifetime' => null));
  116. $this->assertFalse($this->_instance->load('barbar'));
  117. }
  118. public function testSaveCorrectCallWithHashedDirectoryStructure()
  119. {
  120. $this->_instance->setOption('hashed_directory_level', 2);
  121. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  122. $this->assertTrue($res);
  123. }
  124. public function testCleanModeAllWithHashedDirectoryStructure()
  125. {
  126. // clean files created in setUp (without hashed directory level) first
  127. $this->assertTrue($this->_instance->clean('all'));
  128. // set the hashed directory mode
  129. $this->_instance->setOption('hashed_directory_level', 2);
  130. // save the data again
  131. $this->_instance->save('bar : data to cache', 'bar');
  132. $this->_instance->save('bar2 : data to cache', 'bar2');
  133. $this->_instance->save('bar3 : data to cache', 'bar3');
  134. // now delete them
  135. $this->assertTrue($this->_instance->clean('all'));
  136. $this->assertFalse($this->_instance->test('bar'));
  137. $this->assertFalse($this->_instance->test('bar2'));
  138. }
  139. public function testSaveWithABadCacheDir()
  140. {
  141. if (getenv('TRAVIS')) {
  142. $this->markTestSkipped(
  143. 'Test randomly fail on Travis CI.'
  144. );
  145. }
  146. $this->_instance->setOption('cache_dir', '/foo/bar/lfjlqsdjfklsqd/');
  147. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  148. $this->assertFalse($res);
  149. }
  150. public function testShouldProperlyCleanCacheNoMatterTheCacheId()
  151. {
  152. // the 'zzz' and 'ďťň' keys will be sorted after internal-metadatas file
  153. $keys = array(
  154. '9230de5449e0c818ed4804587ed422d5',
  155. 'zzz',
  156. 'Zend_LocaleC_cs_CZ_date_',
  157. 'ďťň'
  158. );
  159. foreach ($keys as $key) {
  160. $this->_instance->save('data to cache', $key);
  161. }
  162. $this->assertTrue($this->_instance->clean(Zend_Cache::CLEANING_MODE_ALL));
  163. }
  164. /**
  165. * The CLEANING_MODE_ALL should delete also old orphaned metadatafiles
  166. */
  167. public function testShouldDeleteOldMetadataFiles()
  168. {
  169. // simulate orphaned metadata file
  170. $fn = $this->_cache_dir
  171. . DIRECTORY_SEPARATOR
  172. . 'zend_cache---internal-metadatas---7a38619e110f03740970cbcd5310f33f';
  173. $file = fopen($fn, 'a+');
  174. fclose($file);
  175. $this->assertTrue(file_exists($fn));
  176. $this->assertTrue($this->_instance->clean(Zend_Cache::CLEANING_MODE_ALL));
  177. $this->assertFalse(file_exists($fn));
  178. }
  179. }