EventDispatcherTest.php 7.4 KB

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