From 8e83e404fbb1d0dc770e5b41fa315a674541459a Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 21 Aug 2016 18:46:34 -0400 Subject: Add FileCache driver --- app/Core/Base.php | 1 + app/Core/Cache/Base.php | 38 --- app/Core/Cache/BaseCache.php | 71 +++++ app/Core/Cache/CacheInterface.php | 45 --- app/Core/Cache/FileCache.php | 98 +++++++ app/Core/Cache/MemoryCache.php | 10 +- app/Core/Plugin/Installer.php | 27 +- app/Core/Tool.php | 28 ++ app/ServiceProvider/CacheProvider.php | 41 +++ app/ServiceProvider/ClassProvider.php | 3 - app/common.php | 1 + app/constants.php | 6 + tests/units/Base.php | 1 + tests/units/Core/Cache/FileCacheTest.php | 186 +++++++++++++ tests/units/Core/FileStorageTest.php | 310 --------------------- tests/units/Core/ObjectStorage/FileStorageTest.php | 310 +++++++++++++++++++++ 16 files changed, 750 insertions(+), 426 deletions(-) delete mode 100644 app/Core/Cache/Base.php create mode 100644 app/Core/Cache/BaseCache.php delete mode 100644 app/Core/Cache/CacheInterface.php create mode 100644 app/Core/Cache/FileCache.php create mode 100644 app/ServiceProvider/CacheProvider.php create mode 100644 tests/units/Core/Cache/FileCacheTest.php delete mode 100644 tests/units/Core/FileStorageTest.php create mode 100644 tests/units/Core/ObjectStorage/FileStorageTest.php diff --git a/app/Core/Base.php b/app/Core/Base.php index 68604785..df82febd 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -18,6 +18,7 @@ use Pimple\Container; * @property \Kanboard\Core\Action\ActionManager $actionManager * @property \Kanboard\Core\ExternalLink\ExternalLinkManager $externalLinkManager * @property \Kanboard\Core\Cache\MemoryCache $memoryCache + * @property \Kanboard\Core\Cache\BaseCache $cacheDriver * @property \Kanboard\Core\Event\EventManager $eventManager * @property \Kanboard\Core\Group\GroupManager $groupManager * @property \Kanboard\Core\Http\Client $httpClient diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/Base.php deleted file mode 100644 index d62b8507..00000000 --- a/app/Core/Cache/Base.php +++ /dev/null @@ -1,38 +0,0 @@ -get($key); - - if ($result === null) { - $result = call_user_func_array(array($class, $method), array_splice($args, 1)); - $this->set($key, $result); - } - - return $result; - } -} diff --git a/app/Core/Cache/BaseCache.php b/app/Core/Cache/BaseCache.php new file mode 100644 index 00000000..04f8d220 --- /dev/null +++ b/app/Core/Cache/BaseCache.php @@ -0,0 +1,71 @@ +get($key); + + if ($result === null) { + $result = call_user_func_array(array($class, $method), array_splice($args, 1)); + $this->set($key, $result); + } + + return $result; + } +} diff --git a/app/Core/Cache/CacheInterface.php b/app/Core/Cache/CacheInterface.php deleted file mode 100644 index d9e9747a..00000000 --- a/app/Core/Cache/CacheInterface.php +++ /dev/null @@ -1,45 +0,0 @@ -createCacheFolder(); + file_put_contents($this->getFilenameFromKey($key), serialize($value)); + } + + /** + * Retrieve an item from the cache by key + * + * @access public + * @param string $key + * @return mixed Null when not found, cached value otherwise + */ + public function get($key) + { + $filename = $this->getFilenameFromKey($key); + + if (file_exists($filename)) { + return unserialize(file_get_contents($filename)); + } + + return null; + } + + /** + * Remove all items from the cache + * + * @access public + */ + public function flush() + { + $this->createCacheFolder(); + Tool::removeAllFiles(CACHE_DIR, false); + } + + /** + * Remove an item from the cache + * + * @access public + * @param string $key + */ + public function remove($key) + { + $filename = $this->getFilenameFromKey($key); + + if (file_exists($filename)) { + unlink($filename); + } + } + + /** + * Get absolute filename from the key + * + * @access protected + * @param string $key + * @return string + */ + protected function getFilenameFromKey($key) + { + return CACHE_DIR.DIRECTORY_SEPARATOR.$key; + } + + /** + * Create cache folder if missing + * + * @access protected + * @throws LogicException + */ + protected function createCacheFolder() + { + if (! is_dir(CACHE_DIR)) { + if (! mkdir(CACHE_DIR, 0755)) { + throw new LogicException('Unable to create cache directory: '.CACHE_DIR); + } + } + } +} diff --git a/app/Core/Cache/MemoryCache.php b/app/Core/Cache/MemoryCache.php index 39e3947b..4fb94728 100644 --- a/app/Core/Cache/MemoryCache.php +++ b/app/Core/Cache/MemoryCache.php @@ -3,12 +3,12 @@ namespace Kanboard\Core\Cache; /** - * Memory Cache + * Memory Cache Driver * - * @package cache + * @package Kanboard\Core\Cache * @author Frederic Guillot */ -class MemoryCache extends Base implements CacheInterface +class MemoryCache extends BaseCache { /** * Container @@ -19,7 +19,7 @@ class MemoryCache extends Base implements CacheInterface private $storage = array(); /** - * Save a new value in the cache + * Store an item in the cache * * @access public * @param string $key @@ -31,7 +31,7 @@ class MemoryCache extends Base implements CacheInterface } /** - * Fetch value from cache + * Retrieve an item from the cache by key * * @access public * @param string $key diff --git a/app/Core/Plugin/Installer.php b/app/Core/Plugin/Installer.php index 48c4d978..b3618aeb 100644 --- a/app/Core/Plugin/Installer.php +++ b/app/Core/Plugin/Installer.php @@ -2,9 +2,8 @@ namespace Kanboard\Core\Plugin; -use RecursiveDirectoryIterator; -use RecursiveIteratorIterator; use ZipArchive; +use Kanboard\Core\Tool; /** * Class Installer @@ -64,7 +63,7 @@ class Installer extends \Kanboard\Core\Base throw new PluginInstallerException(e('You don\'t have the permission to remove this plugin.')); } - $this->removeAllDirectories($pluginFolder); + Tool::removeAllFiles($pluginFolder); } /** @@ -137,26 +136,4 @@ class Installer extends \Kanboard\Core\Base unlink($zip->filename); $zip->close(); } - - /** - * Remove recursively a directory - * - * @access protected - * @param string $directory - */ - protected function removeAllDirectories($directory) - { - $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); - $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); - - foreach ($files as $file) { - if ($file->isDir()) { - rmdir($file->getRealPath()); - } else { - unlink($file->getRealPath()); - } - } - - rmdir($directory); - } } diff --git a/app/Core/Tool.php b/app/Core/Tool.php index bfa6c955..9b8820eb 100644 --- a/app/Core/Tool.php +++ b/app/Core/Tool.php @@ -3,6 +3,8 @@ namespace Kanboard\Core; use Pimple\Container; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; /** * Tool class @@ -12,6 +14,32 @@ use Pimple\Container; */ class Tool { + /** + * Remove recursively a directory + * + * @static + * @access public + * @param string $directory + * @param bool $removeDirectory + */ + public static function removeAllFiles($directory, $removeDirectory = true) + { + $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); + + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getRealPath()); + } else { + unlink($file->getRealPath()); + } + } + + if ($removeDirectory) { + rmdir($directory); + } + } + /** * Build dependency injection container from an array * diff --git a/app/ServiceProvider/CacheProvider.php b/app/ServiceProvider/CacheProvider.php new file mode 100644 index 00000000..0d56e601 --- /dev/null +++ b/app/ServiceProvider/CacheProvider.php @@ -0,0 +1,41 @@ + array( - 'MemoryCache', - ), 'Core\Plugin' => array( 'Hook', ), diff --git a/app/common.php b/app/common.php index 15fd7a75..e5490c11 100644 --- a/app/common.php +++ b/app/common.php @@ -35,6 +35,7 @@ $container->register(new Kanboard\ServiceProvider\MailProvider()); $container->register(new Kanboard\ServiceProvider\HelperProvider()); $container->register(new Kanboard\ServiceProvider\SessionProvider()); $container->register(new Kanboard\ServiceProvider\LoggingProvider()); +$container->register(new Kanboard\ServiceProvider\CacheProvider()); $container->register(new Kanboard\ServiceProvider\DatabaseProvider()); $container->register(new Kanboard\ServiceProvider\AuthenticationProvider()); $container->register(new Kanboard\ServiceProvider\NotificationProvider()); diff --git a/app/constants.php b/app/constants.php index 40b88fe9..3adb0835 100644 --- a/app/constants.php +++ b/app/constants.php @@ -12,6 +12,12 @@ defined('DATA_DIR') or define('DATA_DIR', ROOT_DIR.DIRECTORY_SEPARATOR.'data'); // Files directory (attachments) defined('FILES_DIR') or define('FILES_DIR', DATA_DIR.DIRECTORY_SEPARATOR.'files'); +// Available cache drivers are "file" and "memory" +defined('CACHE_DRIVER') or define('CACHE_DRIVER', 'memory'); + +// Cache folder (file driver) +defined('CACHE_DIR') or define('CACHE_DIR', DATA_DIR.DIRECTORY_SEPARATOR.'cache'); + // Plugins settings defined('PLUGINS_DIR') or define('PLUGINS_DIR', ROOT_DIR.DIRECTORY_SEPARATOR.'plugins'); defined('PLUGIN_API_URL') or define('PLUGIN_API_URL', 'https://kanboard.net/plugins.json'); diff --git a/tests/units/Base.php b/tests/units/Base.php index e44223ce..722a1335 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -38,6 +38,7 @@ abstract class Base extends PHPUnit_Framework_TestCase } $this->container = new Pimple\Container; + $this->container->register(new Kanboard\ServiceProvider\CacheProvider()); $this->container->register(new Kanboard\ServiceProvider\HelperProvider()); $this->container->register(new Kanboard\ServiceProvider\AuthenticationProvider()); $this->container->register(new Kanboard\ServiceProvider\DatabaseProvider()); diff --git a/tests/units/Core/Cache/FileCacheTest.php b/tests/units/Core/Cache/FileCacheTest.php new file mode 100644 index 00000000..b6336581 --- /dev/null +++ b/tests/units/Core/Cache/FileCacheTest.php @@ -0,0 +1,186 @@ +file_put_contents($filename, $data); +} + +function file_get_contents($filename) +{ + return FileCacheTest::$functions->file_get_contents($filename); +} + +function mkdir($filename, $mode = 0777, $recursif = false) +{ + return FileCacheTest::$functions->mkdir($filename, $mode, $recursif); +} + +function is_dir($filename) +{ + return FileCacheTest::$functions->is_dir($filename); +} + +function file_exists($filename) +{ + return FileCacheTest::$functions->file_exists($filename); +} + +function unlink($filename) +{ + return FileCacheTest::$functions->unlink($filename); +} + +class FileCacheTest extends \Base +{ + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + public static $functions; + + public function setUp() + { + parent::setup(); + + self::$functions = $this + ->getMockBuilder('stdClass') + ->setMethods(array( + 'file_put_contents', + 'file_get_contents', + 'file_exists', + 'mkdir', + 'is_dir', + 'unlink', + )) + ->getMock(); + } + + public function tearDown() + { + parent::tearDown(); + self::$functions = null; + } + + public function testSet() + { + $key = 'mykey'; + $data = 'data'; + $cache = new FileCache(); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo(CACHE_DIR) + ) + ->will($this->returnValue(false)); + + self::$functions + ->expects($this->at(1)) + ->method('mkdir') + ->with( + $this->equalTo(CACHE_DIR), + 0755 + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(2)) + ->method('file_put_contents') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key), + $this->equalTo(serialize($data)) + ) + ->will($this->returnValue(true)); + + $cache->set($key, $data); + } + + public function testGet() + { + $key = 'mykey'; + $data = 'data'; + $cache = new FileCache(); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('file_get_contents') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(serialize($data))); + + $this->assertSame($data, $cache->get($key)); + } + + public function testGetWithKeyNotFound() + { + $key = 'mykey'; + $cache = new FileCache(); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(false)); + + $this->assertNull($cache->get($key)); + } + + public function testRemoveWithKeyNotFound() + { + $key = 'mykey'; + $cache = new FileCache(); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(false)); + + self::$functions + ->expects($this->never()) + ->method('unlink'); + + $cache->remove($key); + } + + public function testRemove() + { + $key = 'mykey'; + $cache = new FileCache(); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('unlink') + ->with( + $this->equalTo(CACHE_DIR.DIRECTORY_SEPARATOR.$key) + ) + ->will($this->returnValue(true)); + + $cache->remove($key); + } +} diff --git a/tests/units/Core/FileStorageTest.php b/tests/units/Core/FileStorageTest.php deleted file mode 100644 index a3ad2448..00000000 --- a/tests/units/Core/FileStorageTest.php +++ /dev/null @@ -1,310 +0,0 @@ -file_put_contents($filename, $data); -} - -function file_get_contents($filename) -{ - return FileStorageTest::$functions->file_get_contents($filename); -} - -function mkdir($filename, $mode, $recursif) -{ - return FileStorageTest::$functions->mkdir($filename, $mode, $recursif); -} - -function is_dir($filename) -{ - return FileStorageTest::$functions->is_dir($filename); -} - -function file_exists($filename) -{ - return FileStorageTest::$functions->file_exists($filename); -} - -function unlink($filename) -{ - return FileStorageTest::$functions->unlink($filename); -} - -function readfile($filename) -{ - echo FileStorageTest::$functions->readfile($filename); -} - -function rename($src, $dst) -{ - return FileStorageTest::$functions->rename($src, $dst); -} - -function move_uploaded_file($src, $dst) -{ - return FileStorageTest::$functions->move_uploaded_file($src, $dst); -} - -class FileStorageTest extends \Base -{ - public static $functions; - - public function setUp() - { - parent::setup(); - - self::$functions = $this - ->getMockBuilder('stdClass') - ->setMethods(array( - 'file_put_contents', - 'file_get_contents', - 'file_exists', - 'mkdir', - 'is_dir', - 'unlink', - 'rename', - 'move_uploaded_file', - 'readfile', - )) - ->getMock(); - } - - public function tearDown() - { - parent::tearDown(); - self::$functions = null; - } - - public function testPut() - { - $data = 'data'; - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('is_dir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(false)); - - self::$functions - ->expects($this->at(1)) - ->method('mkdir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(2)) - ->method('file_put_contents') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey'), - $this->equalTo('data') - ) - ->will($this->returnValue(true)); - - $storage->put('mykey', $data); - } - - public function testPutWithSubfolder() - { - $data = 'data'; - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('is_dir') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my') - ) - ->will($this->returnValue(false)); - - self::$functions - ->expects($this->at(1)) - ->method('mkdir') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(2)) - ->method('file_put_contents') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my'.DIRECTORY_SEPARATOR.'key'), - $this->equalTo('data') - ) - ->will($this->returnValue(true)); - - $storage->put('my'.DIRECTORY_SEPARATOR.'key', $data); - } - - /** - * @expectedException \Kanboard\Core\ObjectStorage\ObjectStorageException - */ - public function testPutWhenNotAbleToCreateFolder() - { - $data = 'data'; - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('is_dir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(false)); - - self::$functions - ->expects($this->at(1)) - ->method('mkdir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(false)); - - $storage->put('mykey', $data); - } - - public function testGet() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('file_exists') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(1)) - ->method('file_get_contents') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue('data')); - - $this->assertEquals('data', $storage->get('mykey')); - } - - /** - * @expectedException \Kanboard\Core\ObjectStorage\ObjectStorageException - */ - public function testGetWithFileNotFound() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('file_exists') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(false)); - - $this->assertEquals('data', $storage->get('mykey')); - } - - public function testOutput() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('file_exists') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(1)) - ->method('readfile') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue('data')); - - $this->expectOutputString('data'); - $storage->output('mykey'); - } - - public function testRemove() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('file_exists') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(1)) - ->method('unlink') - ->with( - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - $this->assertTrue($storage->remove('mykey')); - } - - public function testMoveFile() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('is_dir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(1)) - ->method('rename') - ->with( - $this->equalTo('src_file'), - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - $this->assertTrue($storage->moveFile('src_file', 'mykey')); - } - - public function testMoveUploadedFile() - { - $storage = new FileStorage('somewhere'); - - self::$functions - ->expects($this->at(0)) - ->method('is_dir') - ->with( - $this->equalTo('somewhere') - ) - ->will($this->returnValue(true)); - - self::$functions - ->expects($this->at(1)) - ->method('move_uploaded_file') - ->with( - $this->equalTo('src_file'), - $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') - ) - ->will($this->returnValue(true)); - - $this->assertTrue($storage->moveUploadedFile('src_file', 'mykey')); - } -} diff --git a/tests/units/Core/ObjectStorage/FileStorageTest.php b/tests/units/Core/ObjectStorage/FileStorageTest.php new file mode 100644 index 00000000..ed77dedd --- /dev/null +++ b/tests/units/Core/ObjectStorage/FileStorageTest.php @@ -0,0 +1,310 @@ +file_put_contents($filename, $data); +} + +function file_get_contents($filename) +{ + return FileStorageTest::$functions->file_get_contents($filename); +} + +function mkdir($filename, $mode, $recursif) +{ + return FileStorageTest::$functions->mkdir($filename, $mode, $recursif); +} + +function is_dir($filename) +{ + return FileStorageTest::$functions->is_dir($filename); +} + +function file_exists($filename) +{ + return FileStorageTest::$functions->file_exists($filename); +} + +function unlink($filename) +{ + return FileStorageTest::$functions->unlink($filename); +} + +function readfile($filename) +{ + echo FileStorageTest::$functions->readfile($filename); +} + +function rename($src, $dst) +{ + return FileStorageTest::$functions->rename($src, $dst); +} + +function move_uploaded_file($src, $dst) +{ + return FileStorageTest::$functions->move_uploaded_file($src, $dst); +} + +class FileStorageTest extends \Base +{ + public static $functions; + + public function setUp() + { + parent::setup(); + + self::$functions = $this + ->getMockBuilder('stdClass') + ->setMethods(array( + 'file_put_contents', + 'file_get_contents', + 'file_exists', + 'mkdir', + 'is_dir', + 'unlink', + 'rename', + 'move_uploaded_file', + 'readfile', + )) + ->getMock(); + } + + public function tearDown() + { + parent::tearDown(); + self::$functions = null; + } + + public function testPut() + { + $data = 'data'; + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(false)); + + self::$functions + ->expects($this->at(1)) + ->method('mkdir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(2)) + ->method('file_put_contents') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey'), + $this->equalTo($data) + ) + ->will($this->returnValue(true)); + + $storage->put('mykey', $data); + } + + public function testPutWithSubfolder() + { + $data = 'data'; + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my') + ) + ->will($this->returnValue(false)); + + self::$functions + ->expects($this->at(1)) + ->method('mkdir') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(2)) + ->method('file_put_contents') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my'.DIRECTORY_SEPARATOR.'key'), + $this->equalTo('data') + ) + ->will($this->returnValue(true)); + + $storage->put('my'.DIRECTORY_SEPARATOR.'key', $data); + } + + /** + * @expectedException \Kanboard\Core\ObjectStorage\ObjectStorageException + */ + public function testPutWhenNotAbleToCreateFolder() + { + $data = 'data'; + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(false)); + + self::$functions + ->expects($this->at(1)) + ->method('mkdir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(false)); + + $storage->put('mykey', $data); + } + + public function testGet() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('file_get_contents') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue('data')); + + $this->assertEquals('data', $storage->get('mykey')); + } + + /** + * @expectedException \Kanboard\Core\ObjectStorage\ObjectStorageException + */ + public function testGetWithFileNotFound() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(false)); + + $this->assertEquals('data', $storage->get('mykey')); + } + + public function testOutput() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('readfile') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue('data')); + + $this->expectOutputString('data'); + $storage->output('mykey'); + } + + public function testRemove() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('file_exists') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('unlink') + ->with( + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + $this->assertTrue($storage->remove('mykey')); + } + + public function testMoveFile() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('rename') + ->with( + $this->equalTo('src_file'), + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + $this->assertTrue($storage->moveFile('src_file', 'mykey')); + } + + public function testMoveUploadedFile() + { + $storage = new FileStorage('somewhere'); + + self::$functions + ->expects($this->at(0)) + ->method('is_dir') + ->with( + $this->equalTo('somewhere') + ) + ->will($this->returnValue(true)); + + self::$functions + ->expects($this->at(1)) + ->method('move_uploaded_file') + ->with( + $this->equalTo('src_file'), + $this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey') + ) + ->will($this->returnValue(true)); + + $this->assertTrue($storage->moveUploadedFile('src_file', 'mykey')); + } +} -- cgit v1.2.3