FileBackendTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/Backend/File.php';
  11. /**
  12. * Zend_Log
  13. */
  14. require_once 'Zend/Log.php';
  15. require_once 'Zend/Log/Writer/Null.php';
  16. /**
  17. * Common tests for backends
  18. */
  19. require_once 'CommonExtendedBackendTest.php';
  20. /**
  21. * PHPUnit test case
  22. */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /**
  25. * @package Zend_Cache
  26. * @subpackage UnitTests
  27. */
  28. class Zend_Cache_FileBackendTest extends Zend_Cache_CommonExtendedBackendTest {
  29. protected $_instance;
  30. protected $_instance2;
  31. protected $_cache_dir;
  32. public function __construct($name = null, array $data = array(), $dataName = '')
  33. {
  34. parent::__construct('Zend_Cache_Backend_File', $data, $dataName);
  35. }
  36. public function setUp($notag = false)
  37. {
  38. $this->mkdir();
  39. $this->_cache_dir = $this->getTmpDir() . DIRECTORY_SEPARATOR;
  40. $this->_instance = new Zend_Cache_Backend_File(array(
  41. 'cache_dir' => $this->_cache_dir,
  42. ));
  43. $logger = new Zend_Log(new Zend_Log_Writer_Null());
  44. $this->_instance->setDirectives(array('logger' => $logger));
  45. parent::setUp($notag);
  46. }
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. unset($this->_instance);
  51. }
  52. public function testConstructorCorrectCall()
  53. {
  54. $test = new Zend_Cache_Backend_File(array());
  55. }
  56. public function testConstructorWithABadFileNamePrefix()
  57. {
  58. try {
  59. $class = new Zend_Cache_Backend_File(array(
  60. 'file_name_prefix' => 'foo bar'
  61. ));
  62. } catch (Zend_Cache_Exception $e) {
  63. return;
  64. }
  65. $this->fail('Zend_Cache_Exception was expected but not thrown');
  66. }
  67. public function testGetWithANonExistingCacheIdAndANullLifeTime()
  68. {
  69. $this->_instance->setDirectives(array('lifetime' => null));
  70. $this->assertFalse($this->_instance->load('barbar'));
  71. }
  72. public function testSaveCorrectCallWithHashedDirectoryStructure()
  73. {
  74. $this->_instance->setOption('hashed_directory_level', 2);
  75. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  76. $this->assertTrue($res);
  77. }
  78. public function testCleanModeAllWithHashedDirectoryStructure()
  79. {
  80. $this->_instance->setOption('hashed_directory_level', 2);
  81. $this->assertTrue($this->_instance->clean('all'));
  82. $this->assertFalse($this->_instance->test('bar'));
  83. $this->assertFalse($this->_instance->test('bar2'));
  84. }
  85. public function testSaveWithABadCacheDir()
  86. {
  87. $this->_instance->setOption('cache_dir', '/foo/bar/lfjlqsdjfklsqd/');
  88. $res = $this->_instance->save('data to cache', 'foo', array('tag1', 'tag2'));
  89. $this->assertFalse($res);
  90. }
  91. }