ImmutableEventDispatcherTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher\Tests;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class ImmutableEventDispatcherTest extends TestCase
  19. {
  20. /**
  21. * @var MockObject
  22. */
  23. private $innerDispatcher;
  24. /**
  25. * @var ImmutableEventDispatcher
  26. */
  27. private $dispatcher;
  28. protected function setUp()
  29. {
  30. $this->innerDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  31. $this->dispatcher = new ImmutableEventDispatcher($this->innerDispatcher);
  32. }
  33. public function testDispatchDelegates()
  34. {
  35. $event = new Event();
  36. $resultEvent = new Event();
  37. $this->innerDispatcher->expects($this->once())
  38. ->method('dispatch')
  39. ->with('event', $event)
  40. ->willReturn($resultEvent);
  41. $this->assertSame($resultEvent, $this->dispatcher->dispatch('event', $event));
  42. }
  43. public function testGetListenersDelegates()
  44. {
  45. $this->innerDispatcher->expects($this->once())
  46. ->method('getListeners')
  47. ->with('event')
  48. ->willReturn(['result']);
  49. $this->assertSame(['result'], $this->dispatcher->getListeners('event'));
  50. }
  51. public function testHasListenersDelegates()
  52. {
  53. $this->innerDispatcher->expects($this->once())
  54. ->method('hasListeners')
  55. ->with('event')
  56. ->willReturn(true);
  57. $this->assertTrue($this->dispatcher->hasListeners('event'));
  58. }
  59. public function testAddListenerDisallowed()
  60. {
  61. $this->expectException('\BadMethodCallException');
  62. $this->dispatcher->addListener('event', function () { return 'foo'; });
  63. }
  64. public function testAddSubscriberDisallowed()
  65. {
  66. $this->expectException('\BadMethodCallException');
  67. $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock();
  68. $this->dispatcher->addSubscriber($subscriber);
  69. }
  70. public function testRemoveListenerDisallowed()
  71. {
  72. $this->expectException('\BadMethodCallException');
  73. $this->dispatcher->removeListener('event', function () { return 'foo'; });
  74. }
  75. public function testRemoveSubscriberDisallowed()
  76. {
  77. $this->expectException('\BadMethodCallException');
  78. $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock();
  79. $this->dispatcher->removeSubscriber($subscriber);
  80. }
  81. }