ImmutableEventDispatcherTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\TestCase;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class ImmutableEventDispatcherTest extends TestCase
  18. {
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $innerDispatcher;
  23. /**
  24. * @var ImmutableEventDispatcher
  25. */
  26. private $dispatcher;
  27. protected function setUp()
  28. {
  29. $this->innerDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
  30. $this->dispatcher = new ImmutableEventDispatcher($this->innerDispatcher);
  31. }
  32. public function testDispatchDelegates()
  33. {
  34. $event = new Event();
  35. $this->innerDispatcher->expects($this->once())
  36. ->method('dispatch')
  37. ->with($event, 'event')
  38. ->willReturn('result');
  39. $this->assertSame('result', $this->dispatcher->dispatch($event, 'event'));
  40. }
  41. public function testGetListenersDelegates()
  42. {
  43. $this->innerDispatcher->expects($this->once())
  44. ->method('getListeners')
  45. ->with('event')
  46. ->willReturn('result');
  47. $this->assertSame('result', $this->dispatcher->getListeners('event'));
  48. }
  49. public function testHasListenersDelegates()
  50. {
  51. $this->innerDispatcher->expects($this->once())
  52. ->method('hasListeners')
  53. ->with('event')
  54. ->willReturn('result');
  55. $this->assertSame('result', $this->dispatcher->hasListeners('event'));
  56. }
  57. /**
  58. * @expectedException \BadMethodCallException
  59. */
  60. public function testAddListenerDisallowed()
  61. {
  62. $this->dispatcher->addListener('event', function () { return 'foo'; });
  63. }
  64. /**
  65. * @expectedException \BadMethodCallException
  66. */
  67. public function testAddSubscriberDisallowed()
  68. {
  69. $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock();
  70. $this->dispatcher->addSubscriber($subscriber);
  71. }
  72. /**
  73. * @expectedException \BadMethodCallException
  74. */
  75. public function testRemoveListenerDisallowed()
  76. {
  77. $this->dispatcher->removeListener('event', function () { return 'foo'; });
  78. }
  79. /**
  80. * @expectedException \BadMethodCallException
  81. */
  82. public function testRemoveSubscriberDisallowed()
  83. {
  84. $subscriber = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventSubscriberInterface')->getMock();
  85. $this->dispatcher->removeSubscriber($subscriber);
  86. }
  87. }