diff --git a/src/Composer/Script/EventDispatcher.php b/src/Composer/Script/EventDispatcher.php index 7dd1e1f1c..31589bf7b 100644 --- a/src/Composer/Script/EventDispatcher.php +++ b/src/Composer/Script/EventDispatcher.php @@ -95,7 +95,7 @@ class EventDispatcher } try { - $className::$methodName($event); + $this->executeEventPhpScript($className, $methodName, $event); } catch (\Exception $e) { $message = "Script %s handling the %s event terminated with an exception"; $this->io->write(''.sprintf($message, $callable, $event->getName()).''); @@ -116,6 +116,16 @@ class EventDispatcher } } + /** + * @param string $className + * @param string $methodName + * @param Event $event Event invoking the PHP callable + */ + protected function executeEventPhpScript($className, $methodName, Event $event) + { + $className::$methodName($event); + } + /** * @param Event $event Event object * @return array Listeners diff --git a/tests/Composer/Test/Script/EventDispatcherTest.php b/tests/Composer/Test/Script/EventDispatcherTest.php index c6bfc710c..6a06a2e46 100644 --- a/tests/Composer/Test/Script/EventDispatcherTest.php +++ b/tests/Composer/Test/Script/EventDispatcherTest.php @@ -63,6 +63,44 @@ class EventDispatcherTest extends TestCase $dispatcher->dispatchCommandEvent("post-install-cmd"); } + public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack() + { + $io = $this->getMock('Composer\IO\IOInterface'); + $process = $this->getMock('Composer\Util\ProcessExecutor'); + $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher') + ->setConstructorArgs(array( + $this->getMock('Composer\Composer'), + $io, + $process, + )) + ->setMethods(array( + 'getListeners', + 'executeEventPhpScript', + )) + ->getMock(); + + $io->expects($this->never()) + ->method('write'); + $process->expects($this->exactly(2)) + ->method('execute'); + + $listeners = array( + 'echo -n foo', + 'Composer\\Test\\Script\\EventDispatcherTest::someMethod', + 'echo -n bar', + ); + $dispatcher->expects($this->atLeastOnce()) + ->method('getListeners') + ->will($this->returnValue($listeners)); + + $dispatcher->expects($this->once()) + ->method('executeEventPhpScript') + ->with('Composer\Test\Script\EventDispatcherTest', 'someMethod') + ->will($this->returnValue(true)); + + $dispatcher->dispatchCommandEvent("post-install-cmd"); + } + private function getDispatcherStubForListenersTest($listeners, $io) { $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher') @@ -93,4 +131,9 @@ class EventDispatcherTest extends TestCase { throw new \RuntimeException(); } + + public static function someMethod() + { + return true; + } }