EventDispatcherTest.php 5.7 KB

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