From fe57edd9e87832dbd14ea8ffd2dc2f16ac1ceb6f Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 20 Sep 2015 12:38:35 -0400 Subject: [PATCH] Add abstract cache layer --- ChangeLog | 2 + app/Core/Cache.php | 58 ------- app/Core/Cache/Base.php | 38 +++++ app/Core/Cache/CacheInterface.php | 45 ++++++ app/Core/Cache/MemoryCache.php | 65 ++++++++ app/Core/MemoryCache.php | 32 ---- app/Helper/User.php | 4 +- app/ServiceProvider/ClassProvider.php | 4 +- tests/units/Core/Cache/MemoryCacheTest.php | 62 ++++++++ tests/units/Helper/UserHelperTest.php | 166 +++++++++++++++++++++ 10 files changed, 383 insertions(+), 93 deletions(-) delete mode 100644 app/Core/Cache.php create mode 100644 app/Core/Cache/Base.php create mode 100644 app/Core/Cache/CacheInterface.php create mode 100644 app/Core/Cache/MemoryCache.php delete mode 100644 app/Core/MemoryCache.php create mode 100644 tests/units/Core/Cache/MemoryCacheTest.php diff --git a/ChangeLog b/ChangeLog index e0cbd5ed1..4d0140db4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,8 @@ Improvements: * Remove unnecessary margin for calendar header * Show localized documentation if available * Add event subtask.delete +* Add abstract storage layer +* Add abstract cache layer Bug fixes: diff --git a/app/Core/Cache.php b/app/Core/Cache.php deleted file mode 100644 index 670a76e0c..000000000 --- a/app/Core/Cache.php +++ /dev/null @@ -1,58 +0,0 @@ -container = $container; - $this->init(); - } - - /** - * Proxy cache - * - * Note: Arguments must be scalar types - * - * @access public - * @param string $container Container name - * @param string $method Container method - * @return mixed - */ - public function proxy($container, $method) - { - $args = func_get_args(); - $key = 'proxy_'.implode('_', $args); - $result = $this->get($key); - - if ($result === null) { - $result = call_user_func_array(array($this->container[$container], $method), array_splice($args, 2)); - $this->set($key, $result); - } - - return $result; - } -} diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/Base.php new file mode 100644 index 000000000..a16c12f09 --- /dev/null +++ b/app/Core/Cache/Base.php @@ -0,0 +1,38 @@ +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 new file mode 100644 index 000000000..8675ef8eb --- /dev/null +++ b/app/Core/Cache/CacheInterface.php @@ -0,0 +1,45 @@ +storage[$key] = $value; + } + + /** + * Fetch value from cache + * + * @access public + * @param string $key + * @return mixed Null when not found, cached value otherwise + */ + public function get($key) + { + return isset($this->storage[$key]) ? $this->storage[$key] : null; + } + + /** + * Clear all cache + * + * @access public + */ + public function flush() + { + $this->storage = array(); + } + + /** + * Remove cached value + * + * @access public + * @param string $key + */ + public function remove($key) + { + unset($this->storage[$key]); + } +} diff --git a/app/Core/MemoryCache.php b/app/Core/MemoryCache.php deleted file mode 100644 index f80a66efe..000000000 --- a/app/Core/MemoryCache.php +++ /dev/null @@ -1,32 +0,0 @@ -storage[$key] = $value; - } - - public function get($key) - { - return isset($this->storage[$key]) ? $this->storage[$key] : null; - } - - public function flush() - { - $this->storage = array(); - } - - public function remove($key) - { - unset($this->storage[$key]); - } -} diff --git a/app/Helper/User.php b/app/Helper/User.php index cb596fb03..3108a79ab 100644 --- a/app/Helper/User.php +++ b/app/Helper/User.php @@ -99,7 +99,7 @@ class User extends \Core\Base return true; } - return $this->memoryCache->proxy('acl', 'handleProjectAdminPermissions', $project_id); + return $this->memoryCache->proxy($this->container['acl'], 'handleProjectAdminPermissions', $project_id); } /** @@ -114,7 +114,7 @@ class User extends \Core\Base return true; } - return $this->memoryCache->proxy('acl', 'handleProjectManagerPermissions', $project_id); + return $this->memoryCache->proxy($this->container['acl'], 'handleProjectManagerPermissions', $project_id); } /** diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index a5677948d..899574e9b 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -72,12 +72,14 @@ class ClassProvider implements ServiceProviderInterface 'Helper', 'HttpClient', 'Lexer', - 'MemoryCache', 'Request', 'Router', 'Session', 'Template', ), + 'Core\Cache' => array( + 'MemoryCache', + ), 'Integration' => array( 'BitbucketWebhook', 'GithubWebhook', diff --git a/tests/units/Core/Cache/MemoryCacheTest.php b/tests/units/Core/Cache/MemoryCacheTest.php new file mode 100644 index 000000000..b28604ed5 --- /dev/null +++ b/tests/units/Core/Cache/MemoryCacheTest.php @@ -0,0 +1,62 @@ +assertEquals(null, $c->get('mykey')); + } + + public function testSetValue() + { + $c = new MemoryCache; + $c->set('mykey', 'myvalue'); + $this->assertEquals('myvalue', $c->get('mykey')); + } + + public function testRemoveValue() + { + $c = new MemoryCache; + $c->set('mykey', 'myvalue'); + $c->remove('mykey'); + $this->assertEquals(null, $c->get('mykey')); + } + + public function testFlushAll() + { + $c = new MemoryCache; + $c->set('mykey', 'myvalue'); + $c->flush(); + $this->assertEquals(null, $c->get('mykey')); + } + + public function testProxy() + { + $c = new MemoryCache; + + $class = $this + ->getMockBuilder('stdClass') + ->setMethods(array('doSomething')) + ->getMock(); + + $class + ->expects($this->once()) + ->method('doSomething') + ->with( + $this->equalTo(1), + $this->equalTo(2) + ) + ->will($this->returnValue(3)); + + // First call will store the computed value + $this->assertEquals(3, $c->proxy($class, 'doSomething', 1, 2)); + + // Second call get directly the cached value + $this->assertEquals(3, $c->proxy($class, 'doSomething', 1, 2)); + } +} diff --git a/tests/units/Helper/UserHelperTest.php b/tests/units/Helper/UserHelperTest.php index 947f606a5..24f2b3b21 100644 --- a/tests/units/Helper/UserHelperTest.php +++ b/tests/units/Helper/UserHelperTest.php @@ -3,6 +3,10 @@ require_once __DIR__.'/../Base.php'; use Helper\User; +use Model\Project; +use Model\ProjectPermission; +use Model\User as UserModel; +use Core\Session; class UserHelperTest extends Base { @@ -13,4 +17,166 @@ class UserHelperTest extends Base $this->assertEquals('CN', $h->getInitials('chuck norris')); $this->assertEquals('A', $h->getInitials('admin')); } + + public function testIsProjectAdministrationAllowedForProjectAdmin() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project manager + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertFalse($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => true, + ); + + $this->assertTrue($h->isProjectAdministrationAllowed(1)); + } + + public function testIsProjectAdministrationAllowedForProjectMember() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project member + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertFalse($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => false, + ); + + $this->assertFalse($h->isProjectAdministrationAllowed(1)); + } + + public function testIsProjectAdministrationAllowedForProjectManager() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project member + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addManager(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertTrue($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => false, + ); + + $this->assertFalse($h->isProjectAdministrationAllowed(1)); + } + + public function testIsProjectManagementAllowedForProjectAdmin() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project manager + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertFalse($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => true, + ); + + $this->assertTrue($h->isProjectManagementAllowed(1)); + } + + public function testIsProjectManagementAllowedForProjectMember() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project member + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertFalse($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => false, + ); + + $this->assertFalse($h->isProjectManagementAllowed(1)); + } + + public function testIsProjectManagementAllowedForProjectManager() + { + $h = new User($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new UserModel($this->container); + $session = new Session; + + // We create our user + $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); + + // We create a project and set our user as project member + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($pp->addManager(1, 2)); + $this->assertTrue($pp->isMember(1, 2)); + $this->assertTrue($pp->isManager(1, 2)); + + // We fake a session for him + $session['user'] = array( + 'id' => 2, + 'is_admin' => false, + 'is_project_admin' => false, + ); + + $this->assertTrue($h->isProjectManagementAllowed(1)); + } }