EventDispatcherTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\EventDispatcher;
  12. use Composer\EventDispatcher\Event;
  13. use Composer\EventDispatcher\EventDispatcher;
  14. use Composer\Installer\InstallerEvents;
  15. use Composer\TestCase;
  16. use Composer\Script\ScriptEvents;
  17. use Composer\Script\CommandEvent;
  18. use Composer\Util\ProcessExecutor;
  19. class EventDispatcherTest extends TestCase
  20. {
  21. /**
  22. * @expectedException RuntimeException
  23. */
  24. public function testListenerExceptionsAreCaught()
  25. {
  26. $io = $this->getMock('Composer\IO\IOInterface');
  27. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  28. 'Composer\Test\EventDispatcher\EventDispatcherTest::call'
  29. ), $io);
  30. $io->expects($this->at(0))
  31. ->method('writeError')
  32. ->with('> Composer\Test\EventDispatcher\EventDispatcherTest::call');
  33. $io->expects($this->at(1))
  34. ->method('writeError')
  35. ->with('<error>Script Composer\Test\EventDispatcher\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception</error>');
  36. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  37. }
  38. public function testDispatcherCanConvertScriptEventToCommandEventForListener()
  39. {
  40. $io = $this->getMock('Composer\IO\IOInterface');
  41. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  42. 'Composer\Test\EventDispatcher\EventDispatcherTest::expectsCommandEvent'
  43. ), $io);
  44. $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
  45. }
  46. public function testDispatcherDoesNotAttemptConversionForListenerWithoutTypehint()
  47. {
  48. $io = $this->getMock('Composer\IO\IOInterface');
  49. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  50. 'Composer\Test\EventDispatcher\EventDispatcherTest::expectsVariableEvent'
  51. ), $io);
  52. $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
  53. }
  54. /**
  55. * @dataProvider getValidCommands
  56. * @param string $command
  57. */
  58. public function testDispatcherCanExecuteSingleCommandLineScript($command)
  59. {
  60. $process = $this->getMock('Composer\Util\ProcessExecutor');
  61. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  62. ->setConstructorArgs(array(
  63. $this->getMock('Composer\Composer'),
  64. $this->getMock('Composer\IO\IOInterface'),
  65. $process,
  66. ))
  67. ->setMethods(array('getListeners'))
  68. ->getMock();
  69. $listener = array($command);
  70. $dispatcher->expects($this->atLeastOnce())
  71. ->method('getListeners')
  72. ->will($this->returnValue($listener));
  73. $process->expects($this->once())
  74. ->method('execute')
  75. ->with($command)
  76. ->will($this->returnValue(0));
  77. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  78. }
  79. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  80. {
  81. $process = $this->getMock('Composer\Util\ProcessExecutor');
  82. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  83. ->setConstructorArgs(array(
  84. $this->getMock('Composer\Composer'),
  85. $io = $this->getMock('Composer\IO\IOInterface'),
  86. $process,
  87. ))
  88. ->setMethods(array(
  89. 'getListeners',
  90. ))
  91. ->getMock();
  92. $process->expects($this->exactly(2))
  93. ->method('execute')
  94. ->will($this->returnValue(0));
  95. $listeners = array(
  96. 'echo -n foo',
  97. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
  98. 'echo -n bar',
  99. );
  100. $dispatcher->expects($this->atLeastOnce())
  101. ->method('getListeners')
  102. ->will($this->returnValue($listeners));
  103. $io->expects($this->at(0))
  104. ->method('writeError')
  105. ->with($this->equalTo('> echo -n foo'));
  106. $io->expects($this->at(1))
  107. ->method('writeError')
  108. ->with($this->equalTo('> Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'));
  109. $io->expects($this->at(2))
  110. ->method('writeError')
  111. ->with($this->equalTo('> echo -n bar'));
  112. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  113. }
  114. private function getDispatcherStubForListenersTest($listeners, $io)
  115. {
  116. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  117. ->setConstructorArgs(array(
  118. $this->getMock('Composer\Composer'),
  119. $io,
  120. ))
  121. ->setMethods(array('getListeners'))
  122. ->getMock();
  123. $dispatcher->expects($this->atLeastOnce())
  124. ->method('getListeners')
  125. ->will($this->returnValue($listeners));
  126. return $dispatcher;
  127. }
  128. public function getValidCommands()
  129. {
  130. return array(
  131. array('phpunit'),
  132. array('echo foo'),
  133. array('echo -n foo'),
  134. );
  135. }
  136. public function testDispatcherOutputsCommand()
  137. {
  138. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  139. ->setConstructorArgs(array(
  140. $this->getMock('Composer\Composer'),
  141. $io = $this->getMock('Composer\IO\IOInterface'),
  142. new ProcessExecutor,
  143. ))
  144. ->setMethods(array('getListeners'))
  145. ->getMock();
  146. $listener = array('echo foo');
  147. $dispatcher->expects($this->atLeastOnce())
  148. ->method('getListeners')
  149. ->will($this->returnValue($listener));
  150. $io->expects($this->once())
  151. ->method('writeError')
  152. ->with($this->equalTo('> echo foo'));
  153. ob_start();
  154. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  155. $this->assertEquals('foo', trim(ob_get_clean()));
  156. }
  157. public function testDispatcherOutputsErrorOnFailedCommand()
  158. {
  159. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  160. ->setConstructorArgs(array(
  161. $this->getMock('Composer\Composer'),
  162. $io = $this->getMock('Composer\IO\IOInterface'),
  163. new ProcessExecutor,
  164. ))
  165. ->setMethods(array('getListeners'))
  166. ->getMock();
  167. $code = 'exit 1';
  168. $listener = array($code);
  169. $dispatcher->expects($this->atLeastOnce())
  170. ->method('getListeners')
  171. ->will($this->returnValue($listener));
  172. $io->expects($this->at(0))
  173. ->method('writeError')
  174. ->willReturn('> exit 1');
  175. $io->expects($this->at(1))
  176. ->method('writeError')
  177. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  178. $this->setExpectedException('RuntimeException');
  179. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  180. }
  181. public function testDispatcherInstallerEvents()
  182. {
  183. $process = $this->getMock('Composer\Util\ProcessExecutor');
  184. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  185. ->setConstructorArgs(array(
  186. $this->getMock('Composer\Composer'),
  187. $this->getMock('Composer\IO\IOInterface'),
  188. $process,
  189. ))
  190. ->setMethods(array('getListeners'))
  191. ->getMock();
  192. $dispatcher->expects($this->atLeastOnce())
  193. ->method('getListeners')
  194. ->will($this->returnValue(array()));
  195. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  196. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  197. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  198. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  199. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request);
  200. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request, array());
  201. }
  202. public static function call()
  203. {
  204. throw new \RuntimeException();
  205. }
  206. public static function expectsCommandEvent(CommandEvent $event)
  207. {
  208. return false;
  209. }
  210. public static function expectsVariableEvent($event)
  211. {
  212. return false;
  213. }
  214. public static function someMethod()
  215. {
  216. return true;
  217. }
  218. }