EventDispatcherTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 testDispatcherRemoveListener()
  147. {
  148. $composer = $this->createComposerInstance();
  149. $composer->setRepositoryManager($this->getRepositoryManagerMockForDevModePassingTest());
  150. $composer->setInstallationManager($this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock());
  151. $dispatcher = new EventDispatcher(
  152. $composer,
  153. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  154. $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock()
  155. );
  156. $listener = array($this, 'someMethod');
  157. $listener2 = array($this, 'someMethod2');
  158. $listener3 = 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod';
  159. $dispatcher->addListener('ev1', $listener, 0);
  160. $dispatcher->addListener('ev1', $listener, 1);
  161. $dispatcher->addListener('ev1', $listener2, 1);
  162. $dispatcher->addListener('ev1', $listener3);
  163. $dispatcher->addListener('ev2', $listener3);
  164. $dispatcher->addListener('ev2', $listener);
  165. $dispatcher->dispatch('ev1');
  166. $dispatcher->dispatch('ev2');
  167. $expected = '> ev1: Composer\Test\EventDispatcher\EventDispatcherTest->someMethod'.PHP_EOL
  168. .'> ev1: Composer\Test\EventDispatcher\EventDispatcherTest->someMethod2'.PHP_EOL
  169. .'> ev1: Composer\Test\EventDispatcher\EventDispatcherTest->someMethod'.PHP_EOL
  170. .'> ev1: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL
  171. .'> ev2: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL
  172. .'> ev2: Composer\Test\EventDispatcher\EventDispatcherTest->someMethod'.PHP_EOL;
  173. $this->assertEquals($expected, $io->getOutput());
  174. $dispatcher->removeListener($this);
  175. $dispatcher->dispatch('ev1');
  176. $dispatcher->dispatch('ev2');
  177. $expected .= '> ev1: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL
  178. .'> ev2: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL;
  179. $this->assertEquals($expected, $io->getOutput());
  180. }
  181. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  182. {
  183. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  184. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  185. ->setConstructorArgs(array(
  186. $this->createComposerInstance(),
  187. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  188. $process,
  189. ))
  190. ->setMethods(array(
  191. 'getListeners',
  192. ))
  193. ->getMock();
  194. $process->expects($this->exactly(2))
  195. ->method('execute')
  196. ->will($this->returnValue(0));
  197. $listeners = array(
  198. 'echo -n foo',
  199. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
  200. 'echo -n bar',
  201. );
  202. $dispatcher->expects($this->atLeastOnce())
  203. ->method('getListeners')
  204. ->will($this->returnValue($listeners));
  205. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  206. $expected = '> post-install-cmd: echo -n foo'.PHP_EOL.
  207. '> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::someMethod'.PHP_EOL.
  208. '> post-install-cmd: echo -n bar'.PHP_EOL;
  209. $this->assertEquals($expected, $io->getOutput());
  210. }
  211. public function testDispatcherCanPutEnv()
  212. {
  213. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  214. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  215. ->setConstructorArgs(array(
  216. $this->createComposerInstance(),
  217. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  218. $process,
  219. ))
  220. ->setMethods(array(
  221. 'getListeners',
  222. ))
  223. ->getMock();
  224. $listeners = array(
  225. '@putenv ABC=123',
  226. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::getTestEnv',
  227. );
  228. $dispatcher->expects($this->atLeastOnce())
  229. ->method('getListeners')
  230. ->will($this->returnValue($listeners));
  231. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  232. $expected = '> post-install-cmd: @putenv ABC=123'.PHP_EOL.
  233. '> post-install-cmd: Composer\Test\EventDispatcher\EventDispatcherTest::getTestEnv'.PHP_EOL;
  234. $this->assertEquals($expected, $io->getOutput());
  235. }
  236. public function testDispatcherAppendsDirBinOnPathForEveryListener()
  237. {
  238. $currentDirectoryBkp = getcwd();
  239. $composerBinDirBkp = getenv('COMPOSER_BIN_DIR');
  240. chdir(__DIR__);
  241. putenv('COMPOSER_BIN_DIR=' . __DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
  242. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  243. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->setConstructorArgs(array(
  244. $this->createComposerInstance(),
  245. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  246. $process,
  247. ))->setMethods(array(
  248. 'getListeners',
  249. ))->getMock();
  250. $listeners = array(
  251. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::createsVendorBinFolderChecksEnvDoesNotContainsBin',
  252. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::createsVendorBinFolderChecksEnvContainsBin',
  253. );
  254. $dispatcher->expects($this->atLeastOnce())->method('getListeners')->will($this->returnValue($listeners));
  255. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  256. rmdir(__DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
  257. rmdir(__DIR__ . sprintf('%svendor', DIRECTORY_SEPARATOR));
  258. chdir($currentDirectoryBkp);
  259. putenv('COMPOSER_BIN_DIR' . ($composerBinDirBkp === false ? '' : '=' . $composerBinDirBkp));
  260. }
  261. static public function createsVendorBinFolderChecksEnvDoesNotContainsBin()
  262. {
  263. mkdir(__DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), 0700, true);
  264. $val = getenv('PATH');
  265. if (!$val) {
  266. $val = getenv('Path');
  267. }
  268. self::assertFalse(strpos($val, __DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR)));
  269. }
  270. static public function createsVendorBinFolderChecksEnvContainsBin()
  271. {
  272. $val = getenv('PATH');
  273. if (!$val) {
  274. $val = getenv('Path');
  275. }
  276. self::assertNotFalse(strpos($val, __DIR__ . sprintf('%svendor%sbin', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR)));
  277. }
  278. static public function getTestEnv() {
  279. $val = getenv('ABC');
  280. if ($val !== '123') {
  281. throw new \Exception('getenv() did not return the expected value. expected 123 got '. var_export($val, true));
  282. }
  283. }
  284. public function testDispatcherCanExecuteComposerScriptGroups()
  285. {
  286. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  287. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  288. ->setConstructorArgs(array(
  289. $composer = $this->createComposerInstance(),
  290. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  291. $process,
  292. ))
  293. ->setMethods(array(
  294. 'getListeners',
  295. ))
  296. ->getMock();
  297. $process->expects($this->exactly(3))
  298. ->method('execute')
  299. ->will($this->returnValue(0));
  300. $dispatcher->expects($this->atLeastOnce())
  301. ->method('getListeners')
  302. ->will($this->returnCallback(function (Event $event) {
  303. if ($event->getName() === 'root') {
  304. return array('@group');
  305. }
  306. if ($event->getName() === 'group') {
  307. return array('echo -n foo', '@subgroup', 'echo -n bar');
  308. }
  309. if ($event->getName() === 'subgroup') {
  310. return array('echo -n baz');
  311. }
  312. return array();
  313. }));
  314. $dispatcher->dispatch('root', new ScriptEvent('root', $composer, $io));
  315. $expected = '> root: @group'.PHP_EOL.
  316. '> group: echo -n foo'.PHP_EOL.
  317. '> group: @subgroup'.PHP_EOL.
  318. '> subgroup: echo -n baz'.PHP_EOL.
  319. '> group: echo -n bar'.PHP_EOL;
  320. $this->assertEquals($expected, $io->getOutput());
  321. }
  322. public function testRecursionInScriptsNames()
  323. {
  324. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  325. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  326. ->setConstructorArgs(array(
  327. $composer = $this->createComposerInstance(),
  328. $io = new BufferIO('', OutputInterface::VERBOSITY_VERBOSE),
  329. $process
  330. ))
  331. ->setMethods(array(
  332. 'getListeners'
  333. ))
  334. ->getMock();
  335. $process->expects($this->exactly(1))
  336. ->method('execute')
  337. ->will($this->returnValue(0));
  338. $dispatcher->expects($this->atLeastOnce())
  339. ->method('getListeners')
  340. ->will($this->returnCallback(function (Event $event) {
  341. if($event->getName() === 'hello') {
  342. return array('echo Hello');
  343. }
  344. if($event->getName() === 'helloWorld') {
  345. return array('@hello World');
  346. }
  347. return array();
  348. }));
  349. $dispatcher->dispatch('helloWorld', new ScriptEvent('helloWorld', $composer, $io));
  350. $expected = "> helloWorld: @hello World".PHP_EOL.
  351. "> hello: echo Hello " .escapeshellarg('World').PHP_EOL;
  352. $this->assertEquals($expected, $io->getOutput());
  353. }
  354. /**
  355. * @expectedException RuntimeException
  356. */
  357. public function testDispatcherDetectInfiniteRecursion()
  358. {
  359. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  360. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  361. ->setConstructorArgs(array(
  362. $composer = $this->createComposerInstance(),
  363. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  364. $process,
  365. ))
  366. ->setMethods(array(
  367. 'getListeners',
  368. ))
  369. ->getMock();
  370. $dispatcher->expects($this->atLeastOnce())
  371. ->method('getListeners')
  372. ->will($this->returnCallback(function (Event $event) {
  373. if ($event->getName() === 'root') {
  374. return array('@recurse');
  375. }
  376. if ($event->getName() === 'recurse') {
  377. return array('@root');
  378. }
  379. return array();
  380. }));
  381. $dispatcher->dispatch('root', new ScriptEvent('root', $composer, $io));
  382. }
  383. private function getDispatcherStubForListenersTest($listeners, $io)
  384. {
  385. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  386. ->setConstructorArgs(array(
  387. $this->createComposerInstance(),
  388. $io,
  389. ))
  390. ->setMethods(array('getListeners'))
  391. ->getMock();
  392. $dispatcher->expects($this->atLeastOnce())
  393. ->method('getListeners')
  394. ->will($this->returnValue($listeners));
  395. return $dispatcher;
  396. }
  397. public function getValidCommands()
  398. {
  399. return array(
  400. array('phpunit'),
  401. array('echo foo'),
  402. array('echo -n foo'),
  403. );
  404. }
  405. public function testDispatcherOutputsCommand()
  406. {
  407. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  408. ->setConstructorArgs(array(
  409. $this->createComposerInstance(),
  410. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  411. new ProcessExecutor($io),
  412. ))
  413. ->setMethods(array('getListeners'))
  414. ->getMock();
  415. $listener = array('echo foo');
  416. $dispatcher->expects($this->atLeastOnce())
  417. ->method('getListeners')
  418. ->will($this->returnValue($listener));
  419. $io->expects($this->once())
  420. ->method('writeError')
  421. ->with($this->equalTo('> echo foo'));
  422. $io->expects($this->once())
  423. ->method('writeRaw')
  424. ->with($this->equalTo('foo'.PHP_EOL), false);
  425. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  426. }
  427. public function testDispatcherOutputsErrorOnFailedCommand()
  428. {
  429. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  430. ->setConstructorArgs(array(
  431. $this->createComposerInstance(),
  432. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  433. new ProcessExecutor,
  434. ))
  435. ->setMethods(array('getListeners'))
  436. ->getMock();
  437. $code = 'exit 1';
  438. $listener = array($code);
  439. $dispatcher->expects($this->atLeastOnce())
  440. ->method('getListeners')
  441. ->will($this->returnValue($listener));
  442. $io->expects($this->at(0))
  443. ->method('isVerbose')
  444. ->willReturn(0);
  445. $io->expects($this->at(1))
  446. ->method('writeError')
  447. ->willReturn('> exit 1');
  448. $io->expects($this->at(2))
  449. ->method('isInteractive')
  450. ->willReturn(1);
  451. $io->expects($this->at(3))
  452. ->method('writeError')
  453. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with error code 1</error>'));
  454. $this->setExpectedException('RuntimeException');
  455. $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false);
  456. }
  457. public function testDispatcherInstallerEvents()
  458. {
  459. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  460. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  461. ->setConstructorArgs(array(
  462. $this->createComposerInstance(),
  463. $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  464. $process,
  465. ))
  466. ->setMethods(array('getListeners'))
  467. ->getMock();
  468. $dispatcher->expects($this->atLeastOnce())
  469. ->method('getListeners')
  470. ->will($this->returnValue(array()));
  471. $transaction = $this->getMockBuilder('Composer\DependencyResolver\LockTransaction')->disableOriginalConstructor()->getMock();
  472. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_OPERATIONS_EXEC, true, true, $transaction);
  473. }
  474. public static function call()
  475. {
  476. throw new \RuntimeException();
  477. }
  478. public static function someMethod()
  479. {
  480. return true;
  481. }
  482. public static function someMethod2()
  483. {
  484. return true;
  485. }
  486. private function createComposerInstance()
  487. {
  488. $composer = new Composer;
  489. $config = new Config;
  490. $composer->setConfig($config);
  491. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
  492. $composer->setPackage($package);
  493. return $composer;
  494. }
  495. }