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)
+ );
+ }
}
/**
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;
}
/**
/**
* The queriable list.
*
- * @var Horde_Kolab_Storage_Queriable
+ * @var Horde_Kolab_Storage_List
*/
- private $_queriable;
+ private $_list;
/**
* The factory for generating additional resources.
/**
* 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;
}
/**
public function listFolderTypeAnnotations()
{
$result = array();
- $list = $this->_queriable->listFolderTypes();
+ $list = $this->_list->listFolderTypes();
foreach ($list as $folder => $annotation) {
$result[$folder] = $this->_factory->createFolderType($annotation);
}
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;
}
*/
public function listTypes()
{
+ return $this->_list_cache->getQuery(self::TYPES);
}
/**
*/
public function listFolderTypeAnnotations()
{
+ $result = array();
+ $list = $this->_list_cache->getFolderTypes();
+ foreach ($list as $folder => $annotation) {
+ $result[$folder] = $this->_factory->createFolderType($annotation);
+ }
+ return $result;
}
/**
*/
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();
+ }
}
/**
*/
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
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
);
}
+ 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');
$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(
$this->mockDriver->expects($this->never())
->method('getMailboxes')
->will($this->returnValue(array('INBOX')));
- $this->assertEmpty($list->listFolders());
+ $list->listFolders();
}
public function testInvalidVersion()
--- /dev/null
+<?php
+/**
+ * Test the cached list query.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Storage
+ * @subpackage UnitTests
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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'));
+ }
+
+}