EventManagerTest.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Doctrine\Tests\Common;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\Common\EventArgs;
  5. class EventManagerTest extends \Doctrine\Tests\DoctrineTestCase
  6. {
  7. /* Some pseudo events */
  8. const preFoo = 'preFoo';
  9. const postFoo = 'postFoo';
  10. const preBar = 'preBar';
  11. const postBar = 'postBar';
  12. private $_preFooInvoked = false;
  13. private $_postFooInvoked = false;
  14. private $_eventManager;
  15. protected function setUp()
  16. {
  17. $this->_eventManager = new EventManager;
  18. $this->_preFooInvoked = false;
  19. $this->_postFooInvoked = false;
  20. }
  21. public function testInitialState()
  22. {
  23. $this->assertEquals(array(), $this->_eventManager->getListeners());
  24. $this->assertFalse($this->_eventManager->hasListeners(self::preFoo));
  25. $this->assertFalse($this->_eventManager->hasListeners(self::postFoo));
  26. }
  27. public function testAddEventListener()
  28. {
  29. $this->_eventManager->addEventListener(array('preFoo', 'postFoo'), $this);
  30. $this->assertTrue($this->_eventManager->hasListeners(self::preFoo));
  31. $this->assertTrue($this->_eventManager->hasListeners(self::postFoo));
  32. $this->assertEquals(1, count($this->_eventManager->getListeners(self::preFoo)));
  33. $this->assertEquals(1, count($this->_eventManager->getListeners(self::postFoo)));
  34. $this->assertEquals(2, count($this->_eventManager->getListeners()));
  35. }
  36. public function testDispatchEvent()
  37. {
  38. $this->_eventManager->addEventListener(array('preFoo', 'postFoo'), $this);
  39. $this->_eventManager->dispatchEvent(self::preFoo);
  40. $this->assertTrue($this->_preFooInvoked);
  41. $this->assertFalse($this->_postFooInvoked);
  42. }
  43. public function testRemoveEventListener()
  44. {
  45. $this->_eventManager->addEventListener(array('preBar'), $this);
  46. $this->assertTrue($this->_eventManager->hasListeners(self::preBar));
  47. $this->_eventManager->removeEventListener(array('preBar'), $this);
  48. $this->assertFalse($this->_eventManager->hasListeners(self::preBar));
  49. }
  50. public function testAddEventSubscriber()
  51. {
  52. $eventSubscriber = new TestEventSubscriber();
  53. $this->_eventManager->addEventSubscriber($eventSubscriber);
  54. $this->assertTrue($this->_eventManager->hasListeners(self::preFoo));
  55. $this->assertTrue($this->_eventManager->hasListeners(self::postFoo));
  56. }
  57. /* Listener methods */
  58. public function preFoo(EventArgs $e)
  59. {
  60. $this->_preFooInvoked = true;
  61. }
  62. public function postFoo(EventArgs $e)
  63. {
  64. $this->_postFooInvoked = true;
  65. }
  66. }
  67. class TestEventSubscriber implements \Doctrine\Common\EventSubscriber
  68. {
  69. public function getSubscribedEvents()
  70. {
  71. return array('preFoo', 'postFoo');
  72. }
  73. }