Complete the cached list query.
authorGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 05:21:52 +0000 (06:21 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 07:54:27 +0000 (08:54 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Base.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Query/Decorator/Cache.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Query.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/Decorator/CacheTest.php [new file with mode: 0644]

index 15d5517..4e3d67e 100644 (file)
@@ -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;
     }
 
     /**
index 86093d5..4d34266 100644 (file)
@@ -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);
         }
index 1d52dfb..3fc2c87 100644 (file)
 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
index 2b32b03..d386cad 100644 (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
index 7ef9a71..43eb2b9 100644 (file)
@@ -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');
index 1ad9366..ec7e664 100644 (file)
@@ -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 (file)
index 0000000..22e1265
--- /dev/null
@@ -0,0 +1,97 @@
+<?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'));
+    }
+
+}