EventDispatcherTest.php 8.4 KB

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