D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
psa
/
admin
/
plib
/
modules
/
catalog
/
vendor
/
tedivm
/
stash
/
tests
/
Stash
/
Test
/
Driver
/
Filename :
FileSystemTest.php
back
Copy
<?php /* * This file is part of the Stash package. * * (c) Robert Hafner <tedivm@tedivm.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace PleskCatalog\Stash\Test\Driver; use PleskCatalog\Stash\Test\Stubs\PoolGetDriverStub; use PleskCatalog\Stash\Driver\FileSystem; use PleskCatalog\Stash\Item; function strdup($str) { return $str; } /** * @package Stash * @author Robert Hafner <tedivm@tedivm.com> */ class FileSystemTest extends AbstractDriverTestCase { protected $driverClass = 'PleskCatalog\\Stash\\Driver\\FileSystem'; protected $extension = '.php'; protected $persistence = \true; protected function getOptions($options = array()) { return \array_merge(array('memKeyLimit' => 2), $options); } public function testOptionKeyHashFunctionException() { $this->expectException('RuntimeException'); $driver = new FileSystem($this->getOptions(array('keyHashFunction' => 'foobar_' . \mt_rand()))); } public function testOptionEncoderObjectException() { $this->expectException('RuntimeException'); $encoder = new \stdClass(); $driver = new FileSystem($this->getOptions(array('encoder' => $encoder))); } public function testOptionEncoderStringException() { $this->expectException('RuntimeException'); $encoder = 'stdClass'; $driver = new FileSystem($this->getOptions(array('encoder' => $encoder))); } public function testOptionEncoderAsObject() { $encoder = new \PleskCatalog\Stash\Driver\FileSystem\NativeEncoder(); $driver = new FileSystem($this->getOptions(array('encoder' => $encoder))); } public function testOptionEncoderAsString() { $encoder = 'PleskCatalog\\Stash\\Driver\\FileSystem\\NativeEncoder'; $driver = new FileSystem($this->getOptions(array('encoder' => $encoder))); } public function testOptionKeyHashFunction() { $driver = new FileSystem(array('keyHashFunction' => 'md5')); } /** * Test that the paths are created using the key hash function. */ public function testOptionKeyHashFunctionDirs() { $hashfunctions = array('PleskCatalog\\Stash\\Test\\Driver\\strdup', 'strrev', 'md5', function ($value) { return \abs(\crc32($value)); }); $paths = array('one', 'two', 'three', 'four'); foreach ($hashfunctions as $hashfunction) { $driver = new FileSystem($this->getOptions(array('keyHashFunction' => $hashfunction, 'path' => \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash', 'dirSplit' => 1))); $rand = \str_repeat(\uniqid(), 32); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($paths); $item->set($rand)->save(); $allpaths = \array_merge(array('cache'), $paths); $predicted = \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash' . \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, \array_map($hashfunction, $allpaths)) . $this->extension; $this->assertFileExists($predicted); } } /** * Test if the dir split functionality cleanly finished an uneven split e.g. when requested to split "one" into 2, it should split it in "o" and "ne" */ public function testDirSplitFinishesCorrectly() { $paths = array('one', 'two', 'three', 'four'); $outputpaths = array('o', 'ne', 't', 'wo', 'th', 'ree', 'fo', 'ur'); $driver = new FileSystem($this->getOptions(array('keyHashFunction' => function ($key) { return $key; }, 'path' => \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash', 'dirSplit' => 2))); $rand = \str_repeat(\uniqid(), 32); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($paths); $item->set($rand)->save(); $allpaths = \array_merge(array('ca', 'che'), $outputpaths); $predicted = \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash' . \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, $allpaths) . $this->extension; $this->assertFileExists($predicted); } /** * Test creation of directories with long paths (Windows issue) * * Regression test for https://github.com/tedivm/Stash/issues/61 * * There are currently no short term plans to allow long paths in PHP windows * http://www.mail-archive.com/internals@lists.php.net/msg62672.html * */ public function testLongPathFolderCreation() { if (\strtolower(\substr(\PHP_OS, 0, 3)) !== 'win') { $this->markTestSkipped('This test can only occur on Windows based systems.'); } $cachePath = \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash'; $driver = new FileSystem($this->getOptions(array('keyHashFunction' => 'PleskCatalog\\Stash\\Test\\Driver\\strdup', 'path' => $cachePath, 'dirSplit' => 1))); $key = array(); // MAX_PATH is 260 - http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx while (\strlen($cachePath . \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, $key)) < 259) { // 32 character string typical of an md5 sum $key[] = "abcdefghijklmnopqrstuvwxyz123456"; } $key[] = "abcdefghijklmnopqrstuvwxyz123456"; $this->expiration = \time() + 3600; $this->setExpectedException('PleskCatalog\\Stash\\Exception\\WindowsPathMaxLengthException'); $driver->storeData($key, "test", $this->expiration); } /** * Test creation of file with long paths (Windows issue) * * Regression test for https://github.com/tedivm/Stash/issues/61 * * There are currently no short term plans to allow long paths in PHP windows * http://www.mail-archive.com/internals@lists.php.net/msg62672.html * */ public function testLongPathFileCreation() { if (\strtolower(\substr(\PHP_OS, 0, 3)) !== 'win') { $this->markTestSkipped('This test can only occur on Windows based systems.'); } $cachePath = \sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'stash'; $driver = new FileSystem($this->getOptions(array('keyHashFunction' => 'PleskCatalog\\Stash\\Test\\Driver\\strdup', 'path' => $cachePath, 'dirSplit' => 1))); $key = array(); // MAX_PATH is 260 - http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx while (\strlen($cachePath . \DIRECTORY_SEPARATOR . \implode(\DIRECTORY_SEPARATOR, $key)) < 259) { // 32 character string typical of an md5 sum $key[] = "abcdefghijklmnopqrstuvwxyz123456"; } $this->expiration = \time() + 3600; $this->setExpectedException('PleskCatalog\\Stash\\Exception\\WindowsPathMaxLengthException'); $driver->storeData($key, "test", $this->expiration); } }