Test and fix the PEAR exception wrapper.
authorGunnar Wrobel <p@rdus.de>
Tue, 26 Oct 2010 15:19:28 +0000 (17:19 +0200)
committerGunnar Wrobel <p@rdus.de>
Tue, 26 Oct 2010 15:19:28 +0000 (17:19 +0200)
framework/Exception/lib/Horde/Exception/Pear.php
framework/Exception/test/Horde/Exception/Autoload.php [new file with mode: 0644]
framework/Exception/test/Horde/Exception/ExceptionTest.php

index e0d43b7..31ae8f7 100644 (file)
@@ -34,7 +34,7 @@ class Horde_Exception_Pear extends Horde_Exception
     public function __construct(PEAR_Error $error)
     {
         parent::__construct(
-            $error->getMessage() . $this->_getPearTrace(),
+            $error->getMessage() . $this->_getPearTrace($error),
             $error->getCode()
         );
     }
@@ -50,11 +50,14 @@ class Horde_Exception_Pear extends Horde_Exception
     {
         $backtrace = $error->getBacktrace();
         if (!empty($backtrace)) {
-            $pear_error .= "\n\n" . 'PEAR Error:' . "\n";
+            $pear_error = "\n\n" . 'PEAR Error:' . "\n";
             foreach ($backtrace as $frame) {
-                $pear_error .= '    ' . $frame['class'] . '->'
-                    . $frame['function'] . ' ' . $frame['file']
-                    . ':' . $frame['line'] . "\n";
+                $pear_error .= '    '
+                    . (isset($frame['class']) ? $frame['class'] : 'unkown')
+                    . (isset($frame['type']) ? $frame['type'] : 'unkown')
+                    . (isset($frame['function']) ? $frame['function'] : 'unkown') . ' '
+                    . (isset($frame['file']) ? $frame['file'] : 'unkown') . ':'
+                    . (isset($frame['line']) ? $frame['line'] : 'unkown') . "\n";
             }
             $pear_error .= "\n";
             return $pear_error;
diff --git a/framework/Exception/test/Horde/Exception/Autoload.php b/framework/Exception/test/Horde/Exception/Autoload.php
new file mode 100644 (file)
index 0000000..813fa40
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * Copyright 2009-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   Horde
+ * @package    Exception
+ * @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=Exception
+ */
+
+require_once 'Horde/Test/Autoload.php';
+
+/** Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
index dffbdf2..7a33433 100644 (file)
 /**
  * Require the tested classes.
  */
-require_once 'Horde/Exception.php';
-require_once 'Horde/Exception/LastError.php';
-require_once 'Horde/Exception/NotFound.php';
-require_once 'Horde/Exception/PermissionDenied.php';
-require_once 'Horde/Exception/Prior.php';
+require_once 'Autoload.php';
 
 /**
  * Test for the Horde_Exception:: class.
@@ -141,6 +137,22 @@ class Horde_Exception_ExceptionTest extends  PHPUnit_Framework_TestCase
         $this->assertSame('An error occurred: get_last_error', $e->getMessage());
     }
 
+    public function testCatchingAndConvertingPearErrors()
+    {
+        @require_once 'PEAR.php';
+        if (!class_exists('PEAR_Error')) {
+            $this->markTestSkipped('PEAR_Error is missing!');
+        }
+        try {
+            Horde_Exception_Pear::catchError(new PEAR_Error('An error occurred.'));
+        } catch (Horde_Exception_Pear $e) {
+            $this->assertContains(
+                'Horde_Exception_ExceptionTest->testCatchingAndConvertingPearErrors unkown:unkown',
+                $e->getMessage()
+            );
+        }
+    }
+
     private function _getLastError()
     {
         return array(