EventDispatcherTest.php 8.9 KB

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