EventDispatcherTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Installer\InstallerEvents;
  15. use Composer\TestCase;
  16. use Composer\Script\ScriptEvents;
  17. use Composer\Script\CommandEvent;
  18. use Composer\Util\ProcessExecutor;
  19. class EventDispatcherTest extends TestCase
  20. {
  21. /**
  22. * @expectedException RuntimeException
  23. */
  24. public function testListenerExceptionsAreCaught()
  25. {
  26. $io = $this->getMock('Composer\IO\IOInterface');
  27. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  28. "Composer\Test\EventDispatcher\EventDispatcherTest::call"
  29. ), $io);
  30. $io->expects($this->once())
  31. ->method('write')
  32. ->with('<error>Script Composer\Test\EventDispatcher\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception</error>');
  33. $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
  34. }
  35. public function testDispatcherCanConvertScriptEventToCommandEventForListener()
  36. {
  37. $io = $this->getMock('Composer\IO\IOInterface');
  38. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  39. "Composer\Test\EventDispatcher\EventDispatcherTest::convertEvent"
  40. ), $io);
  41. $this->assertEquals(1, $dispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, false));
  42. }
  43. /**
  44. * @dataProvider getValidCommands
  45. * @param string $command
  46. */
  47. public function testDispatcherCanExecuteSingleCommandLineScript($command)
  48. {
  49. $process = $this->getMock('Composer\Util\ProcessExecutor');
  50. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  51. ->setConstructorArgs(array(
  52. $this->getMock('Composer\Composer'),
  53. $this->getMock('Composer\IO\IOInterface'),
  54. $process,
  55. ))
  56. ->setMethods(array('getListeners'))
  57. ->getMock();
  58. $listener = array($command);
  59. $dispatcher->expects($this->atLeastOnce())
  60. ->method('getListeners')
  61. ->will($this->returnValue($listener));
  62. $process->expects($this->once())
  63. ->method('execute')
  64. ->with($command)
  65. ->will($this->returnValue(0));
  66. $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
  67. }
  68. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  69. {
  70. $process = $this->getMock('Composer\Util\ProcessExecutor');
  71. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  72. ->setConstructorArgs(array(
  73. $this->getMock('Composer\Composer'),
  74. $this->getMock('Composer\IO\IOInterface'),
  75. $process,
  76. ))
  77. ->setMethods(array(
  78. 'getListeners',
  79. 'executeEventPhpScript',
  80. ))
  81. ->getMock();
  82. $process->expects($this->exactly(2))
  83. ->method('execute')
  84. ->will($this->returnValue(0));
  85. $listeners = array(
  86. 'echo -n foo',
  87. 'Composer\\Test\\EventDispatcher\\EventDispatcherTest::someMethod',
  88. 'echo -n bar',
  89. );
  90. $dispatcher->expects($this->atLeastOnce())
  91. ->method('getListeners')
  92. ->will($this->returnValue($listeners));
  93. $dispatcher->expects($this->once())
  94. ->method('executeEventPhpScript')
  95. ->with('Composer\Test\EventDispatcher\EventDispatcherTest', 'someMethod')
  96. ->will($this->returnValue(true));
  97. $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
  98. }
  99. private function getDispatcherStubForListenersTest($listeners, $io)
  100. {
  101. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  102. ->setConstructorArgs(array(
  103. $this->getMock('Composer\Composer'),
  104. $io,
  105. ))
  106. ->setMethods(array('getListeners'))
  107. ->getMock();
  108. $dispatcher->expects($this->atLeastOnce())
  109. ->method('getListeners')
  110. ->will($this->returnValue($listeners));
  111. return $dispatcher;
  112. }
  113. public function getValidCommands()
  114. {
  115. return array(
  116. array('phpunit'),
  117. array('echo foo'),
  118. array('echo -n foo'),
  119. );
  120. }
  121. public function testDispatcherOutputsCommands()
  122. {
  123. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  124. ->setConstructorArgs(array(
  125. $this->getMock('Composer\Composer'),
  126. $this->getMock('Composer\IO\IOInterface'),
  127. new ProcessExecutor,
  128. ))
  129. ->setMethods(array('getListeners'))
  130. ->getMock();
  131. $listener = array('echo foo');
  132. $dispatcher->expects($this->atLeastOnce())
  133. ->method('getListeners')
  134. ->will($this->returnValue($listener));
  135. ob_start();
  136. $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
  137. $this->assertEquals('foo', trim(ob_get_clean()));
  138. }
  139. public function testDispatcherOutputsErrorOnFailedCommand()
  140. {
  141. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  142. ->setConstructorArgs(array(
  143. $this->getMock('Composer\Composer'),
  144. $io = $this->getMock('Composer\IO\IOInterface'),
  145. new ProcessExecutor,
  146. ))
  147. ->setMethods(array('getListeners'))
  148. ->getMock();
  149. $code = 'exit 1';
  150. $listener = array($code);
  151. $dispatcher->expects($this->atLeastOnce())
  152. ->method('getListeners')
  153. ->will($this->returnValue($listener));
  154. $io->expects($this->once())
  155. ->method('write')
  156. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  157. $this->setExpectedException('RuntimeException');
  158. $dispatcher->dispatchCommandEvent(ScriptEvents::POST_INSTALL_CMD, false);
  159. }
  160. public function testDispatcherInstallerEvents()
  161. {
  162. $process = $this->getMock('Composer\Util\ProcessExecutor');
  163. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  164. ->setConstructorArgs(array(
  165. $this->getMock('Composer\Composer'),
  166. $this->getMock('Composer\IO\IOInterface'),
  167. $process,
  168. ))
  169. ->setMethods(array('getListeners'))
  170. ->getMock();
  171. $dispatcher->expects($this->atLeastOnce())
  172. ->method('getListeners')
  173. ->will($this->returnValue(array()));
  174. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  175. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  176. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  177. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  178. $dispatcher->dispatchInstallerEvent(InstallerEvents::PRE_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request);
  179. $dispatcher->dispatchInstallerEvent(InstallerEvents::POST_DEPENDENCIES_SOLVING, $policy, $pool, $installedRepo, $request, array());
  180. }
  181. public static function call()
  182. {
  183. throw new \RuntimeException();
  184. }
  185. public static function convertEvent(CommandEvent $event)
  186. {
  187. return false;
  188. }
  189. public static function someMethod()
  190. {
  191. return true;
  192. }
  193. }