FileBackendTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2014 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-2014 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. $this->_instance->setOption('hashed_directory_level', 2);
  127. $this->assertTrue($this->_instance->clean('all'));
  128. $this->assertFalse($this->_instance->test('bar'));
  129. $this->assertFalse($this->_instance->test('bar2'));
  130. }
  131. public function testSaveWithABadCacheDir()
  132. {
  133. $this->_instance->setOption('cache_dir', '/foo/bar/lfjlqsdjfklsqd/');
  134. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  135. $this->assertFalse($res);
  136. }
  137. }