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 itpull/1186/head
parent
22cab83bb1
commit
88650f9333
|
@ -95,7 +95,7 @@ class EventDispatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$className::$methodName($event);
|
$this->executeEventPhpScript($className, $methodName, $event);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$message = "Script %s handling the %s event terminated with an exception";
|
$message = "Script %s handling the %s event terminated with an exception";
|
||||||
$this->io->write('<error>'.sprintf($message, $callable, $event->getName()).'</error>');
|
$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
|
* @param Event $event Event object
|
||||||
* @return array Listeners
|
* @return array Listeners
|
||||||
|
|
|
@ -63,6 +63,44 @@ class EventDispatcherTest extends TestCase
|
||||||
$dispatcher->dispatchCommandEvent("post-install-cmd");
|
$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)
|
private function getDispatcherStubForListenersTest($listeners, $io)
|
||||||
{
|
{
|
||||||
$dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
|
$dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
|
||||||
|
@ -93,4 +131,9 @@ class EventDispatcherTest extends TestCase
|
||||||
{
|
{
|
||||||
throw new \RuntimeException();
|
throw new \RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function someMethod()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue