diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-08-21 18:46:34 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-08-21 18:46:34 -0400 |
commit | 8e83e404fbb1d0dc770e5b41fa315a674541459a (patch) | |
tree | 38186a4ef5818133489b41f85f200fa28bed2806 /tests/units/Core/Cache | |
parent | 836e93546355e20f5a3cca0e2fd4e649264ce7ac (diff) |
Add FileCache driver
Diffstat (limited to 'tests/units/Core/Cache')
-rw-r--r-- | tests/units/Core/Cache/FileCacheTest.php | 186 |
1 files changed, 186 insertions, 0 deletions
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); + } +} |