ImmutableEventDispatcherTest.php 2.8 KB

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