}
/**
+ * Return the ID of the underlying connection.
+ *
+ * @return string The connection ID.
+ */
+ public function getConnectionId()
+ {
+ return $this->_list->getConnectionId();
+ }
+
+ /**
+ * Check if the cache has been initialized at all and synchronize it if not.
+ *
+ * @return NULL
+ */
+ private function _init()
+ {
+ }
+
+ /**
* Returns the list of folders visible to the current user.
*
* @return array The list of folders, represented as a list of strings.
*/
public function listFolders()
{
- $result = $this->_list->listFolders();
- return $result;
+ $this->_init();
+ $a = $this->_cache->loadListData(
+ $this->_list->getConnectionId(),
+ 'FOLDERS'
+ );
+ if (empty($a)) {
+ $a = $this->_cacheFolders();
+ }
+ return $a;
+ }
+
+ /**
+ * Caches and returns the list of folders visible to the current user.
+ *
+ * @return array The list of folders, represented as a list of strings.
+ */
+ private function _cacheFolders()
+ {
+ $list = $this->_list->listFolders();
+ $this->_cache->storeListData(
+ $this->_list->getConnectionId(),
+ 'FOLDERS',
+ $list
+ );
+ return $list;
}
/**
}
/**
+ * Synchronize the list information with the information from the backend.
+ *
+ * @return NULL
+ */
+ public function synchronize()
+ {
+ $this->_cacheFolders();
+ }
+
+ /**
* Return the specified query type.
*
* @param string $name The query name.
--- /dev/null
+<?php
+/**
+ * Test the folder list cache decorator.
+ *
+ * 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 folder list cache decorator.
+ *
+ * Copyright 2010 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_Decorator_CacheTest
+extends Horde_Kolab_Storage_TestCase
+{
+ public function testListFolderIsArray()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getNullList(),
+ $this->getMockCache()
+ );
+ $this->assertType('array', $list->listFolders());
+ }
+
+ public function testListFolder()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getTwoFolderList(),
+ $this->getMockCache()
+ );
+ $this->assertEquals(
+ array('INBOX', 'INBOX/a'),
+ $list->listFolders()
+ );
+ }
+
+ public function testLongerList()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getAnnotatedList(),
+ $this->getMockCache()
+ );
+ $this->assertEquals(
+ array('INBOX', 'INBOX/a', 'INBOX/Calendar', 'INBOX/Contacts', 'INBOX/Notes', 'INBOX/Tasks'),
+ $list->listFolders()
+ );
+ }
+
+ public function testMockedList()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getMockDriverList(),
+ $this->getMockCache()
+ );
+ $this->mockDriver->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('INBOX')));
+ $this->assertEquals(
+ array('INBOX'),
+ $list->listFolders()
+ );
+ }
+
+ public function testCachedList()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getMockDriverList(),
+ $this->getMockCache()
+ );
+ $this->mockDriver->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('INBOX')));
+ $list->listFolders();
+ $this->assertEquals(
+ array('INBOX'),
+ $list->listFolders()
+ );
+ }
+
+ public function testTwoCachedLists()
+ {
+ $cache = $this->getMockCache();
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getMockDriverList(),
+ $cache
+ );
+ $this->mockDriver->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('INBOX')));
+ $this->mockDriver->expects($this->any())
+ ->method('getId')
+ ->will($this->returnValue(array('A')));
+ $mockDriver2 = $this->getMock('Horde_Kolab_Storage_Driver');
+ $list2 = new Horde_Kolab_Storage_List_Decorator_Cache(
+ new Horde_Kolab_Storage_List_Base(
+ $mockDriver2,
+ new Horde_Kolab_Storage_Factory()
+ ),
+ $cache
+ );
+ $mockDriver2->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('NOTHING')));
+ $this->mockDriver->expects($this->any())
+ ->method('getId')
+ ->will($this->returnValue(array('B')));
+
+ $list->listFolders();
+ $list2->listFolders();
+ $this->assertEquals(
+ array('NOTHING'),
+ $list2->listFolders()
+ );
+ }
+
+ public function testSynchronizeFolders()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getMockDriverList(),
+ $this->getMockCache()
+ );
+ $this->mockDriver->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('INBOX')));
+ $list->synchronize();
+ }
+
+ public function testSynchronizeFolderCache()
+ {
+ $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+ $this->getMockDriverList(),
+ $this->getMockCache()
+ );
+ $this->mockDriver->expects($this->once())
+ ->method('getMailboxes')
+ ->will($this->returnValue(array('INBOX')));
+ $list->synchronize();
+ $list->listFolders();
+ }
+}