1
0
Fork 0

Add test for intermixing PHP callables and CLI commands in a single event's script stack

Wrapped execution of the PHP callable in its own method in order to mock/test it
pull/1186/head
John Kary 2012-10-06 22:37:52 -05:00
parent 22cab83bb1
commit 88650f9333
2 changed files with 54 additions and 1 deletions

View File

@ -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('<error>'.sprintf($message, $callable, $event->getName()).'</error>');
@ -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

View File

@ -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;
}
}