EventDispatcherTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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\EventDispatcher\EventDispatcher;
  14. use Composer\EventDispatcher\ScriptExecutionException;
  15. use Composer\Installer\InstallerEvents;
  16. use Composer\Config;
  17. use Composer\Composer;
  18. use Composer\Test\TestCase;
  19. use Composer\IO\BufferIO;
  20. use Composer\Script\ScriptEvents;
  21. use Composer\Script\Event as ScriptEvent;
  22. use Composer\Util\ProcessExecutor;
  23. use Symfony\Component\Console\Output\OutputInterface;
  24. class EventDispatcherTest extends TestCase
  25. {
  26. /**
  27. * @expectedException RuntimeException
  28. */
  29. public function testListenerExceptionsAreCaught()
  30. {
  31. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  32. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  33. 'Composer\Test\EventDispatcher\EventDispatcherTest::call',
  34. ), $io);
  35. $io->expects($this->at(0))
  36. ->method('isVerbose')
  37. ->willReturn(0);
  38. $io->expects($this->at(1))
  39. ->method('writeError')
  40. ->with('> Composer\Test\EventDispatcher\EventDispatcherTest::call');
  41. $io->expects($this->at(2))
  42. ->method('writeError')
  43. ->with('<error>Script Composer\Test\EventDispatcher\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception</error>');
  44. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  45. }
  46. /**
  47. * @dataProvider getValidCommands
  48. * @param string $command
  49. */
  50. public function testDispatcherCanExecuteSingleCommandLineScript($command)
  51. {
  52. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  53. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  54. ->setConstructorArgs(array(
  55. $this->createComposerInstance(),
  56. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  57. $process,
  58. ))
  59. ->setMethods(array('getListeners'))
  60. ->getMock();
  61. $listener = array($command);
  62. $dispatcher->expects($this->atLeastOnce())
  63. ->method('getListeners')
  64. ->will($this->returnValue($listener));
  65. $process->expects($this->once())
  66. ->method('execute')
  67. ->with($command)
  68. ->will($this->returnValue(0));
  69. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  70. }
  71. /**
  72. * @dataProvider getDevModes
  73. * @param bool $devMode
  74. */
  75. public function testDispatcherPassDevModeToAutoloadGeneratorForScriptEvents($devMode)
  76. {
  77. $composer = $this->createComposerInstance();
  78. $generator = $this->getGeneratorMockForDevModePassingTest();
  79. $generator->expects($this->atLeastOnce())
  80. ->method('setDevMode')
  81. ->with($devMode);
  82. $composer->setAutoloadGenerator($generator);
  83. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  84. $package->method('getScripts')->will($this->returnValue(array('scriptName' => array('scriptName'))));
  85. $composer->setPackage($package);
  86. $composer->setRepositoryManager($this->getRepositoryManagerMockForDevModePassingTest());
  87. $composer->setInstallationManager($this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock());
  88. $dispatcher = new EventDispatcher(
  89. $composer,
  90. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  91. $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock()
  92. );
  93. $event = $this->getMockBuilder('Composer\Script\Event')
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $event->method('getName')->will($this->returnValue('scriptName'));
  97. $event->expects($this->atLeastOnce())
  98. ->method('isDevMode')
  99. ->will($this->returnValue($devMode));
  100. $dispatcher->hasEventListeners($event);
  101. }
  102. public function getDevModes()
  103. {
  104. return array(
  105. array(true),
  106. array(false),
  107. );
  108. }
  109. private function getGeneratorMockForDevModePassingTest()
  110. {
  111. $generator = $this->getMockBuilder('Composer\Autoload\AutoloadGenerator')
  112. ->disableOriginalConstructor()
  113. ->setMethods(array(
  114. 'buildPackageMap',
  115. 'parseAutoloads',
  116. 'createLoader',
  117. 'setDevMode',
  118. ))
  119. ->getMock();
  120. $generator
  121. ->method('buildPackageMap')
  122. ->will($this->returnValue(array()));
  123. $generator
  124. ->method('parseAutoloads')
  125. ->will($this->returnValue(array()));
  126. $generator
  127. ->method('createLoader')
  128. ->will($this->returnValue($this->getMockBuilder('Composer\Autoload\ClassLoader')->getMock()));
  129. return $generator;
  130. }
  131. private function getRepositoryManagerMockForDevModePassingTest()
  132. {
  133. $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  134. ->disableOriginalConstructor()
  135. ->setMethods(array('getLocalRepository'))
  136. ->getMock();
  137. $repo = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
  138. $repo
  139. ->method('getCanonicalPackages')
  140. ->will($this->returnValue(array()));
  141. $rm
  142. ->method('getLocalRepository')
  143. ->will($this->returnValue($repo));
  144. return $rm;
  145. }
  146. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  147. {
  148. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  149. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  150. ->setConstructorArgs(array(
  151. $this->createComposerInstance(),
  152. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  153. $process,
  154. ))
  155. ->setMethods(array(
  156. 'getListeners',
  157. ))
  158. ->getMock();
  159. $process->expects($this->exactly(2))
  160. ->method('execute')
  161. ->will($this->returnValue(0));
  162. $listeners = array(
  163. 'echo -n foo',
  164. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
  165. 'echo -n bar',
  166. );
  167. $dispatcher->expects($this->atLeastOnce())
  168. ->method('getListeners')
  169. ->will($this->returnValue($listeners));
  170. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  171. $expected = '> post-install-cmd: echo -n foo'.PHP_EOL.
  172. '> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL.
  173. '> post-install-cmd: echo -n bar'.PHP_EOL;
  174. $this->assertEquals($expected, $io->getOutput());
  175. }
  176. public function testDispatcherCanExecuteComposerScriptGroups()
  177. {
  178. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  179. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  180. ->setConstructorArgs(array(
  181. $composer = $this->createComposerInstance(),
  182. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  183. $process,
  184. ))
  185. ->setMethods(array(
  186. 'getListeners',
  187. ))
  188. ->getMock();
  189. $process->expects($this->exactly(3))
  190. ->method('execute')
  191. ->will($this->returnValue(0));
  192. $dispatcher->expects($this->atLeastOnce())
  193. ->method('getListeners')
  194. ->will($this->returnCallback(function (Event $event) {
  195. if ($event->getName() === 'root') {
  196. return array('@group');
  197. }
  198. if ($event->getName() === 'group') {
  199. return array('echo -n foo', '@subgroup', 'echo -n bar');
  200. }
  201. if ($event->getName() === 'subgroup') {
  202. return array('echo -n baz');
  203. }
  204. return array();
  205. }));
  206. $dispatcher->dispatch('root', new ScriptEvent('root', $composer, $io));
  207. $expected = '> root: @group'.PHP_EOL.
  208. '> group: echo -n foo'.PHP_EOL.
  209. '> group: @subgroup'.PHP_EOL.
  210. '> subgroup: echo -n baz'.PHP_EOL.
  211. '> group: echo -n bar'.PHP_EOL;
  212. $this->assertEquals($expected, $io->getOutput());
  213. }
  214. public function testRecursionInScriptsNames()
  215. {
  216. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  217. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  218. ->setConstructorArgs(array(
  219. $composer = $this->createComposerInstance(),
  220. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  221. $process
  222. ))
  223. ->setMethods(array(
  224. 'getListeners'
  225. ))
  226. ->getMock();
  227. $process->expects($this->exactly(1))
  228. ->method('execute')
  229. ->will($this->returnValue(0));
  230. $dispatcher->expects($this->atLeastOnce())
  231. ->method('getListeners')
  232. ->will($this->returnCallback(function (Event $event) {
  233. if($event->getName() === 'hello') {
  234. return array('echo Hello');
  235. }
  236. if($event->getName() === 'helloWorld') {
  237. return array('@hello World');
  238. }
  239. return array();
  240. }));
  241. $dispatcher->dispatch('helloWorld', new ScriptEvent('helloWorld', $composer, $io));
  242. $expected = "> helloWorld: @hello World".PHP_EOL.
  243. "> hello: echo Hello " .escapeshellarg('World').PHP_EOL;
  244. $this->assertEquals($expected, $io->getOutput());
  245. }
  246. /**
  247. * @expectedException RuntimeException
  248. */
  249. public function testDispatcherDetectInfiniteRecursion()
  250. {
  251. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  252. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  253. ->setConstructorArgs(array(
  254. $composer = $this->createComposerInstance(),
  255. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  256. $process,
  257. ))
  258. ->setMethods(array(
  259. 'getListeners',
  260. ))
  261. ->getMock();
  262. $dispatcher->expects($this->atLeastOnce())
  263. ->method('getListeners')
  264. ->will($this->returnCallback(function (Event $event) {
  265. if ($event->getName() === 'root') {
  266. return array('@recurse');
  267. }
  268. if ($event->getName() === 'recurse') {
  269. return array('@root');
  270. }
  271. return array();
  272. }));
  273. $dispatcher->dispatch('root', new ScriptEvent('root', $composer, $io));
  274. }
  275. private function getDispatcherStubForListenersTest($listeners, $io)
  276. {
  277. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  278. ->setConstructorArgs(array(
  279. $this->createComposerInstance(),
  280. $io,
  281. ))
  282. ->setMethods(array('getListeners'))
  283. ->getMock();
  284. $dispatcher->expects($this->atLeastOnce())
  285. ->method('getListeners')
  286. ->will($this->returnValue($listeners));
  287. return $dispatcher;
  288. }
  289. public function getValidCommands()
  290. {
  291. return array(
  292. array('phpunit'),
  293. array('echo foo'),
  294. array('echo -n foo'),
  295. );
  296. }
  297. public function testDispatcherOutputsCommand()
  298. {
  299. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  300. ->setConstructorArgs(array(
  301. $this->createComposerInstance(),
  302. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  303. new ProcessExecutor($io),
  304. ))
  305. ->setMethods(array('getListeners'))
  306. ->getMock();
  307. $listener = array('echo foo');
  308. $dispatcher->expects($this->atLeastOnce())
  309. ->method('getListeners')
  310. ->will($this->returnValue($listener));
  311. $io->expects($this->once())
  312. ->method('writeError')
  313. ->with($this->equalTo('> echo foo'));
  314. $io->expects($this->once())
  315. ->method('write')
  316. ->with($this->equalTo('foo'.PHP_EOL), false);
  317. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  318. }
  319. public function testDispatcherOutputsErrorOnFailedCommand()
  320. {
  321. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  322. ->setConstructorArgs(array(
  323. $this->createComposerInstance(),
  324. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  325. new ProcessExecutor,
  326. ))
  327. ->setMethods(array('getListeners'))
  328. ->getMock();
  329. $code = 'exit 1';
  330. $listener = array($code);
  331. $dispatcher->expects($this->atLeastOnce())
  332. ->method('getListeners')
  333. ->will($this->returnValue($listener));
  334. $io->expects($this->at(0))
  335. ->method('isVerbose')
  336. ->willReturn(0);
  337. $io->expects($this->at(1))
  338. ->method('writeError')
  339. ->willReturn('> exit 1');
  340. $io->expects($this->at(2))
  341. ->method('writeError')
  342. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with error code 1</error>'));
  343. $this->setExpectedException('RuntimeException');
  344. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  345. }
  346. public function testDispatcherInstallerEvents()
  347. {
  348. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  349. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  350. ->setConstructorArgs(array(
  351. $this->createComposerInstance(),
  352. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  353. $process,
  354. ))
  355. ->setMethods(array('getListeners'))
  356. ->getMock();
  357. $dispatcher->expects($this->atLeastOnce())
  358. ->method('getListeners')
  359. ->will($this->returnValue(array()));
  360. $policy = $this->getMockBuilder('Composer\DependencyResolver\PolicyInterface')->getMock();
  361. $repositorySet = $this->getMockBuilder('Composer\Repository\RepositorySet')->disableOriginalConstructor()->getMock();
  362. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  363. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  364. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, true, $policy, $repositorySet, $installedRepo, $request);
  365. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, true, $policy, $repositorySet, $installedRepo, $request, array());
  366. }
  367. public static function call()
  368. {
  369. throw new \RuntimeException();
  370. }
  371. public static function someMethod()
  372. {
  373. return true;
  374. }
  375. private function createComposerInstance()
  376. {
  377. $composer = new Composer;
  378. $config = new Config;
  379. $composer->setConfig($config);
  380. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  381. $composer->setPackage($package);
  382. return $composer;
  383. }
  384. }