EventDispatcherTest.php 12 KB

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