EventDispatcherTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public function testDispatcherCanExecuteCommandLineScripts()
  32. {
  33. $eventCliCommand = 'phpunit';
  34. $process = $this->getMock('Composer\Util\ProcessExecutor');
  35. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  36. ->setConstructorArgs(array(
  37. $this->getMock('Composer\Composer'),
  38. $this->getMock('Composer\IO\IOInterface'),
  39. $process,
  40. ))
  41. ->setMethods(array('getListeners'))
  42. ->getMock();
  43. $listeners = array($eventCliCommand);
  44. $dispatcher->expects($this->atLeastOnce())
  45. ->method('getListeners')
  46. ->will($this->returnValue($listeners));
  47. $process->expects($this->once())
  48. ->method('execute')
  49. ->with($eventCliCommand);
  50. $dispatcher->dispatchCommandEvent("post-install-cmd");
  51. }
  52. private function getDispatcherStubForListenersTest($listeners, $io)
  53. {
  54. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')
  55. ->setConstructorArgs(array(
  56. $this->getMock('Composer\Composer'),
  57. $io,
  58. ))
  59. ->setMethods(array('getListeners'))
  60. ->getMock();
  61. $dispatcher->expects($this->atLeastOnce())
  62. ->method('getListeners')
  63. ->will($this->returnValue($listeners));
  64. return $dispatcher;
  65. }
  66. public static function call()
  67. {
  68. throw new \RuntimeException();
  69. }
  70. }