EventDispatcherTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. class EventDispatcherTest extends TestCase
  16. {
  17. /**
  18. * @expectedException RuntimeException
  19. */
  20. public function testListenerExceptionsAreCaught()
  21. {
  22. $io = $this->getMock('Composer\IO\IOInterface');
  23. $dispatcher = $this->getDispatcherStubForListenersTest(array(
  24. "Composer\Test\Script\EventDispatcherTest::call"
  25. ), $io);
  26. $io->expects($this->once())
  27. ->method('write')
  28. ->with('<error>Script Composer\Test\Script\EventDispatcherTest::call handling the post-install-cmd event terminated with an exception</error>');
  29. $dispatcher->dispatchCommandEvent("post-install-cmd");
  30. }
  31. /**
  32. * @dataProvider getValidCommands
  33. * @param string $command
  34. */
  35. public function testDispatcherCanExecuteSingleCommandLineScript($command)
  36. {
  37. $process = $this->getMock('Composer\Util\ProcessExecutor');
  38. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  39. ->setConstructorArgs(array(
  40. $this->getMock('Composer\Composer'),
  41. $this->getMock('Composer\IO\IOInterface'),
  42. $process,
  43. ))
  44. ->setMethods(array('getListeners'))
  45. ->getMock();
  46. $listener = array($command);
  47. $dispatcher->expects($this->atLeastOnce())
  48. ->method('getListeners')
  49. ->will($this->returnValue($listener));
  50. $process->expects($this->once())
  51. ->method('execute')
  52. ->with($command);
  53. $dispatcher->dispatchCommandEvent("post-install-cmd");
  54. }
  55. public function testDispatcherCanExecuteCliAndPhpInSameEventScriptStack()
  56. {
  57. $io = $this->getMock('Composer\IO\IOInterface');
  58. $process = $this->getMock('Composer\Util\ProcessExecutor');
  59. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  60. ->setConstructorArgs(array(
  61. $this->getMock('Composer\Composer'),
  62. $io,
  63. $process,
  64. ))
  65. ->setMethods(array(
  66. 'getListeners',
  67. 'executeEventPhpScript',
  68. ))
  69. ->getMock();
  70. $io->expects($this->never())
  71. ->method('write');
  72. $process->expects($this->exactly(2))
  73. ->method('execute');
  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");
  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 static function call()
  111. {
  112. throw new \RuntimeException();
  113. }
  114. public static function someMethod()
  115. {
  116. return true;
  117. }
  118. }