summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-08-21 18:46:34 -0400
committerFrederic Guillot <fred@kanboard.net>2016-08-21 18:46:34 -0400
commit8e83e404fbb1d0dc770e5b41fa315a674541459a (patch)
tree38186a4ef5818133489b41f85f200fa28bed2806 /tests
parent836e93546355e20f5a3cca0e2fd4e649264ce7ac (diff)
Add FileCache driver
Diffstat (limited to 'tests')
-rw-r--r--tests/units/Base.php1
-rw-r--r--tests/units/Core/Cache/FileCacheTest.php186
-rw-r--r--tests/units/Core/ObjectStorage/FileStorageTest.php (renamed from tests/units/Core/FileStorageTest.php)4
3 files changed, 189 insertions, 2 deletions
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 @@
+<?php
+
+namespace Kanboard\Core\Cache;
+
+require_once __DIR__.'/../../Base.php';
+
+function file_put_contents($filename, $data)
+{
+ return FileCacheTest::$functions->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/ObjectStorage/FileStorageTest.php
index a3ad2448..ed77dedd 100644
--- a/tests/units/Core/FileStorageTest.php
+++ b/tests/units/Core/ObjectStorage/FileStorageTest.php
@@ -2,7 +2,7 @@
namespace Kanboard\Core\ObjectStorage;
-require_once __DIR__.'/../Base.php';
+require_once __DIR__.'/../../Base.php';
function file_put_contents($filename, $data)
{
@@ -105,7 +105,7 @@ class FileStorageTest extends \Base
->method('file_put_contents')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey'),
- $this->equalTo('data')
+ $this->equalTo($data)
)
->will($this->returnValue(true));