From fdfdb26263f94b5dd6275bc3e16d064b41e986a5 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel
Date: Mon, 3 Jan 2011 13:48:17 +0100 Subject: [PATCH] Remove the query creation from the list handler. Keep only the most basic data retrieval in the list handler and move everything else in the base query. --- .../Kolab_Storage/lib/Horde/Kolab/Storage/List.php | 12 +--- .../lib/Horde/Kolab/Storage/List/Base.php | 71 ++-------------------- .../Horde/Kolab/Storage/List/Decorator/Cache.php | 33 +--------- .../lib/Horde/Kolab/Storage/List/Decorator/Log.php | 61 ++++++++----------- .../lib/Horde/Kolab/Storage/List/Query/Base.php | 53 +++++++++++++++- .../lib/Horde/Kolab/Storage/Queriable.php | 10 --- .../test/Horde/Kolab/Storage/Autoload.php | 3 + .../test/Horde/Kolab/Storage/Stub/FactoryQuery.php | 27 ++++++++ .../test/Horde/Kolab/Storage/TestCase.php | 8 +-- .../test/Horde/Kolab/Storage/Unit/FactoryTest.php | 70 +++++++++++++++++++++ .../Horde/Kolab/Storage/Unit/List/BaseTest.php | 53 +--------------- .../Kolab/Storage/Unit/List/Decorator/LogTest.php | 38 +----------- .../Kolab/Storage/Unit/List/Query/BaseTest.php | 52 +++++++++++----- 13 files changed, 231 insertions(+), 260 deletions(-) create mode 100644 framework/Kolab_Storage/test/Horde/Kolab/Storage/Stub/FactoryQuery.php diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List.php index d144a0238..e29c00204 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List.php @@ -43,18 +43,10 @@ extends Horde_Kolab_Storage_Queriable public function listFolders(); /** - * Returns the folder types as associative array. - * - * @return array The list folder types with the folder names as key and the - * type as values. - */ - public function listTypes(); - - /** * Returns the folder type annotation as associative array. * * @return array The list folder types with the folder names as key and the - * type handler as values. + * folder type as values. */ - public function listFolderTypeAnnotations(); + public function listFolderTypes(); } diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Base.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Base.php index 12e1cfeb9..793e511bc 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Base.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Base.php @@ -36,24 +36,15 @@ implements Horde_Kolab_Storage_List private $_driver; /** - * The factory for generating additional resources. - * - * @var Horde_Kolab_Storage_Factory - */ - private $_factory; - - /** * Constructor. * * @param Horde_Kolab_Storage_Driver $driver The primary connection driver. * @param Horde_Kolab_Storage_Factory $factory The factory. */ public function __construct( - Horde_Kolab_Storage_Driver $driver, - Horde_Kolab_Storage_Factory $factory + Horde_Kolab_Storage_Driver $driver ) { $this->_driver = $driver; - $this->_factory = $factory; } /** @@ -77,69 +68,15 @@ implements Horde_Kolab_Storage_List } /** - * Returns the folder types as associative array. - * - * @return array The list folder types with the folder names as key and the - * type as values. - */ - public function listTypes() - { - $result = array(); - $list = $this->listFolderTypeAnnotations(); - foreach ($list as $folder => $annotation) { - $result[$folder] = $annotation->getType(); - } - return $result; - } - - /** * Returns the folder type annotation as associative array. * * @return array The list folder types with the folder names as key and the - * type handler as values. + * folder type as values. */ - public function listFolderTypeAnnotations() + public function listFolderTypes() { - $result = array(); - $list = $this->_driver->listAnnotation( + return $this->_driver->listAnnotation( '/shared/vendor/kolab/folder-type' ); - foreach ($list as $folder => $annotation) { - $result[$folder] = $this->_factory->createFolderType($annotation); - } - return $result; - } - - /** - * Return the specified query type. - * - * @param string $name The query name. - * - * @return Horde_Kolab_Storage_Query A query handler. - * - * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported. - */ - public function getQuery($name) - { - return $this->getQueryWithParent($name, $this); - } - - /** - * Return the specified query type based on the specified parent object. - * - * @param string $name The query name. - * @param Horde_Kolab_Storage_List $parent The base for the query object. - * - * @return Horde_Kolab_Storage_Query A query handler. - * - * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported. - */ - public function getQueryWithParent($name, Horde_Kolab_Storage_List $parent) - { - $class = 'Horde_Kolab_Storage_List_Query_' . $name; - if (class_exists($class)) { - return new $class($parent); - } - throw new Horde_Kolab_Storage_Exception(sprintf('No such query "%s"!', $name)); } } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php index 756cd816d..5aa6b99bc 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php @@ -111,26 +111,14 @@ implements Horde_Kolab_Storage_List } /** - * Returns the folder types as associative array. - * - * @return array The list folder types with the folder names as key and the - * type as values. - */ - public function listTypes() - { - $result = $this->_list->listTypes(); - return $result; - } - - /** * Returns the folder type annotation as associative array. * * @return array The list folder types with the folder names as key and the - * type handler as values. + * folder type as values. */ - public function listFolderTypeAnnotations() + public function listFolderTypes() { - $result = $this->_list->listFolderTypeAnnotations(); + $result = $this->_list->listFolderTypes(); return $result; } @@ -143,19 +131,4 @@ implements Horde_Kolab_Storage_List { $this->_cacheFolders(); } - - /** - * Return the specified query type. - * - * @param string $name The query name. - * - * @return Horde_Kolab_Storage_Query A query handler. - * - * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported. - */ - public function getQuery($name) - { - return $this->_list->getQueryWithParent($name, $this); - } - } \ No newline at end of file diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Log.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Log.php index 4bf62b731..d2816ea4f 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Log.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Log.php @@ -74,26 +74,19 @@ implements Horde_Kolab_Storage_List */ public function listFolders() { - $this->_logger->info('Listing folders.'); - $result = $this->_list->listFolders(); $this->_logger->info( - sprintf('List contained %s folders.', count($result)) + sprintf( + 'Listing folders for %s.', + $this->getConnectionId() + ) ); - return $result; - } - - /** - * Returns the folder types as associative array. - * - * @return array The list folder types with the folder names as key and the - * type as values. - */ - public function listTypes() - { - $this->_logger->info('Listing folder types.'); - $result = $this->_list->listTypes(); + $result = $this->_list->listFolders(); $this->_logger->info( - sprintf('List contained %s folders and types.', count($result)) + sprintf( + 'List for %s contained %s folders.', + $this->getConnectionId(), + count($result) + ) ); return $result; } @@ -102,30 +95,24 @@ implements Horde_Kolab_Storage_List * Returns the folder type annotation as associative array. * * @return array The list folder types with the folder names as key and the - * type handler as values. + * folder type as values. */ - public function listFolderTypeAnnotations() + public function listFolderTypes() { - $this->_logger->info('Listing folder type annotations.'); - $result = $this->_list->listFolderTypeAnnotations(); $this->_logger->info( - sprintf('List contained %s folders and annotations.', count($result)) + sprintf( + 'Listing folder type annotations for %s.', + $this->getConnectionId() + ) + ); + $result = $this->_list->listFolderTypes(); + $this->_logger->info( + sprintf( + 'List for %s contained %s folders and annotations.', + $this->getConnectionId(), + count($result) + ) ); return $result; } - - /** - * Return the specified query type. - * - * @param string $name The query name. - * - * @return Horde_Kolab_Storage_Query A query handler. - * - * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported. - */ - public function getQuery($name) - { - return $this->_list->getQueryWithParent($name, $this); - } - } \ No newline at end of file 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 82125a94b..f7b44eece 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 @@ -36,6 +36,13 @@ implements Horde_Kolab_Storage_Query private $_queriable; /** + * The factory for generating additional resources. + * + * @var Horde_Kolab_Storage_Factory + */ + private $_factory; + + /** * Constructor. * * @param Horde_Kolab_Storage_Queriable $queriable The queriable list. @@ -46,6 +53,50 @@ implements Horde_Kolab_Storage_Query } /** + * Inject the factory. + * + * @param Horde_Kolab_Storage_Factory $factory The factory. + * + * @return NULL + */ + public function setFactory(Horde_Kolab_Storage_Factory $factory) + { + $this->_factory = $factory; + } + + /** + * Returns the folder types as associative array. + * + * @return array The list folder types with the folder names as key and the + * type as values. + */ + public function listTypes() + { + $result = array(); + $list = $this->listFolderTypeAnnotations(); + foreach ($list as $folder => $annotation) { + $result[$folder] = $annotation->getType(); + } + return $result; + } + + /** + * Returns the folder type annotation as associative array. + * + * @return array The list folder types with the folder names as key and the + * type handler as values. + */ + public function listFolderTypeAnnotations() + { + $result = array(); + $list = $this->_queriable->listFolderTypes(); + foreach ($list as $folder => $annotation) { + $result[$folder] = $this->_factory->createFolderType($annotation); + } + return $result; + } + + /** * List all folders of a specific type. * * @param string $type The folder type the listing should be limited to. @@ -55,7 +106,7 @@ implements Horde_Kolab_Storage_Query public function listByType($type) { $result = array(); - foreach ($this->_queriable->listTypes() as $folder => $folder_type) { + foreach ($this->listTypes() as $folder => $folder_type) { if ($folder_type == $type) { $result[] = $folder; } diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Queriable.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Queriable.php index 4cf5d450c..239b56d39 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Queriable.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Queriable.php @@ -27,15 +27,5 @@ */ interface Horde_Kolab_Storage_Queriable { - /** - * Return the specified query type. - * - * @param string $name The query name. - * - * @return Horde_Kolab_Storage_Query A query handler. - * - * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported. - */ - public function getQuery($name); } diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Autoload.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Autoload.php index 3ec0be20f..6a9e9ed6c 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Autoload.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Autoload.php @@ -19,3 +19,6 @@ error_reporting(E_ALL | E_STRICT); /** Load the basic test definition */ require_once dirname(__FILE__) . '/TestCase.php'; + +/** Load stub definitions */ +require_once dirname(__FILE__) . '/Stub/FactoryQuery.php'; diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Stub/FactoryQuery.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Stub/FactoryQuery.php new file mode 100644 index 000000000..8c3d829bf --- /dev/null +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Stub/FactoryQuery.php @@ -0,0 +1,27 @@ +called = true; + } +} \ No newline at end of file diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php index 5f6142a9a..04a1f63ce 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php @@ -46,9 +46,9 @@ extends PHPUnit_Framework_TestCase ); } - protected function getNullList() + protected function getNullList($factory = null) { - $factory = new Horde_Kolab_Storage_Factory(); + $factory = $this->completeFactory($factory); return new Horde_Kolab_Storage_List_Base( $this->getNullMock($factory), $factory @@ -140,9 +140,9 @@ extends PHPUnit_Framework_TestCase ); } - protected function getAnnotatedList() + protected function getAnnotatedList($factory = null) { - $factory = new Horde_Kolab_Storage_Factory(); + $factory = $this->completeFactory($factory); return new Horde_Kolab_Storage_List_Base( $this->getAnnotatedMock($factory), $factory diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/FactoryTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/FactoryTest.php index ecbb3fddc..2e10c8098 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/FactoryTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/FactoryTest.php @@ -162,4 +162,74 @@ extends Horde_Kolab_Storage_TestCase ) ); } + + /** + * @expectedException Horde_Kolab_Storage_Exception + */ + public function testCreateQueryForUnsupported() + { + $list = new Horde_Kolab_Storage_List_Base( + $this->getNullMock(), + new Horde_Kolab_Storage_Factory() + ); + $factory = new Horde_Kolab_Storage_Factory(); + $factory->createListQuery('NO_SUCH_QUERY', $list); + } + + public function testQueryReturnsQuery() + { + $list = new Horde_Kolab_Storage_List_Base( + $this->getNullMock(), + new Horde_Kolab_Storage_Factory() + ); + $factory = new Horde_Kolab_Storage_Factory(); + $this->assertInstanceOf( + 'Horde_Kolab_Storage_Query', + $factory->createListQuery('Base', $list) + ); + } + + public function testFactoryInjection() + { + $list = new Horde_Kolab_Storage_List_Base( + $this->getNullMock(), + new Horde_Kolab_Storage_Factory() + ); + $factory = new Horde_Kolab_Storage_Factory(); + $this->assertInstanceOf( + 'Horde_Kolab_Storage_Query', + $factory->createListQuery('Base', $list) + ); + } + + public function testQueryStub() + { + $list = new Horde_Kolab_Storage_List_Base( + $this->getNullMock(), + new Horde_Kolab_Storage_Factory() + ); + $factory = new Horde_Kolab_Storage_Factory(); + $this->assertInstanceOf( + 'Horde_Kolab_Storage_Query', + $factory->createListQuery( + 'Horde_Kolab_Storage_Stub_FactoryQuery', + $list + ) + ); + } + + public function testQueryFactoryInjection() + { + $list = new Horde_Kolab_Storage_List_Base( + $this->getNullMock(), + new Horde_Kolab_Storage_Factory() + ); + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery( + 'Horde_Kolab_Storage_Stub_FactoryQuery', + $list + ); + $this->assertTrue($query->called); + } + } diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/BaseTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/BaseTest.php index efc9a4d8d..1980707b1 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/BaseTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/BaseTest.php @@ -56,41 +56,13 @@ extends Horde_Kolab_Storage_TestCase ); } - public function testTypeReturnsArray() + public function testListFolderTypesReturnsArray() { $list = new Horde_Kolab_Storage_List_Base( $this->getNullMock(), new Horde_Kolab_Storage_Factory() ); - $this->assertType('array', $list->listTypes()); - } - - public function testTypeReturnsAnnotations() - { - $list = new Horde_Kolab_Storage_List_Base( - $this->getAnnotatedMock(), - new Horde_Kolab_Storage_Factory() - ); - $this->assertEquals( - array( - 'INBOX/Calendar' => 'event', - 'INBOX/Contacts' => 'contact', - 'INBOX/Notes' => 'note', - 'INBOX/Tasks' => 'task', - ), - $list->listTypes() - ); - } - - public function testAnnotationsReturnsHandlers() - { - $list = new Horde_Kolab_Storage_List_Base( - $this->getAnnotatedMock(), - new Horde_Kolab_Storage_Factory() - ); - foreach ($list->listFolderTypeAnnotations() as $folder => $type) { - $this->assertInstanceOf('Horde_Kolab_Storage_Folder_Type', $type); - }; + $this->assertType('array', $list->listFolderTypes()); } public function testListQueriable() @@ -101,25 +73,4 @@ extends Horde_Kolab_Storage_TestCase ); $this->assertTrue($list instanceOf Horde_Kolab_Storage_Queriable); } - - /** - * @expectedException Horde_Kolab_Storage_Exception - */ - public function testGetQueryForUnsupported() - { - $list = new Horde_Kolab_Storage_List_Base( - $this->getNullMock(), - new Horde_Kolab_Storage_Factory() - ); - $list->getQuery('NO_SUCH_QUERY'); - } - - public function testQueryReturnsQuery() - { - $list = new Horde_Kolab_Storage_List_Base( - $this->getNullMock(), - new Horde_Kolab_Storage_Factory() - ); - $this->assertInstanceOf('Horde_Kolab_Storage_Query', $list->getQuery('Base')); - } } diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/LogTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/LogTest.php index d41762a8d..8b21d993e 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/LogTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/LogTest.php @@ -52,27 +52,7 @@ extends Horde_Kolab_Storage_TestCase $this->getMockLogger() ); $list->listFolders(); - $this->assertLogContains('List contained 2 folders.'); - } - - public function testListTypesFolderCount() - { - $list = new Horde_Kolab_Storage_List_Decorator_Log( - $this->getNullList(), - $this->getMockLogger() - ); - $list->listTypes(); - $this->assertLogCount(2); - } - - public function testListTypesLogsEntry() - { - $list = new Horde_Kolab_Storage_List_Decorator_Log( - $this->getAnnotatedList(), - $this->getMockLogger() - ); - $list->listTypes(); - $this->assertLogContains('List contained 4 folders and types.'); + $this->assertLogContains('List for test@example.com@mock:0 contained 2 folders.'); } public function testListAnnotationsLogsEntry() @@ -81,19 +61,7 @@ extends Horde_Kolab_Storage_TestCase $this->getAnnotatedList(), $this->getMockLogger() ); - $list->listFolderTypeAnnotations(); - $this->assertLogContains('List contained 4 folders and annotations.'); - } - - public function testGetQueryReturnsQuery() - { - $list = new Horde_Kolab_Storage_List_Decorator_Log( - $this->getNullList(), - $this->getMockLogger() - ); - $this->assertInstanceOf( - 'Horde_Kolab_Storage_Query', - $list->getQuery('Base') - ); + $list->listFolderTypes(); + $this->assertLogContains('List for test@example.com@mock:0 contained 4 folders and annotations.'); } } diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php index 76e389bf2..a70d7db0f 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Query/BaseTest.php @@ -37,31 +37,53 @@ extends Horde_Kolab_Storage_TestCase { public function testByTypeReturnsArray() { - $list = new Horde_Kolab_Storage_List_Base( - $this->getNullMock(), - new Horde_Kolab_Storage_Factory() - ); - $query = $list->getQuery('Base'); + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getNullList($factory)); $this->assertType('array', $query->listByType('test')); } public function testListCalendarsListsCalendars() { - $list = new Horde_Kolab_Storage_List_Base( - $this->getAnnotatedMock(), - new Horde_Kolab_Storage_Factory() - ); - $query = $list->getQuery('Base'); + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory)); $this->assertEquals(array('INBOX/Calendar'), $query->listByType('event')); } public function testListTasklistsListsTasklists() { - $list = new Horde_Kolab_Storage_List_Base( - $this->getAnnotatedMock(), - new Horde_Kolab_Storage_Factory() - ); - $query = $list->getQuery('Base'); + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory)); $this->assertEquals(array('INBOX/Tasks'), $query->listByType('task')); } + + public function testTypeReturnsArray() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getNullList($factory)); + $this->assertType('array', $query->listTypes()); + } + + public function testTypeReturnsAnnotations() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory)); + $this->assertEquals( + array( + 'INBOX/Calendar' => 'event', + 'INBOX/Contacts' => 'contact', + 'INBOX/Notes' => 'note', + 'INBOX/Tasks' => 'task', + ), + $query->listTypes() + ); + } + + public function testAnnotationsReturnsHandlers() + { + $factory = new Horde_Kolab_Storage_Factory(); + $query = $factory->createListQuery('Base', $this->getAnnotatedList($factory)); + foreach ($query->listFolderTypeAnnotations() as $folder => $type) { + $this->assertInstanceOf('Horde_Kolab_Storage_Folder_Type', $type); + }; + } } -- 2.11.0