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 --- 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 +++++++++++++++++++++ 4 files changed, 497 insertions(+), 310 deletions(-) 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 (limited to 'tests') 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