EventDispatcherTest.php 9.3 KB

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