EventDispatcherTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. public function testDispatcherCanExecuteComposerScriptGroups()
  120. {
  121. $process = $this->getMock('Composer\Util\ProcessExecutor');
  122. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  123. ->setConstructorArgs(array(
  124. $composer = $this->getMock('Composer\Composer'),
  125. $io = $this->getMock('Composer\IO\IOInterface'),
  126. $process,
  127. ))
  128. ->setMethods(array(
  129. 'getListeners',
  130. ))
  131. ->getMock();
  132. $process->expects($this->exactly(3))
  133. ->method('execute')
  134. ->will($this->returnValue(0));
  135. $dispatcher->expects($this->atLeastOnce())
  136. ->method('getListeners')
  137. ->will($this->returnCallback(function (Event $event) {
  138. if ($event->getName() === 'root') {
  139. return array('@group');
  140. } elseif ($event->getName() === 'group') {
  141. return array('echo -n foo', '@subgroup', 'echo -n bar');
  142. } elseif ($event->getName() === 'subgroup') {
  143. return array('echo -n baz');
  144. }
  145. return array();
  146. }));
  147. $io->expects($this->any())
  148. ->method('isVerbose')
  149. ->willReturn(1);
  150. $io->expects($this->at(1))
  151. ->method('writeError')
  152. ->with($this->equalTo('> root: @group'));
  153. $io->expects($this->at(3))
  154. ->method('writeError')
  155. ->with($this->equalTo('> group: echo -n foo'));
  156. $io->expects($this->at(5))
  157. ->method('writeError')
  158. ->with($this->equalTo('> group: @subgroup'));
  159. $io->expects($this->at(7))
  160. ->method('writeError')
  161. ->with($this->equalTo('> subgroup: echo -n baz'));
  162. $io->expects($this->at(9))
  163. ->method('writeError')
  164. ->with($this->equalTo('> group: echo -n bar'));
  165. $dispatcher->dispatch('root', new CommandEvent('root', $composer, $io));
  166. }
  167. private function getDispatcherStubForListenersTest($listeners, $io)
  168. {
  169. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  170. ->setConstructorArgs(array(
  171. $this->getMock('Composer\Composer'),
  172. $io,
  173. ))
  174. ->setMethods(array('getListeners'))
  175. ->getMock();
  176. $dispatcher->expects($this->atLeastOnce())
  177. ->method('getListeners')
  178. ->will($this->returnValue($listeners));
  179. return $dispatcher;
  180. }
  181. public function getValidCommands()
  182. {
  183. return array(
  184. array('phpunit'),
  185. array('echo foo'),
  186. array('echo -n foo'),
  187. );
  188. }
  189. public function testDispatcherOutputsCommand()
  190. {
  191. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  192. ->setConstructorArgs(array(
  193. $this->getMock('Composer\Composer'),
  194. $io = $this->getMock('Composer\IO\IOInterface'),
  195. new ProcessExecutor,
  196. ))
  197. ->setMethods(array('getListeners'))
  198. ->getMock();
  199. $listener = array('echo foo');
  200. $dispatcher->expects($this->atLeastOnce())
  201. ->method('getListeners')
  202. ->will($this->returnValue($listener));
  203. $io->expects($this->once())
  204. ->method('writeError')
  205. ->with($this->equalTo('> echo foo'));
  206. ob_start();
  207. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  208. $this->assertEquals('foo', trim(ob_get_clean()));
  209. }
  210. public function testDispatcherOutputsErrorOnFailedCommand()
  211. {
  212. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  213. ->setConstructorArgs(array(
  214. $this->getMock('Composer\Composer'),
  215. $io = $this->getMock('Composer\IO\IOInterface'),
  216. new ProcessExecutor,
  217. ))
  218. ->setMethods(array('getListeners'))
  219. ->getMock();
  220. $code = 'exit 1';
  221. $listener = array($code);
  222. $dispatcher->expects($this->atLeastOnce())
  223. ->method('getListeners')
  224. ->will($this->returnValue($listener));
  225. $io->expects($this->at(0))
  226. ->method('isVerbose')
  227. ->willReturn(0);
  228. $io->expects($this->at(1))
  229. ->method('writeError')
  230. ->willReturn('> exit 1');
  231. $io->expects($this->at(2))
  232. ->method('writeError')
  233. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  234. $this->setExpectedException('RuntimeException');
  235. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  236. }
  237. public function testDispatcherInstallerEvents()
  238. {
  239. $process = $this->getMock('Composer\Util\ProcessExecutor');
  240. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  241. ->setConstructorArgs(array(
  242. $this->getMock('Composer\Composer'),
  243. $this->getMock('Composer\IO\IOInterface'),
  244. $process,
  245. ))
  246. ->setMethods(array('getListeners'))
  247. ->getMock();
  248. $dispatcher->expects($this->atLeastOnce())
  249. ->method('getListeners')
  250. ->will($this->returnValue(array()));
  251. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  252. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  253. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  254. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  255. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request);
  256. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request, array());
  257. }
  258. public static function call()
  259. {
  260. throw new \RuntimeException();
  261. }
  262. public static function expectsCommandEvent(CommandEvent $event)
  263. {
  264. return false;
  265. }
  266. public static function expectsVariableEvent($event)
  267. {
  268. return false;
  269. }
  270. public static function someMethod()
  271. {
  272. return true;
  273. }
  274. }