EventDispatcherTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. private function getDispatcherStubForListenersTest($listeners, $io)
  56. {
  57. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  58. ->setConstructorArgs(array(
  59. $this->getMock('Composer\Composer'),
  60. $io,
  61. ))
  62. ->setMethods(array('getListeners'))
  63. ->getMock();
  64. $dispatcher->expects($this->atLeastOnce())
  65. ->method('getListeners')
  66. ->will($this->returnValue($listeners));
  67. return $dispatcher;
  68. }
  69. public function getValidCommands()
  70. {
  71. return array(
  72. array('phpunit'),
  73. array('echo foo'),
  74. array('echo -n foo'),
  75. );
  76. }
  77. public static function call()
  78. {
  79. throw new \RuntimeException();
  80. }
  81. }