From d3cec0399a8df70dfdaa7dc6d701ace94d87e980 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 22 May 2012 13:16:56 +0200 Subject: [PATCH] CS fixes & do not replace the exception since that removes the stack trace --- src/Composer/Script/EventDispatcher.php | 8 ++-- .../Test/Script/EventDispatcherTest.php | 45 +++++++++---------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/Composer/Script/EventDispatcher.php b/src/Composer/Script/EventDispatcher.php index c2248b190..1e8eaa58b 100644 --- a/src/Composer/Script/EventDispatcher.php +++ b/src/Composer/Script/EventDispatcher.php @@ -91,11 +91,9 @@ class EventDispatcher try { $className::$methodName($event); } catch (\Exception $e) { - $message = "%s terminated with an exception.\n[%s] %s"; - throw new \RuntimeException(sprintf($message, - $callable, - get_class($e), - $e->getMessage())); + $message = "Script %s handling the %s event terminated with an exception"; + $this->io->write(''.sprintf($message, $callable, $event->getName()).''); + throw $e; } } } diff --git a/tests/Composer/Test/Script/EventDispatcherTest.php b/tests/Composer/Test/Script/EventDispatcherTest.php index 724eb182d..2da566907 100644 --- a/tests/Composer/Test/Script/EventDispatcherTest.php +++ b/tests/Composer/Test/Script/EventDispatcherTest.php @@ -1,4 +1,5 @@ - */ class EventDispatcherTest extends TestCase { /** - * Test the doDispatch method properly catches any exception - * thrown from the listener invocation, i.e., $className::$methodName($event), - * and replaces it with a \RuntimeException. - * - * @expectedException \RuntimeException + * @expectedException RuntimeException */ - public function testListenerExceptionsAreSuppressed() + public function testListenerExceptionsAreCaught() { + $io = $this->getMock('Composer\IO\IOInterface'); $dispatcher = $this->getDispatcherStubForListenersTest(array( - "Composer\Test\Script\EventDispatcherTest::call" - )); + "Composer\Test\Script\EventDispatcherTest::call" + ), $io); + + $io->expects($this->once()) + ->method('write') + ->with('Script Composer\Test\Script\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception'); + $dispatcher->dispatchCommandEvent("post-install-cmd"); } - private function getDispatcherStubForListenersTest($listeners) + private function getDispatcherStubForListenersTest($listeners, $io) { $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher') - ->setConstructorArgs(array( - $this->getMock('Composer\Composer'), - $this->getMock('Composer\IO\IOInterface'))) - ->setMethods(array('getListeners')) - ->getMock(); + ->setConstructorArgs(array( + $this->getMock('Composer\Composer'), + $io, + )) + ->setMethods(array('getListeners')) + ->getMock(); $dispatcher->expects($this->atLeastOnce()) - ->method('getListeners') - ->will($this->returnValue($listeners)); + ->method('getListeners') + ->will($this->returnValue($listeners)); return $dispatcher; } public static function call() { - throw new \Exception(); + throw new \RuntimeException(); } } \ No newline at end of file