Add Horde_Support_Backtrace#getMap().
authorJan Schneider <jan@horde.org>
Tue, 22 Dec 2009 00:49:09 +0000 (01:49 +0100)
committerJan Schneider <jan@horde.org>
Tue, 22 Dec 2009 01:48:45 +0000 (02:48 +0100)
framework/Support/lib/Horde/Support/Backtrace.php
framework/Support/test/Horde/Support/BacktraceTest.php

index aed0b27..cd4f895 100644 (file)
@@ -111,4 +111,30 @@ class Horde_Support_Backtrace
     {
         return $this->getContext(1);
     }
+
+    /**
+     * Returns a simple, human-readable list of the complete backtrace.
+     *
+     * @return string  The backtrace map.
+     */
+    public function getMap()
+    {
+        $count = count($this->_backtrace);
+        $pad = strlen($count);
+        $map = '';
+        for ($i = $count - 1; $i >= 0; $i--) {
+            $map .= str_pad($count - $i, $pad, ' ', STR_PAD_LEFT) . '. ';
+            if (isset($this->_backtrace[$i]['class'])) {
+                $map .= $this->_backtrace[$i]['class']
+                    . $this->_backtrace[$i]['type'];
+            }
+            $map .= $this->_backtrace[$i]['function'] . '()';
+            if (isset($this->_backtrace[$i]['file'])) {
+                $map .= ' ' . $this->_backtrace[$i]['file']
+                    . ':' . $this->_backtrace[$i]['line'];
+            }
+            $map .= "\n";
+        }
+        return $map;
+    }
 }
index 8c026cf..12d25e6 100644 (file)
@@ -7,6 +7,11 @@
  * @license    http://opensource.org/licenses/bsd-license.php
  */
 
+function backtraceTestFunction()
+{
+    return debug_backtrace(false);
+}
+
 /**
  * @group      support
  * @category   Horde
  */
 class Horde_Support_BacktraceTest extends PHPUnit_Framework_TestCase
 {
+    // Keep these two methods on the top so that the line numbers don't change
+    // when new tests are added.
+    public function instanceMethod()
+    {
+        return Horde_Support_BacktraceTest::staticMethod();
+    }
+
+    public static function staticMethod()
+    {
+        return backtraceTestFunction();
+    }
+
     public function testCreateFromDefaultBacktrace()
     {
         $trace = new Horde_Support_Backtrace();
@@ -67,6 +84,18 @@ class Horde_Support_BacktraceTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(count($dbt), $backtrace->getNestingLevel());
     }
 
+    public function testGetMap()
+    {
+        $backtrace = new Horde_Support_Backtrace(array_slice($this->instanceMethod(), 0, 4));
+        $file = __FILE__;
+        $this->assertEquals("1. Horde_Support_BacktraceTest->testGetMap()
+2. Horde_Support_BacktraceTest->instanceMethod() $file:89
+3. Horde_Support_BacktraceTest::staticMethod() $file:29
+4. backtraceTestFunction() $file:34
+",
+                            $backtrace->getMap());
+    }
+
     public function returnBacktrace()
     {
         return debug_backtrace();