EventDispatcherTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /**
  168. * @expectedException RuntimeException
  169. */
  170. public function testDispatcherDetectInfiniteRecursion()
  171. {
  172. $process = $this->getMock('Composer\Util\ProcessExecutor');
  173. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  174. ->setConstructorArgs(array(
  175. $composer = $this->getMock('Composer\Composer'),
  176. $io = $this->getMock('Composer\IO\IOInterface'),
  177. $process,
  178. ))
  179. ->setMethods(array(
  180. 'getListeners',
  181. ))
  182. ->getMock();
  183. $dispatcher->expects($this->atLeastOnce())
  184. ->method('getListeners')
  185. ->will($this->returnCallback(function (Event $event) {
  186. if ($event->getName() === 'root') {
  187. return array('@recurse');
  188. } elseif ($event->getName() === 'recurse') {
  189. return array('@root');
  190. }
  191. return array();
  192. }));
  193. $dispatcher->dispatch('root', new CommandEvent('root', $composer, $io));
  194. }
  195. private function getDispatcherStubForListenersTest($listeners, $io)
  196. {
  197. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  198. ->setConstructorArgs(array(
  199. $this->getMock('Composer\Composer'),
  200. $io,
  201. ))
  202. ->setMethods(array('getListeners'))
  203. ->getMock();
  204. $dispatcher->expects($this->atLeastOnce())
  205. ->method('getListeners')
  206. ->will($this->returnValue($listeners));
  207. return $dispatcher;
  208. }
  209. public function getValidCommands()
  210. {
  211. return array(
  212. array('phpunit'),
  213. array('echo foo'),
  214. array('echo -n foo'),
  215. );
  216. }
  217. public function testDispatcherOutputsCommand()
  218. {
  219. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  220. ->setConstructorArgs(array(
  221. $this->getMock('Composer\Composer'),
  222. $io = $this->getMock('Composer\IO\IOInterface'),
  223. new ProcessExecutor,
  224. ))
  225. ->setMethods(array('getListeners'))
  226. ->getMock();
  227. $listener = array('echo foo');
  228. $dispatcher->expects($this->atLeastOnce())
  229. ->method('getListeners')
  230. ->will($this->returnValue($listener));
  231. $io->expects($this->once())
  232. ->method('writeError')
  233. ->with($this->equalTo('> echo foo'));
  234. ob_start();
  235. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  236. $this->assertEquals('foo', trim(ob_get_clean()));
  237. }
  238. public function testDispatcherOutputsErrorOnFailedCommand()
  239. {
  240. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  241. ->setConstructorArgs(array(
  242. $this->getMock('Composer\Composer'),
  243. $io = $this->getMock('Composer\IO\IOInterface'),
  244. new ProcessExecutor,
  245. ))
  246. ->setMethods(array('getListeners'))
  247. ->getMock();
  248. $code = 'exit 1';
  249. $listener = array($code);
  250. $dispatcher->expects($this->atLeastOnce())
  251. ->method('getListeners')
  252. ->will($this->returnValue($listener));
  253. $io->expects($this->at(0))
  254. ->method('isVerbose')
  255. ->willReturn(0);
  256. $io->expects($this->at(1))
  257. ->method('writeError')
  258. ->willReturn('> exit 1');
  259. $io->expects($this->at(2))
  260. ->method('writeError')
  261. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  262. $this->setExpectedException('RuntimeException');
  263. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  264. }
  265. public function testDispatcherInstallerEvents()
  266. {
  267. $process = $this->getMock('Composer\Util\ProcessExecutor');
  268. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  269. ->setConstructorArgs(array(
  270. $this->getMock('Composer\Composer'),
  271. $this->getMock('Composer\IO\IOInterface'),
  272. $process,
  273. ))
  274. ->setMethods(array('getListeners'))
  275. ->getMock();
  276. $dispatcher->expects($this->atLeastOnce())
  277. ->method('getListeners')
  278. ->will($this->returnValue(array()));
  279. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  280. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  281. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  282. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  283. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request);
  284. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request, array());
  285. }
  286. public static function call()
  287. {
  288. throw new \RuntimeException();
  289. }
  290. public static function expectsCommandEvent(CommandEvent $event)
  291. {
  292. return false;
  293. }
  294. public static function expectsVariableEvent($event)
  295. {
  296. return false;
  297. }
  298. public static function someMethod()
  299. {
  300. return true;
  301. }
  302. }