From f626ccbcee1b67a13e14cae6584c21243b1ec79d Mon Sep 17 00:00:00 2001 From: Andrea Turso Date: Tue, 15 May 2012 21:25:18 +0100 Subject: [PATCH] Wrapped the listener invocation a try/catch block that catches any exception and throws a \RuntimeException. Added a test case for the EventDispatcher. Note: In order to test the doDispatch method I had to use a stub EventDispatcher with a getListeners that returned a preconfigured array. IMHO there should be a way to inject the listeners into the EventDispatcher. --- src/Composer/Script/EventDispatcher.php | 6 +- .../Test/Script/EventDispatcherTest.php | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/Script/EventDispatcherTest.php diff --git a/src/Composer/Script/EventDispatcher.php b/src/Composer/Script/EventDispatcher.php index 83b4fb500..3b420ba70 100644 --- a/src/Composer/Script/EventDispatcher.php +++ b/src/Composer/Script/EventDispatcher.php @@ -91,7 +91,11 @@ class EventDispatcher throw new \UnexpectedValueException('Method '.$callable.' is not callable, can not call '.$event->getName().' script'); } - $className::$methodName($event); + try { + $className::$methodName($event); + } catch (\Exception $e) { + throw new \RuntimeException("'{$callable}' terminated with an exception."); + } } } diff --git a/tests/Composer/Test/Script/EventDispatcherTest.php b/tests/Composer/Test/Script/EventDispatcherTest.php new file mode 100644 index 000000000..b49e05fe1 --- /dev/null +++ b/tests/Composer/Test/Script/EventDispatcherTest.php @@ -0,0 +1,58 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Script; + +use Exception; +use Composer\Test\TestCase; +use Composer\Script\Event; +use Composer\Script\EventDispatcher; + +/** + * + * @group + * @ticket #693 + * @author Andrea Turso + */ +class EventDispatcherTest extends TestCase +{ + /** + * @expectedException \RuntimeException + */ + public function testListenerExceptionsAreSuppressed() + { + $dispatcher = $this->getDispatcherStubForListenersTest(array( + "Composer\Test\Script\EventDispatcherTest::call" + )); + $dispatcher->dispatchCommandEvent("post-install-cmd"); + } + + private function getDispatcherStubForListenersTest($listeners) + { + $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher') + ->setConstructorArgs(array( + $this->getMock('Composer\Composer'), + $this->getMock('Composer\IO\IOInterface'))) + ->setMethods(array('getListeners')) + ->getMock(); + + $dispatcher->expects($this->atLeastOnce()) + ->method('getListeners') + ->will($this->returnValue($listeners)); + + return $dispatcher; + } + + public static function call() + { + throw new Exception(); + } +} \ No newline at end of file