EventDispatcherTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. ->will($this->returnValue(0));
  55. $dispatcher->dispatchCommandEvent("post-install-cmd", false);
  56. }
  57. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  58. {
  59. $process = $this->getMock('Composer\Util\ProcessExecutor');
  60. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  61. ->setConstructorArgs(array(
  62. $this->getMock('Composer\Composer'),
  63. $this->getMock('Composer\IO\IOInterface'),
  64. $process,
  65. ))
  66. ->setMethods(array(
  67. 'getListeners',
  68. 'executeEventPhpScript',
  69. ))
  70. ->getMock();
  71. $process->expects($this->exactly(2))
  72. ->method('execute')
  73. ->will($this->returnValue(0));
  74. $listeners = array(
  75. 'echo -n foo',
  76. 'Composer\\Test\\Script\\EventDispatcherTest::someMethod',
  77. 'echo -n bar',
  78. );
  79. $dispatcher->expects($this->atLeastOnce())
  80. ->method('getListeners')
  81. ->will($this->returnValue($listeners));
  82. $dispatcher->expects($this->once())
  83. ->method('executeEventPhpScript')
  84. ->with('Composer\Test\Script\EventDispatcherTest', 'someMethod')
  85. ->will($this->returnValue(true));
  86. $dispatcher->dispatchCommandEvent("post-install-cmd", false);
  87. }
  88. private function getDispatcherStubForListenersTest($listeners, $io)
  89. {
  90. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  91. ->setConstructorArgs(array(
  92. $this->getMock('Composer\Composer'),
  93. $io,
  94. ))
  95. ->setMethods(array('getListeners'))
  96. ->getMock();
  97. $dispatcher->expects($this->atLeastOnce())
  98. ->method('getListeners')
  99. ->will($this->returnValue($listeners));
  100. return $dispatcher;
  101. }
  102. public function getValidCommands()
  103. {
  104. return array(
  105. array('phpunit'),
  106. array('echo foo'),
  107. array('echo -n foo'),
  108. );
  109. }
  110. public function testDispatcherOutputsCommands()
  111. {
  112. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  113. ->setConstructorArgs(array(
  114. $this->getMock('Composer\Composer'),
  115. $this->getMock('Composer\IO\IOInterface'),
  116. new ProcessExecutor,
  117. ))
  118. ->setMethods(array('getListeners'))
  119. ->getMock();
  120. $listener = array('echo foo');
  121. $dispatcher->expects($this->atLeastOnce())
  122. ->method('getListeners')
  123. ->will($this->returnValue($listener));
  124. ob_start();
  125. $dispatcher->dispatchCommandEvent("post-install-cmd", false);
  126. $this->assertEquals('foo', trim(ob_get_clean()));
  127. }
  128. public function testDispatcherOutputsErrorOnFailedCommand()
  129. {
  130. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  131. ->setConstructorArgs(array(
  132. $this->getMock('Composer\Composer'),
  133. $io = $this->getMock('Composer\IO\IOInterface'),
  134. new ProcessExecutor,
  135. ))
  136. ->setMethods(array('getListeners'))
  137. ->getMock();
  138. $code = 'exit 1';
  139. $listener = array($code);
  140. $dispatcher->expects($this->atLeastOnce())
  141. ->method('getListeners')
  142. ->will($this->returnValue($listener));
  143. $io->expects($this->once())
  144. ->method('write')
  145. ->with($this->equalTo('<error>Script '.$code.' handling the post-install-cmd event returned with an error</error>'));
  146. $this->setExpectedException('RuntimeException');
  147. $dispatcher->dispatchCommandEvent("post-install-cmd", false);
  148. }
  149. public static function call()
  150. {
  151. throw new \RuntimeException();
  152. }
  153. public static function someMethod()
  154. {
  155. return true;
  156. }
  157. }