FileSystemTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Stash\Test\Driver;
  11. use Stash\Test\Stubs\PoolGetDriverStub;
  12. use Stash\Driver\FileSystem;
  13. use Stash\Item;
  14. function strdup($str)
  15. {
  16. return $str;
  17. }
  18. /**
  19. * @package Stash
  20. * @author Robert Hafner <tedivm@tedivm.com>
  21. */
  22. class FileSystemTest extends AbstractDriverTest
  23. {
  24. protected $driverClass = 'Stash\Driver\FileSystem';
  25. protected $extension = '.php';
  26. protected $persistence = true;
  27. protected function getOptions($options = array())
  28. {
  29. return array_merge(array('memKeyLimit' => 2), $options);
  30. }
  31. /**
  32. * @expectedException Stash\Exception\RuntimeException
  33. */
  34. public function testOptionKeyHashFunctionException()
  35. {
  36. $driver = new FileSystem($this->getOptions(array('keyHashFunction' => 'foobar_'.mt_rand())));
  37. }
  38. /**
  39. * @expectedException Stash\Exception\RuntimeException
  40. */
  41. public function testOptionEncoderObjectException()
  42. {
  43. $encoder = new \stdClass();
  44. $driver = new FileSystem($this->getOptions(array('encoder' => $encoder)));
  45. }
  46. /**
  47. * @expectedException Stash\Exception\RuntimeException
  48. */
  49. public function testOptionEncoderStringException()
  50. {
  51. $encoder = 'stdClass';
  52. $driver = new FileSystem($this->getOptions(array('encoder' => $encoder)));
  53. }
  54. public function testOptionEncoderAsObject()
  55. {
  56. $encoder = new \Stash\Driver\FileSystem\NativeEncoder();
  57. $driver = new FileSystem($this->getOptions(array('encoder' => $encoder)));
  58. }
  59. public function testOptionEncoderAsString()
  60. {
  61. $encoder = '\Stash\Driver\FileSystem\NativeEncoder';
  62. $driver = new FileSystem($this->getOptions(array('encoder' => $encoder)));
  63. }
  64. public function testOptionKeyHashFunction()
  65. {
  66. $driver = new FileSystem(array('keyHashFunction' => 'md5'));
  67. }
  68. /**
  69. * Test that the paths are created using the key hash function.
  70. */
  71. public function testOptionKeyHashFunctionDirs()
  72. {
  73. $hashfunctions = array('Stash\Test\Driver\strdup', 'strrev', 'md5', function ($value) {
  74. return abs(crc32($value));
  75. });
  76. $paths = array('one', 'two', 'three', 'four');
  77. foreach ($hashfunctions as $hashfunction) {
  78. $driver = new FileSystem($this->getOptions(array(
  79. 'keyHashFunction' => $hashfunction,
  80. 'path' => sys_get_temp_dir().DIRECTORY_SEPARATOR.'stash',
  81. 'dirSplit' => 1
  82. )));
  83. $rand = str_repeat(uniqid(), 32);
  84. $item = new Item();
  85. $poolStub = new PoolGetDriverStub();
  86. $poolStub->setDriver($driver);
  87. $item->setPool($poolStub);
  88. $item->setKey($paths);
  89. $item->set($rand)->save();
  90. $allpaths = array_merge(array('cache'), $paths);
  91. $predicted = sys_get_temp_dir().
  92. DIRECTORY_SEPARATOR.
  93. 'stash'.
  94. DIRECTORY_SEPARATOR.
  95. implode(DIRECTORY_SEPARATOR, array_map($hashfunction, $allpaths)).
  96. $this->extension;
  97. $this->assertFileExists($predicted);
  98. }
  99. }
  100. /**
  101. * Test creation of directories with long paths (Windows issue)
  102. *
  103. * Regression test for https://github.com/tedivm/Stash/issues/61
  104. *
  105. * There are currently no short term plans to allow long paths in PHP windows
  106. * http://www.mail-archive.com/internals@lists.php.net/msg62672.html
  107. *
  108. */
  109. public function testLongPathFolderCreation()
  110. {
  111. if (strtolower(substr(PHP_OS, 0, 3)) !== 'win') {
  112. $this->markTestSkipped('This test can only occur on Windows based systems.');
  113. }
  114. $cachePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'stash';
  115. $driver = new FileSystem($this->getOptions(array(
  116. 'keyHashFunction' => 'Stash\Test\Driver\strdup',
  117. 'path' => $cachePath,
  118. 'dirSplit' => 1
  119. )));
  120. $key=array();
  121. // MAX_PATH is 260 - http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
  122. while (strlen($cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $key)) < 259) {
  123. // 32 character string typical of an md5 sum
  124. $key[]="abcdefghijklmnopqrstuvwxyz123456";
  125. }
  126. $key[]="abcdefghijklmnopqrstuvwxyz123456";
  127. $this->expiration = time() + 3600;
  128. $this->setExpectedException('\Stash\Exception\WindowsPathMaxLengthException');
  129. $driver->storeData($key, "test", $this->expiration);
  130. }
  131. /**
  132. * Test creation of file with long paths (Windows issue)
  133. *
  134. * Regression test for https://github.com/tedivm/Stash/issues/61
  135. *
  136. * There are currently no short term plans to allow long paths in PHP windows
  137. * http://www.mail-archive.com/internals@lists.php.net/msg62672.html
  138. *
  139. */
  140. public function testLongPathFileCreation()
  141. {
  142. if (strtolower(substr(PHP_OS, 0, 3)) !== 'win') {
  143. $this->markTestSkipped('This test can only occur on Windows based systems.');
  144. }
  145. $cachePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'stash';
  146. $driver = new FileSystem($this->getOptions(array(
  147. 'keyHashFunction' => 'Stash\Test\Driver\strdup',
  148. 'path' => $cachePath,
  149. 'dirSplit' => 1
  150. )));
  151. $key=array();
  152. // MAX_PATH is 260 - http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
  153. while (strlen($cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $key)) < 259) {
  154. // 32 character string typical of an md5 sum
  155. $key[]="abcdefghijklmnopqrstuvwxyz123456";
  156. }
  157. $this->expiration = time() + 3600;
  158. $this->setExpectedException('\Stash\Exception\WindowsPathMaxLengthException');
  159. $driver->storeData($key, "test", $this->expiration);
  160. }
  161. }