diff options
Diffstat (limited to 'tests/units/Core')
-rw-r--r-- | tests/units/Core/Cache/FileCacheTest.php | 186 | ||||
-rw-r--r-- | tests/units/Core/ObjectStorage/FileStorageTest.php (renamed from tests/units/Core/FileStorageTest.php) | 4 | ||||
-rw-r--r-- | tests/units/Core/Plugin/HookTest.php | 62 | ||||
-rw-r--r-- | tests/units/Core/User/UserSessionTest.php | 21 |
4 files changed, 226 insertions, 47 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); + } +} 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)); diff --git a/tests/units/Core/Plugin/HookTest.php b/tests/units/Core/Plugin/HookTest.php index d1c139b3..acadede0 100644 --- a/tests/units/Core/Plugin/HookTest.php +++ b/tests/units/Core/Plugin/HookTest.php @@ -8,89 +8,103 @@ class HookTest extends Base { public function testGetListeners() { - $h = new Hook; - $this->assertEmpty($h->getListeners('myhook')); + $hook = new Hook; + $this->assertEmpty($hook->getListeners('myhook')); - $h->on('myhook', 'A'); - $h->on('myhook', 'B'); + $hook->on('myhook', 'A'); + $hook->on('myhook', 'B'); - $this->assertEquals(array('A', 'B'), $h->getListeners('myhook')); + $this->assertEquals(array('A', 'B'), $hook->getListeners('myhook')); } public function testExists() { - $h = new Hook; - $this->assertFalse($h->exists('myhook')); + $hook = new Hook; + $this->assertFalse($hook->exists('myhook')); - $h->on('myhook', 'A'); + $hook->on('myhook', 'A'); - $this->assertTrue($h->exists('myhook')); + $this->assertTrue($hook->exists('myhook')); } public function testMergeWithNoBinding() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); - $result = $h->merge('myhook', $values, array('p' => 'c')); + $result = $hook->merge('myhook', $values, array('p' => 'c')); $this->assertEquals($values, $result); } public function testMergeWithBindings() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); $expected = array('A', 'B', 'c', 'D'); - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return array($p); }); - $h->on('myhook', function () { + $hook->on('myhook', function () { return array('D'); }); - $result = $h->merge('myhook', $values, array('p' => 'c')); + $result = $hook->merge('myhook', $values, array('p' => 'c')); $this->assertEquals($expected, $result); $this->assertEquals($expected, $values); } public function testMergeWithBindingButReturningBadData() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); $expected = array('A', 'B'); - $h->on('myhook', function () { + $hook->on('myhook', function () { return 'string'; }); - $result = $h->merge('myhook', $values); + $result = $hook->merge('myhook', $values); $this->assertEquals($expected, $result); $this->assertEquals($expected, $values); } public function testFirstWithNoBinding() { - $h = new Hook; + $hook = new Hook; - $result = $h->first('myhook', array('p' => 2)); + $result = $hook->first('myhook', array('p' => 2)); $this->assertEquals(null, $result); } public function testFirstWithMultipleBindings() { - $h = new Hook; + $hook = new Hook; - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return $p + 1; }); - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return $p; }); - $result = $h->first('myhook', array('p' => 3)); + $result = $hook->first('myhook', array('p' => 3)); $this->assertEquals(4, $result); } + + public function testHookWithReference() + { + $hook = new Hook(); + + $hook->on('myhook', function (&$p) { + $p = 2; + }); + + $param = 123; + $result = $hook->reference('myhook', $param); + $this->assertSame(2, $result); + $this->assertSame(2, $param); + } } diff --git a/tests/units/Core/User/UserSessionTest.php b/tests/units/Core/User/UserSessionTest.php index 64413f98..2a118079 100644 --- a/tests/units/Core/User/UserSessionTest.php +++ b/tests/units/Core/User/UserSessionTest.php @@ -83,27 +83,6 @@ class UserSessionTest extends Base $this->assertFalse($us->isAdmin()); } - public function testCommentSorting() - { - $us = new UserSession($this->container); - $this->assertEquals('ASC', $us->getCommentSorting()); - - $us->setCommentSorting('DESC'); - $this->assertEquals('DESC', $us->getCommentSorting()); - } - - public function testBoardCollapseMode() - { - $us = new UserSession($this->container); - $this->assertFalse($us->isBoardCollapsed(2)); - - $us->setBoardDisplayMode(3, false); - $this->assertFalse($us->isBoardCollapsed(3)); - - $us->setBoardDisplayMode(3, true); - $this->assertTrue($us->isBoardCollapsed(3)); - } - public function testFilters() { $us = new UserSession($this->container); |