From 5d7acac2705a6a4e6dde8992faac21b82c8e6cfb Mon Sep 17 00:00:00 2001 From: filp Date: Wed, 5 Jun 2013 20:35:21 +0100 Subject: [PATCH] Add initial support for chained exception inspection --- src/Whoops/Exception/Inspector.php | 32 ++++++++++++++++++++++ tests/Whoops/Exception/InspectorTest.php | 35 ++++++++++++++++++++---- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/Whoops/Exception/Inspector.php b/src/Whoops/Exception/Inspector.php index 2e10c6d..19408dd 100644 --- a/src/Whoops/Exception/Inspector.php +++ b/src/Whoops/Exception/Inspector.php @@ -21,6 +21,11 @@ class Inspector */ private $frames; + /** + * @var Whoops\Exception\Inspector + */ + private $previousExceptionInspector; + /** * @param Exception $exception The exception to inspect */ @@ -53,6 +58,33 @@ class Inspector return $this->exception->getMessage(); } + /** + * Does this Exception have a previous Exception? + * @return bool + */ + public function hasPreviousException() + { + return !!$this->previousExceptionInspector || !!$this->exception->getPrevious(); + } + + /** + * Returns an inspector for a previous inspector, if any. + * @todo Clean this up a bit, cache stuff a bit better. + * @return Whoops\Exception\Inspector|null + */ + public function getPreviousExceptionInspector() + { + if($this->previousExceptionInspector === null) { + $previousException = $this->exception->getPrevious(); + + if($previousException) { + $this->previousExceptionInspector = new Inspector($previousException); + } + } + + return $this->previousExceptionInspector; + } + /** * Returns an iterator for the inspected exception's * frames. diff --git a/tests/Whoops/Exception/InspectorTest.php b/tests/Whoops/Exception/InspectorTest.php index 7c78740..ad7061d 100644 --- a/tests/Whoops/Exception/InspectorTest.php +++ b/tests/Whoops/Exception/InspectorTest.php @@ -7,19 +7,19 @@ namespace Whoops\Exception; use Whoops\Exception\Inspector; use Whoops\TestCase; -use RuntimeException; use Exception; -use Mockery as m; class InspectorTest extends TestCase { /** - * @param string $message + * @param string $message + * @param int $code + * @param Exception $previous * @return Exception */ - protected function getException($message = null) + protected function getException($message = null, $code = 0, Exception $previous = null) { - return m::mock('Exception', array($message)); + return new Exception($message, $code, $previous); } /** @@ -64,4 +64,29 @@ class InspectorTest extends TestCase $this->assertInstanceOf('Whoops\\Exception\\FrameCollection', $inspector->getFrames()); } + + /** + * @covers Whoops\Exception\Inspector::hasPreviousException + * @covers Whoops\Exception\Inspector::getPreviousExceptionInspector + */ + public function testPreviousException() + { + $previousException = $this->getException("I'm here first!"); + $exception = $this->getException("Oh boy", null, $previousException); + $inspector = $this->getInspectorInstance($exception); + + $this->assertTrue($inspector->hasPreviousException()); + $this->assertEquals($previousException, $inspector->getPreviousExceptionInspector()->getException()); + } + + /** + * @covers Whoops\Exception\Inspector::hasPreviousException + */ + public function testNegativeHasPreviousException() + { + $exception = $this->getException("Oh boy"); + $inspector = $this->getInspectorInstance($exception); + + $this->assertFalse($inspector->hasPreviousException()); + } }