From 6d13cec425cca118f69e659416d1dbb98188ce05 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 4 Jan 2011 06:21:52 +0100 Subject: [PATCH] Complete the cached list query. --- .../lib/Horde/Kolab/Storage/Cache/List.php | 51 +++++++++++- .../lib/Horde/Kolab/Storage/List/Query/Base.php | 12 +-- .../Kolab/Storage/List/Query/Decorator/Cache.php | 64 ++++++++++---- .../lib/Horde/Kolab/Storage/Query.php | 7 -- .../test/Horde/Kolab/Storage/TestCase.php | 17 ++++ .../Storage/Unit/List/Decorator/CacheTest.php | 10 ++- .../Unit/List/Query/Decorator/CacheTest.php | 97 ++++++++++++++++++++++ 7 files changed, 222 insertions(+), 36 deletions(-) create mode 100644 framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/Decorator/CacheTest.php diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php index 15d5517a9..4e3d67e78 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php @@ -140,8 +140,13 @@ class Horde_Kolab_Storage_Cache_List public function getFolders() { $this->_load(); - return isset($this->_data[self::FOLDERS]) ? - $this->_data[self::FOLDERS] : array(); + if (isset($this->_data[self::FOLDERS])) { + return $this->_data[self::FOLDERS]; + } else { + throw new Horde_Kolab_Storage_Exception( + sprintf('Missing cache data (Key: %s). Synchronize first!', self::FOLDERS) + ); + } } /** @@ -153,8 +158,46 @@ class Horde_Kolab_Storage_Cache_List public function getFolderTypes() { $this->_load(); - return isset($this->_data[self::TYPES]) ? - $this->_data[self::TYPES] : array(); + if (isset($this->_data[self::TYPES])) { + return $this->_data[self::TYPES]; + } else { + throw new Horde_Kolab_Storage_Exception( + sprintf('Missing cache data (Key: %s). Synchronize first!', self::TYPES) + ); + } + } + + /** + * Return query information. + * + * @param string $key The query key. + * + * @return mixed The query data. + */ + public function getQuery($key) + { + $this->_load(); + if (isset($this->_data[self::QUERIES][$key])) { + return $this->_data[self::QUERIES][$key]; + } else { + throw new Horde_Kolab_Storage_Exception( + sprintf('Missing query cache data (Key: %s). Synchronize first!', $key) + ); + } + } + + /** + * Set query information. + * + * @param string $key The query key. + * @param mixed $data The query data. + * + * @return NULL + */ + public function setQuery($key, $data) + { + $this->_load(); + $this->_data[self::QUERIES][$key] = $data; } /** diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php index 86093d56f..4d34266a9 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php @@ -31,9 +31,9 @@ implements Horde_Kolab_Storage_List_Query /** * The queriable list. * - * @var Horde_Kolab_Storage_Queriable + * @var Horde_Kolab_Storage_List */ - private $_queriable; + private $_list; /** * The factory for generating additional resources. @@ -45,11 +45,11 @@ implements Horde_Kolab_Storage_List_Query /** * Constructor. * - * @param Horde_Kolab_Storage_Queriable $queriable The queriable list. + * @param Horde_Kolab_Storage_List $list The queriable list. */ - public function __construct(Horde_Kolab_Storage_Queriable $queriable) + public function __construct(Horde_Kolab_Storage_List $list) { - $this->_queriable = $queriable; + $this->_list = $list; } /** @@ -89,7 +89,7 @@ implements Horde_Kolab_Storage_List_Query public function listFolderTypeAnnotations() { $result = array(); - $list = $this->_queriable->listFolderTypes(); + $list = $this->_list->listFolderTypes(); foreach ($list as $folder => $annotation) { $result[$folder] = $this->_factory->createFolderType($annotation); } diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Decorator/Cache.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Decorator/Cache.php index 1d52dfb2f..3fc2c87df 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Decorator/Cache.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Decorator/Cache.php @@ -28,39 +28,47 @@ class Horde_Kolab_Storage_List_Query_Decorator_Cache implements Horde_Kolab_Storage_List_Query { + /** The folder type list */ + const TYPES = 'TYPES'; + + /** The folder list sorted by type */ + const BY_TYPE = 'BY_TYPE'; + /** * The queriable list. * - * @var Horde_Kolab_Storage_Queriable + * @var Horde_Kolab_Storage_List */ - private $_queriable; + private $_list; /** - * The factory for generating additional resources. + * The list cache. * - * @var Horde_Kolab_Storage_Factory + * @var Horde_Kolab_Storage_Cache_List */ - private $_factory; + private $_list_cache; /** - * Constructor. + * The factory for generating additional resources. * - * @param Horde_Kolab_Storage_Queriable $queriable The queriable list. + * @var Horde_Kolab_Storage_Factory */ - public function __construct(Horde_Kolab_Storage_Queriable $queriable) - { - $this->_queriable = $queriable; - } + private $_factory; /** - * Inject the factory. - * - * @param Horde_Kolab_Storage_Factory $factory The factory. + * Constructor. * - * @return NULL + * @param Horde_Kolab_Storage_List $list The queriable list. + * @param Horde_Kolab_Storage_Factory $factory The factory. + * @param Horde_Kolab_Storage_Cache_List $list_cache The list cache. */ - public function setFactory(Horde_Kolab_Storage_Factory $factory) - { + public function __construct( + Horde_Kolab_Storage_List $list, + Horde_Kolab_Storage_Factory $factory, + Horde_Kolab_Storage_Cache_List $list_cache + ) { + $this->_list = $list; + $this->_list_cache = $list_cache; $this->_factory = $factory; } @@ -72,6 +80,7 @@ implements Horde_Kolab_Storage_List_Query */ public function listTypes() { + return $this->_list_cache->getQuery(self::TYPES); } /** @@ -82,6 +91,12 @@ implements Horde_Kolab_Storage_List_Query */ public function listFolderTypeAnnotations() { + $result = array(); + $list = $this->_list_cache->getFolderTypes(); + foreach ($list as $folder => $annotation) { + $result[$folder] = $this->_factory->createFolderType($annotation); + } + return $result; } /** @@ -93,6 +108,12 @@ implements Horde_Kolab_Storage_List_Query */ public function listByType($type) { + $by_type = $this->_list_cache->getQuery(self::BY_TYPE); + if (isset($by_type[$type])) { + return $by_type[$type]; + } else { + return array(); + } } /** @@ -102,5 +123,14 @@ implements Horde_Kolab_Storage_List_Query */ public function synchronize() { + $types = array(); + $by_type = array(); + foreach ($this->listFolderTypeAnnotations() as $folder => $annotation) { + $type = $annotation->getType(); + $types[$folder] = $type; + $by_type[$type][] = $folder; + } + $this->_list_cache->setQuery(self::TYPES, $types); + $this->_list_cache->setQuery(self::BY_TYPE, $by_type); } } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Query.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Query.php index 2b32b0316..d386cad49 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Query.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Query.php @@ -28,13 +28,6 @@ interface Horde_Kolab_Storage_Query { /** - * Constructor. - * - * @param Horde_Kolab_Storage_Queriable $queriable The queriable object. - */ - public function __construct(Horde_Kolab_Storage_Queriable $queriable); - - /** * Synchronize the query data with the information from the backend. * * @return NULL diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php index 7ef9a7193..43eb2b911 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php @@ -188,6 +188,23 @@ extends PHPUnit_Framework_TestCase ); } + protected function getCachedQueryForList($bare_list, $factory) + { + $list_cache = $this->getMockListCache(); + $list = new Horde_Kolab_Storage_List_Decorator_Cache( + $bare_list, + $list_cache + ); + $query = new Horde_Kolab_Storage_List_Query_Decorator_Cache( + $list, + $factory, + $list_cache + ); + $list->registerQuery($query); + $list->synchronize(); + return $query; + } + protected function getMockDriverList() { $this->mockDriver = $this->getMock('Horde_Kolab_Storage_Driver'); diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php index 1ad9366f1..ec7e66408 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php @@ -262,12 +262,18 @@ extends Horde_Kolab_Storage_TestCase $this->getMockListCache() ); $this->mockDriver->expects($this->once()) + ->method('getMailboxes') + ->will($this->returnValue(array('INBOX'))); + $this->mockDriver->expects($this->once()) ->method('listAnnotation') ->will($this->returnValue(array('INBOX' => 'mail.default'))); $list->listFolders(); } - public function testEmptyIfFooled() + /** + * @expectedException Horde_Kolab_Storage_Exception + */ + public function testExceptionIfFooled() { $cache = $this->getMockCache(); $list = new Horde_Kolab_Storage_List_Decorator_Cache( @@ -280,7 +286,7 @@ extends Horde_Kolab_Storage_TestCase $this->mockDriver->expects($this->never()) ->method('getMailboxes') ->will($this->returnValue(array('INBOX'))); - $this->assertEmpty($list->listFolders()); + $list->listFolders(); } public function testInvalidVersion() diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/Decorator/CacheTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/Decorator/CacheTest.php new file mode 100644 index 000000000..22e1265a4 --- /dev/null +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/Decorator/CacheTest.php @@ -0,0 +1,97 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../../../../Autoload.php'; + +/** + * Test the cached list query. + * + * Copyright 2011 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Kolab + * @package Kolab_Storage + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Storage + */ +class Horde_Kolab_Storage_Unit_List_Query_Decorator_CacheTest +extends Horde_Kolab_Storage_TestCase +{ + public function testAnotationsReturnsArray() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getNullList($factory), $factory); + $this->assertType('array', $query->listFolderTypeAnnotations()); + } + + public function testAnnotationsReturnsHandlers() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory); + foreach ($query->listFolderTypeAnnotations() as $folder => $type) { + $this->assertInstanceOf('Horde_Kolab_Storage_Folder_Type', $type); + }; + } + + public function testTypeReturnsArray() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getNullList($factory), $factory); + $this->assertType('array', $query->listTypes()); + } + + public function testTypeReturnsAnnotations() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory); + $this->assertEquals( + array( + 'INBOX/Calendar' => 'event', + 'INBOX/Contacts' => 'contact', + 'INBOX/Notes' => 'note', + 'INBOX/Tasks' => 'task', + ), + $query->listTypes() + ); + } + + public function testByTypeReturnsArray() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getNullList($factory), $factory); + $this->assertType('array', $query->listByType('test')); + } + + public function testListCalendarsListsCalendars() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory); + $this->assertEquals(array('INBOX/Calendar'), $query->listByType('event')); + } + + public function testListTasklistsListsTasklists() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $this->getCachedQueryForList($this->getAnnotatedList($factory), $factory); + $this->assertEquals(array('INBOX/Tasks'), $query->listByType('task')); + } + +} -- 2.11.0