3 namespace Doctrine\Tests\Common\Cache;
5 use Doctrine\Common\Cache\Cache;
10 class FileCacheTest extends \Doctrine\Tests\DoctrineTestCase
13 * @var \Doctrine\Common\Cache\FileCache
17 protected function setUp()
19 $this->driver = $this->getMock(
20 'Doctrine\Common\Cache\FileCache',
21 array('doFetch', 'doContains', 'doSave'),
26 public function testFilenameShouldCreateThePathWithOneSubDirectory()
28 $cache = $this->driver;
29 $method = new \ReflectionMethod($cache, 'getFilename');
34 $expectedDir = implode(DIRECTORY_SEPARATOR, $expectedDir);
36 $method->setAccessible(true);
38 $path = $method->invoke($cache, $key);
39 $dirname = pathinfo($path, PATHINFO_DIRNAME);
41 $this->assertEquals(DIRECTORY_SEPARATOR . $expectedDir, $dirname);
44 public function testFileExtensionCorrectlyEscaped()
46 $driver1 = $this->getMock(
47 'Doctrine\Common\Cache\FileCache',
48 array('doFetch', 'doContains', 'doSave'),
51 $driver2 = $this->getMock(
52 'Doctrine\Common\Cache\FileCache',
53 array('doFetch', 'doContains', 'doSave'),
54 array(__DIR__, '.php')
57 $doGetStats = new \ReflectionMethod($driver1, 'doGetStats');
59 $doGetStats->setAccessible(true);
61 $stats1 = $doGetStats->invoke($driver1);
62 $stats2 = $doGetStats->invoke($driver2);
64 $this->assertSame(0, $stats1[Cache::STATS_MEMORY_USAGE]);
65 $this->assertGreaterThan(0, $stats2[Cache::STATS_MEMORY_USAGE]);
71 public function testFileExtensionSlashCorrectlyEscaped()
73 $driver = $this->getMock(
74 'Doctrine\Common\Cache\FileCache',
75 array('doFetch', 'doContains', 'doSave'),
76 array(__DIR__ . '/../', DIRECTORY_SEPARATOR . basename(__FILE__))
79 $doGetStats = new \ReflectionMethod($driver, 'doGetStats');
81 $doGetStats->setAccessible(true);
83 $stats = $doGetStats->invoke($driver);
85 $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_USAGE]);
88 public function testNonIntUmaskThrowsInvalidArgumentException()
90 $this->setExpectedException('InvalidArgumentException');
93 'Doctrine\Common\Cache\FileCache',
94 array('doFetch', 'doContains', 'doSave'),
95 array('', '', 'invalid')
99 public function testGetDirectoryReturnsRealpathDirectoryString()
101 $directory = __DIR__ . '/../';
102 $driver = $this->getMock(
103 'Doctrine\Common\Cache\FileCache',
104 array('doFetch', 'doContains', 'doSave'),
108 $doGetDirectory = new \ReflectionMethod($driver, 'getDirectory');
110 $actualDirectory = $doGetDirectory->invoke($driver);
111 $expectedDirectory = realpath($directory);
113 $this->assertEquals($expectedDirectory, $actualDirectory);
116 public function testGetExtensionReturnsExtensionString()
118 $directory = __DIR__ . '/../';
119 $extension = DIRECTORY_SEPARATOR . basename(__FILE__);
120 $driver = $this->getMock(
121 'Doctrine\Common\Cache\FileCache',
122 array('doFetch', 'doContains', 'doSave'),
123 array($directory, $extension)
126 $doGetExtension = new \ReflectionMethod($driver, 'getExtension');
128 $actualExtension = $doGetExtension->invoke($driver);
130 $this->assertEquals($extension, $actualExtension);
133 const WIN_MAX_PATH_LEN = 258;
135 public static function getBasePathForWindowsPathLengthTests($pathLength)
137 // Not using __DIR__ because it can get screwed up when xdebug debugger is attached.
138 $basePath = realpath(sys_get_temp_dir()) . '/' . uniqid('doctrine-cache', true);
140 /** @noinspection MkdirRaceConditionInspection */
143 $basePath = realpath($basePath);
145 // Test whether the desired path length is odd or even.
146 $desiredPathLengthIsOdd = ($pathLength % 2) == 1;
148 // If the cache key is not too long, the filecache codepath will add
149 // a slash and bin2hex($key). The length of the added portion will be an odd number.
150 // len(desired) = len(base path) + len(slash . bin2hex($key))
153 $basePathLengthShouldBeOdd = !$desiredPathLengthIsOdd;
155 $basePathLengthIsOdd = (strlen($basePath) % 2) == 1;
157 // If the base path needs to be odd or even where it is not, we add an odd number of
158 // characters as a pad. In this case, we're adding '\aa' (or '/aa' depending on platform)
159 // This is all to make it so that the key we're testing would result in
160 // a path that is exactly the length we want to test IF the path length limit
161 // were not in place in FileCache.
162 if ($basePathLengthIsOdd != $basePathLengthShouldBeOdd) {
163 $basePath .= DIRECTORY_SEPARATOR . "aa";
171 * @param string $basePath
175 public static function getKeyAndPathFittingLength($length, $basePath)
177 $baseDirLength = strlen($basePath);
178 $extensionLength = strlen('.doctrine.cache');
179 $directoryLength = strlen(DIRECTORY_SEPARATOR . 'aa' . DIRECTORY_SEPARATOR);
180 $keyLength = $length - ($baseDirLength + $extensionLength + $directoryLength); // - 1 because of slash
182 $key = str_repeat('a', floor($keyLength / 2));
184 $keyHash = hash('sha256', $key);
187 . DIRECTORY_SEPARATOR
188 . substr($keyHash, 0, 2)
189 . DIRECTORY_SEPARATOR
193 $hashedKeyPath = $basePath
194 . DIRECTORY_SEPARATOR
195 . substr($keyHash, 0, 2)
196 . DIRECTORY_SEPARATOR
200 return array($key, $keyPath, $hashedKeyPath);
203 public function getPathLengthsToTest()
205 // Windows officially supports 260 bytes including null terminator
206 // 259 characters is too large due to PHP bug (https://bugs.php.net/bug.php?id=70943)
207 // 260 characters is too large - null terminator is included in allowable length
217 * @runInSeparateProcess
218 * @dataProvider getPathLengthsToTest
220 * @covers \Doctrine\Common\Cache\FileCache::getFilename
223 * @param bool $pathShouldBeHashed
225 public function testWindowsPathLengthLimitationsAreCorrectlyRespected($length, $pathShouldBeHashed)
227 if (! defined('PHP_WINDOWS_VERSION_BUILD')) {
228 define('PHP_WINDOWS_VERSION_BUILD', 'Yes, this is the "usual suspect", with the usual limitations');
231 $basePath = self::getBasePathForWindowsPathLengthTests($length);
233 $fileCache = $this->getMockForAbstractClass(
234 'Doctrine\Common\Cache\FileCache',
235 array($basePath, '.doctrine.cache')
238 list($key, $keyPath, $hashedKeyPath) = self::getKeyAndPathFittingLength($length, $basePath);
240 $getFileName = new \ReflectionMethod($fileCache, 'getFilename');
242 $getFileName->setAccessible(true);
247 sprintf('Path expected to be %d characters long is %d characters long', $length, strlen($keyPath))
250 if ($pathShouldBeHashed) {
251 $keyPath = $hashedKeyPath;
254 if ($pathShouldBeHashed) {
257 $getFileName->invoke($fileCache, $key),
258 'Keys should be hashed correctly if they are over the limit.'
263 $getFileName->invoke($fileCache, $key),
264 'Keys below limit of the allowed length are used directly, unhashed'