Expect Exceptions from Horde_Ldap not PEAR errors.
authorGunnar Wrobel <p@rdus.de>
Fri, 19 Mar 2010 12:02:58 +0000 (13:02 +0100)
committerGunnar Wrobel <p@rdus.de>
Fri, 19 Mar 2010 12:02:58 +0000 (13:02 +0100)
framework/Kolab_Server/lib/Horde/Kolab/Server/Ldap.php
framework/Kolab_Server/test/Horde/Kolab/Server/Class/Server/Ldap/FilteredTest.php
framework/Kolab_Server/test/Horde/Kolab/Server/Class/Server/Ldap/StandardTest.php
framework/Kolab_Server/test/Horde/Kolab/Server/Class/Server/LdapTest.php

index 3d2d2b6..ea33de5 100644 (file)
@@ -65,10 +65,15 @@ implements Horde_Kolab_Server_Interface
         $this->_conn    = $connection;
         $this->_base_dn = $base_dn;
 
-        $this->_handleError(
-            Horde_Ldap::checkLDAPExtension(),
-            Horde_Kolab_Server_Exception::MISSING_LDAP_EXTENSION
-        );
+        try {
+            Horde_Ldap::checkLDAPExtension();
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                $e->getMessage(),
+                Horde_Kolab_Server_Exception::MISSING_LDAP_EXTENSION,
+                $e
+            );
+        }
     }
 
     /**
@@ -97,7 +102,7 @@ implements Horde_Kolab_Server_Interface
                 } else {
                     throw new Horde_Kolab_Server_Exception(
                         'Bind failed!',
-                        Horde_Kolab_Server_Exception::BIND_FAILED,
+                        Horde_Kolab_Server_Exception::SYSTEM,
                         $e
                     );
                 }
@@ -209,14 +214,18 @@ implements Horde_Kolab_Server_Interface
         array $data
     ) {
         $changes = new Horde_Kolab_Server_Ldap_Changes($object, $data);
-        $entry  = $this->_conn->getWrite()->getEntry(
-            $object->getGuid(), array_keys($data)
-        );
-        $this->_handleError($entry, Horde_Kolab_Server_Exception::SYSTEM);
-        $this->_handleError(
-            $this->_conn->getWrite()->modify($entry, $changes->getChangeset()),
-            Horde_Kolab_Server_Exception::SYSTEM
-        );
+        try {
+            $entry  = $this->_conn->getWrite()->getEntry(
+                $object->getGuid(), array_keys($data)
+            );
+            $this->_conn->getWrite()->modify($entry, $changes->getChangeset());
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Saving failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
     }
 
     /**
@@ -234,12 +243,16 @@ implements Horde_Kolab_Server_Interface
         Horde_Kolab_Server_Object_Interface $object,
         array $data
     ) {
-        $entry  = Horde_Ldap_Entry::createFresh($object->getGuid(), $data);
-        $this->_handleError($entry, Horde_Kolab_Server_Exception::SYSTEM);
-        $this->_handleError(
-            $this->_conn->getWrite()->add($entry),
-            Horde_Kolab_Server_Exception::SYSTEM
-        );
+        try {
+            $entry  = Horde_Ldap_Entry::createFresh($object->getGuid(), $data);
+            $this->_conn->getWrite()->add($entry);
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Adding object failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
     }
 
     /**
@@ -253,10 +266,15 @@ implements Horde_Kolab_Server_Interface
      */
     public function delete($guid)
     {
-        $this->_handleError(
-            $this->_conn->getWrite()->delete($guid),
-            Horde_Kolab_Server_Exception::SYSTEM
-        );
+        try {
+            $this->_conn->getWrite()->delete($guid);
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Deleting object failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
     }
 
     /**
@@ -271,10 +289,15 @@ implements Horde_Kolab_Server_Interface
      */
     public function rename($guid, $new)
     {
-        $this->_handleError(
-            $this->_conn->getWrite()->move($guid, $new),
-            Horde_Kolab_Server_Exception::SYSTEM
-        );
+        try {
+            $this->_conn->getWrite()->move($guid, $new);
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Renaming object failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
     }
 
     /**
@@ -286,9 +309,15 @@ implements Horde_Kolab_Server_Interface
      */
     public function getSchema()
     {
-        $result = $this->_conn->getRead()->schema();
-        $this->_handleError($result, Horde_Kolab_Server_Exception::SYSTEM);
-        return $result;
+        try {
+            return $this->_conn->getRead()->schema();
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Retrieving the schema failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
     }
 
     /**
@@ -300,45 +329,27 @@ implements Horde_Kolab_Server_Interface
      */
     public function getParentGuid($guid)
     {
-        $base = Horde_Ldap_Util::ldap_explode_dn(
-            $guid,
-            array(
-                'casefold' => 'none',
-                'reverse' => false,
-                'onlyvalues' => false
-            )
-        );
-        $this->_handleError($base);
-        $id = array_shift($base);
-        $parent = Horde_Ldap_Util::canonical_dn(
-            $base, array('casefold' => 'none')
-        );
-        $this->_handleError($parent);
-        return $parent;
-    }
-
-    /**
-     * Check for a PEAR Error and convert it to an exception if necessary.
-     *
-     * @param mixed $result The result to be tested.
-     * @param code  $code   The error code to use in case the result is an error.
-     *
-     * @return NULL.
-     *
-     * @throws Horde_Kolab_Server_Exception If the connection failed.
-     */
-    private function _handleError(
-        $result,
-        $code = Horde_Kolab_Server_Exception::SYSTEM
-    ) {
-        if ($result instanceOf PEAR_Error) {
-            if ($code == Horde_Kolab_Server_Exception::BIND_FAILED
-                && $result->getCode() == 49) {
-                throw new Horde_Kolab_Server_Exception_Bindfailed($result, $code);
-            } else {
-                throw new Horde_Kolab_Server_Exception($result, $code);
-            }
+        try {
+            $base = Horde_Ldap_Util::ldap_explode_dn(
+                $guid,
+                array(
+                    'casefold' => 'none',
+                    'reverse' => false,
+                    'onlyvalues' => false
+                )
+            );
+            $id = array_shift($base);
+            $parent = Horde_Ldap_Util::canonical_dn(
+                $base, array('casefold' => 'none')
+            );
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Retrieving the parent object failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
         }
+        return $parent;
     }
 
     /**
@@ -355,8 +366,15 @@ implements Horde_Kolab_Server_Interface
      */
     protected function _search($filter, array $params, $base)
     {
-        $search = $this->_conn->getRead()->search($base, $filter, $params);
-        $this->_handleError($search, Horde_Kolab_Server_Exception::SYSTEM);
+        try {
+            $search = $this->_conn->getRead()->search($base, $filter, $params);
+        } catch (Horde_Ldap_Exception $e) {
+            throw new Horde_Kolab_Server_Exception(
+                'Search failed!',
+                Horde_Kolab_Server_Exception::SYSTEM,
+                $e
+            );
+        }
         return new Horde_Kolab_Server_Result_Ldap($search);
     }
 }
index c22f84f..443ae88 100644 (file)
@@ -107,7 +107,7 @@ class Horde_Kolab_Server_Class_Server_Ldap_FilteredTest extends Horde_Kolab_Serv
     {
         $this->ldap_read->expects($this->exactly(1))
             ->method('search')
-            ->will($this->returnValue(new PEAR_Error('Search failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Search failed!')));
         try {
             $this->assertEquals(array('dn' => 'test'), $this->server->findBelow('(equals=equals)', ''));
             $this->fail('No exception!');
index fab3e27..242be7b 100644 (file)
@@ -106,7 +106,7 @@ class Horde_Kolab_Server_Class_Server_Ldap_StandardTest extends Horde_Kolab_Serv
     {
         $this->ldap_read->expects($this->exactly(1))
             ->method('search')
-            ->will($this->returnValue(new PEAR_Error('Search failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Search failed!')));
         try {
             $this->assertEquals(array('dn' => 'test'), $this->server->findBelow('(equals=equals)', ''));
             $this->fail('No exception!');
index 97cc421..1fc544a 100644 (file)
@@ -34,8 +34,6 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
 {
     public function setUp()
     {
-        $this->skipIfNoLdap();
-
         $this->ldap_read  = $this->getMock('Horde_Ldap');
         $this->ldap_write = $this->getMock('Horde_Ldap');
         $connection = new Horde_Kolab_Server_Connection_Splittedldap(
@@ -115,7 +113,7 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
             $this->fail('No exception!');
         } catch (Exception $e) {
             $this->assertEquals('Bind failed!', $e->getMessage());
-            $this->assertEquals(Horde_Kolab_Server_Exception::BIND_FAILED, $e->getCode());
+            $this->assertEquals(Horde_Kolab_Server_Exception::SYSTEM, $e->getCode());
         }
     }
 
@@ -263,7 +261,7 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
     {
         $this->ldap_read->expects($this->exactly(1))
             ->method('search')
-            ->will($this->returnValue(new PEAR_Error('Search failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Search failed!')));
         try {
             $this->assertEquals(array('dn' => 'test'), $this->server->find('(equals=equals)'));
             $this->fail('No exception!');
@@ -332,7 +330,7 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
         $object = $this->getMock('Horde_Kolab_Server_Object_Interface');
         $this->ldap_write->expects($this->exactly(1))
             ->method('modify')
-            ->will($this->returnValue(new PEAR_Error('Saving failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Saving failed!')));
         $object->expects($this->exactly(1))
             ->method('readInternal')
             ->will($this->returnValue(array()));
@@ -377,12 +375,12 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
         $object = $this->getMock('Horde_Kolab_Server_Object_Interface');
         $this->ldap_write->expects($this->exactly(1))
             ->method('add')
-            ->will($this->returnValue(new PEAR_Error('Saving failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Saving failed!')));
         try {
             $this->server->add($object, array('add' => array('dn' => 'test')));
             $this->fail('No exception!');
         } catch (Exception $e) {
-            $this->assertEquals('Saving failed!', $e->getMessage());
+            $this->assertEquals('Adding object failed!', $e->getMessage());
             $this->assertEquals(Horde_Kolab_Server_Exception::SYSTEM, $e->getCode());
         }
     }
@@ -407,12 +405,12 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
     {
         $this->ldap_write->expects($this->exactly(1))
             ->method('delete')
-            ->will($this->returnValue(new PEAR_Error('Deleting failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Deleting failed!')));
         try {
             $this->server->delete('test');
             $this->fail('No exception!');
         } catch (Exception $e) {
-            $this->assertEquals('Deleting failed!', $e->getMessage());
+            $this->assertEquals('Deleting object failed!', $e->getMessage());
             $this->assertEquals(Horde_Kolab_Server_Exception::SYSTEM, $e->getCode());
         }
     }
@@ -445,12 +443,12 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
     {
         $this->ldap_write->expects($this->exactly(1))
             ->method('move')
-            ->will($this->returnValue(new PEAR_Error('Renaming failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Renaming failed!')));
         try {
             $this->server->rename('test', 'new');
             $this->fail('No exception!');
         } catch (Exception $e) {
-            $this->assertEquals('Renaming failed!', $e->getMessage());
+            $this->assertEquals('Renaming object failed!', $e->getMessage());
             $this->assertEquals(Horde_Kolab_Server_Exception::SYSTEM, $e->getCode());
         }
     }
@@ -470,12 +468,12 @@ class Horde_Kolab_Server_Class_Server_LdapTest extends Horde_Kolab_Server_LdapTe
     {
         $this->ldap_read->expects($this->exactly(1))
             ->method('schema')
-            ->will($this->returnValue(new PEAR_Error('Schema failed!')));
+            ->will($this->throwException(new Horde_Ldap_Exception('Schema failed!')));
         try {
             $this->server->getSchema();
             $this->fail('No exception!');
         } catch (Exception $e) {
-            $this->assertEquals('Schema failed!', $e->getMessage());
+            $this->assertEquals('Retrieving the schema failed!', $e->getMessage());
             $this->assertEquals(Horde_Kolab_Server_Exception::SYSTEM, $e->getCode());
         }
     }