EventDispatcherTest.php 8.4 KB

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