EventDispatcherTest.php 5.9 KB

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