EventDispatcherTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $process = $this->getMock('Composer\Util\ProcessExecutor');
  58. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  59. ->setConstructorArgs(array(
  60. $this->getMock('Composer\Composer'),
  61. $this->getMock('Composer\IO\IOInterface'),
  62. $process,
  63. ))
  64. ->setMethods(array(
  65. 'getListeners',
  66. 'executeEventPhpScript',
  67. ))
  68. ->getMock();
  69. $process->expects($this->exactly(2))
  70. ->method('execute');
  71. $listeners = array(
  72. 'echo -n foo',
  73. 'Composer\\Test\\Script\\EventDispatcherTest::someMethod',
  74. 'echo -n bar',
  75. );
  76. $dispatcher->expects($this->atLeastOnce())
  77. ->method('getListeners')
  78. ->will($this->returnValue($listeners));
  79. $dispatcher->expects($this->once())
  80. ->method('executeEventPhpScript')
  81. ->with('Composer\Test\Script\EventDispatcherTest', 'someMethod')
  82. ->will($this->returnValue(true));
  83. $dispatcher->dispatchCommandEvent("post-install-cmd");
  84. }
  85. private function getDispatcherStubForListenersTest($listeners, $io)
  86. {
  87. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  88. ->setConstructorArgs(array(
  89. $this->getMock('Composer\Composer'),
  90. $io,
  91. ))
  92. ->setMethods(array('getListeners'))
  93. ->getMock();
  94. $dispatcher->expects($this->atLeastOnce())
  95. ->method('getListeners')
  96. ->will($this->returnValue($listeners));
  97. return $dispatcher;
  98. }
  99. public function getValidCommands()
  100. {
  101. return array(
  102. array('phpunit'),
  103. array('echo foo'),
  104. array('echo -n foo'),
  105. );
  106. }
  107. public static function call()
  108. {
  109. throw new \RuntimeException();
  110. }
  111. public static function someMethod()
  112. {
  113. return true;
  114. }
  115. }