EventDispatcherTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\IO\BufferIO;
  16. use Composer\Script\ScriptEvents;
  17. use Composer\Script\CommandEvent;
  18. use Composer\Util\ProcessExecutor;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. class EventDispatcherTest extends TestCase
  21. {
  22. /**
  23. * @expectedException RuntimeException
  24. */
  25. public function testListenerExceptionsAreCaught()
  26. {
  27. $io = $this->getMock('Composer\IO\IOInterface');
  28. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  29. 'Composer\Test\EventDispatcher\EventDispatcherTest::call',
  30. ), $io);
  31. $io->expects($this->at(0))
  32. ->method('isVerbose')
  33. ->willReturn(0);
  34. $io->expects($this->at(1))
  35. ->method('writeError')
  36. ->with('> Composer\Test\EventDispatcher\EventDispatcherTest::call');
  37. $io->expects($this->at(2))
  38. ->method('writeError')
  39. ->with('<error>Script Composer\Test\EventDispatcher\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception</error>');
  40. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  41. }
  42. public function testDispatcherCanConvertScriptEventToCommandEventForListener()
  43. {
  44. $io = $this->getMock('Composer\IO\IOInterface');
  45. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  46. 'Composer\Test\EventDispatcher\EventDispatcherTest::expectsCommandEvent',
  47. ), $io);
  48. $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
  49. }
  50. public function testDispatcherDoesNotAttemptConversionForListenerWithoutTypehint()
  51. {
  52. $io = $this->getMock('Composer\IO\IOInterface');
  53. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  54. 'Composer\Test\EventDispatcher\EventDispatcherTest::expectsVariableEvent',
  55. ), $io);
  56. $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
  57. }
  58. /**
  59. * @dataProvider getValidCommands
  60. * @param string $command
  61. */
  62. public function testDispatcherCanExecuteSingleCommandLineScript($command)
  63. {
  64. $process = $this->getMock('Composer\Util\ProcessExecutor');
  65. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  66. ->setConstructorArgs(array(
  67. $this->getMock('Composer\Composer'),
  68. $this->getMock('Composer\IO\IOInterface'),
  69. $process,
  70. ))
  71. ->setMethods(array('getListeners'))
  72. ->getMock();
  73. $listener = array($command);
  74. $dispatcher->expects($this->atLeastOnce())
  75. ->method('getListeners')
  76. ->will($this->returnValue($listener));
  77. $process->expects($this->once())
  78. ->method('execute')
  79. ->with($command)
  80. ->will($this->returnValue(0));
  81. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  82. }
  83. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  84. {
  85. $process = $this->getMock('Composer\Util\ProcessExecutor');
  86. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  87. ->setConstructorArgs(array(
  88. $this->getMock('Composer\Composer'),
  89. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  90. $process,
  91. ))
  92. ->setMethods(array(
  93. 'getListeners',
  94. ))
  95. ->getMock();
  96. $process->expects($this->exactly(2))
  97. ->method('execute')
  98. ->will($this->returnValue(0));
  99. $listeners = array(
  100. 'echo -n foo',
  101. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
  102. 'echo -n bar',
  103. );
  104. $dispatcher->expects($this->atLeastOnce())
  105. ->method('getListeners')
  106. ->will($this->returnValue($listeners));
  107. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  108. $expected = '> post-install-cmd: echo -n foo'.PHP_EOL.
  109. '> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL.
  110. '> post-install-cmd: echo -n bar'.PHP_EOL;
  111. $this->assertEquals($expected, $io->getOutput());
  112. }
  113. public function testDispatcherCanExecuteComposerScriptGroups()
  114. {
  115. $process = $this->getMock('Composer\Util\ProcessExecutor');
  116. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  117. ->setConstructorArgs(array(
  118. $composer = $this->getMock('Composer\Composer'),
  119. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  120. $process,
  121. ))
  122. ->setMethods(array(
  123. 'getListeners',
  124. ))
  125. ->getMock();
  126. $process->expects($this->exactly(3))
  127. ->method('execute')
  128. ->will($this->returnValue(0));
  129. $dispatcher->expects($this->atLeastOnce())
  130. ->method('getListeners')
  131. ->will($this->returnCallback(function (Event $event) {
  132. if ($event->getName() === 'root') {
  133. return array('@group');
  134. } elseif ($event->getName() === 'group') {
  135. return array('echo -n foo', '@subgroup', 'echo -n bar');
  136. } elseif ($event->getName() === 'subgroup') {
  137. return array('echo -n baz');
  138. }
  139. return array();
  140. }));
  141. $dispatcher->dispatch('root', new CommandEvent('root', $composer, $io));
  142. $expected = '> root: @group'.PHP_EOL.
  143. '> group: echo -n foo'.PHP_EOL.
  144. '> group: @subgroup'.PHP_EOL.
  145. '> subgroup: echo -n baz'.PHP_EOL.
  146. '> group: echo -n bar'.PHP_EOL;
  147. $this->assertEquals($expected, $io->getOutput());
  148. }
  149. /**
  150. * @expectedException RuntimeException
  151. */
  152. public function testDispatcherDetectInfiniteRecursion()
  153. {
  154. $process = $this->getMock('Composer\Util\ProcessExecutor');
  155. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  156. ->setConstructorArgs(array(
  157. $composer = $this->getMock('Composer\Composer'),
  158. $io = $this->getMock('Composer\IO\IOInterface'),
  159. $process,
  160. ))
  161. ->setMethods(array(
  162. 'getListeners',
  163. ))
  164. ->getMock();
  165. $dispatcher->expects($this->atLeastOnce())
  166. ->method('getListeners')
  167. ->will($this->returnCallback(function (Event $event) {
  168. if ($event->getName() === 'root') {
  169. return array('@recurse');
  170. } elseif ($event->getName() === 'recurse') {
  171. return array('@root');
  172. }
  173. return array();
  174. }));
  175. $dispatcher->dispatch('root', new CommandEvent('root', $composer, $io));
  176. }
  177. private function getDispatcherStubForListenersTest($listeners, $io)
  178. {
  179. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  180. ->setConstructorArgs(array(
  181. $this->getMock('Composer\Composer'),
  182. $io,
  183. ))
  184. ->setMethods(array('getListeners'))
  185. ->getMock();
  186. $dispatcher->expects($this->atLeastOnce())
  187. ->method('getListeners')
  188. ->will($this->returnValue($listeners));
  189. return $dispatcher;
  190. }
  191. public function getValidCommands()
  192. {
  193. return array(
  194. array('phpunit'),
  195. array('echo foo'),
  196. array('echo -n foo'),
  197. );
  198. }
  199. public function testDispatcherOutputsCommand()
  200. {
  201. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  202. ->setConstructorArgs(array(
  203. $this->getMock('Composer\Composer'),
  204. $io = $this->getMock('Composer\IO\IOInterface'),
  205. new ProcessExecutor,
  206. ))
  207. ->setMethods(array('getListeners'))
  208. ->getMock();
  209. $listener = array('echo foo');
  210. $dispatcher->expects($this->atLeastOnce())
  211. ->method('getListeners')
  212. ->will($this->returnValue($listener));
  213. $io->expects($this->once())
  214. ->method('writeError')
  215. ->with($this->equalTo('> echo foo'));
  216. ob_start();
  217. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  218. $this->assertEquals('foo', trim(ob_get_clean()));
  219. }
  220. public function testDispatcherOutputsErrorOnFailedCommand()
  221. {
  222. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  223. ->setConstructorArgs(array(
  224. $this->getMock('Composer\Composer'),
  225. $io = $this->getMock('Composer\IO\IOInterface'),
  226. new ProcessExecutor,
  227. ))
  228. ->setMethods(array('getListeners'))
  229. ->getMock();
  230. $code = 'exit 1';
  231. $listener = array($code);
  232. $dispatcher->expects($this->atLeastOnce())
  233. ->method('getListeners')
  234. ->will($this->returnValue($listener));
  235. $io->expects($this->at(0))
  236. ->method('isVerbose')
  237. ->willReturn(0);
  238. $io->expects($this->at(1))
  239. ->method('writeError')
  240. ->willReturn('> exit 1');
  241. $io->expects($this->at(2))
  242. ->method('writeError')
  243. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  244. $this->setExpectedException('RuntimeException');
  245. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  246. }
  247. public function testDispatcherInstallerEvents()
  248. {
  249. $process = $this->getMock('Composer\Util\ProcessExecutor');
  250. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  251. ->setConstructorArgs(array(
  252. $this->getMock('Composer\Composer'),
  253. $this->getMock('Composer\IO\IOInterface'),
  254. $process,
  255. ))
  256. ->setMethods(array('getListeners'))
  257. ->getMock();
  258. $dispatcher->expects($this->atLeastOnce())
  259. ->method('getListeners')
  260. ->will($this->returnValue(array()));
  261. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  262. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  263. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  264. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  265. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request);
  266. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $policy, $pool, $installedRepo, $request, array());
  267. }
  268. public static function call()
  269. {
  270. throw new \RuntimeException();
  271. }
  272. public static function expectsCommandEvent(CommandEvent $event)
  273. {
  274. return false;
  275. }
  276. public static function expectsVariableEvent($event)
  277. {
  278. return false;
  279. }
  280. public static function someMethod()
  281. {
  282. return true;
  283. }
  284. }