EventDispatcherTest.php 12 KB

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