SimpleEventDispatcherTest.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Events/SimpleEventDispatcher.php';
  4. require_once 'Swift/Events/EventListener.php';
  5. require_once 'Swift/Transport.php';
  6. require_once 'Swift/Mime/Message.php';
  7. require_once 'Swift/TransportException.php';
  8. class Swift_Events_SimpleEventDispatcherTest
  9. extends Swift_Tests_SwiftUnitTestCase
  10. {
  11. private $_dispatcher;
  12. public function setUp()
  13. {
  14. $this->_dispatcher = new Swift_Events_SimpleEventDispatcher();
  15. }
  16. public function testSendEventCanBeCreated()
  17. {
  18. $transport = $this->_stub('Swift_Transport');
  19. $message = $this->_stub('Swift_Mime_Message');
  20. $evt = $this->_dispatcher->createSendEvent($transport, $message);
  21. $this->assertIsA($evt, 'Swift_Events_SendEvent');
  22. $this->assertSame($message, $evt->getMessage());
  23. $this->assertSame($transport, $evt->getTransport());
  24. }
  25. public function testCommandEventCanBeCreated()
  26. {
  27. $buf = $this->_stub('Swift_Transport');
  28. $evt = $this->_dispatcher->createCommandEvent($buf, "FOO\r\n", array(250));
  29. $this->assertIsA($evt, 'Swift_Events_CommandEvent');
  30. $this->assertSame($buf, $evt->getSource());
  31. $this->assertEqual("FOO\r\n", $evt->getCommand());
  32. $this->assertEqual(array(250), $evt->getSuccessCodes());
  33. }
  34. public function testResponseEventCanBeCreated()
  35. {
  36. $buf = $this->_stub('Swift_Transport');
  37. $evt = $this->_dispatcher->createResponseEvent($buf, "250 Ok\r\n", true);
  38. $this->assertIsA($evt, 'Swift_Events_ResponseEvent');
  39. $this->assertSame($buf, $evt->getSource());
  40. $this->assertEqual("250 Ok\r\n", $evt->getResponse());
  41. $this->assertTrue($evt->isValid());
  42. }
  43. public function testTransportChangeEventCanBeCreated()
  44. {
  45. $transport = $this->_stub('Swift_Transport');
  46. $evt = $this->_dispatcher->createTransportChangeEvent($transport);
  47. $this->assertIsA($evt, 'Swift_Events_TransportChangeEvent');
  48. $this->assertSame($transport, $evt->getSource());
  49. }
  50. public function testTransportExceptionEventCanBeCreated()
  51. {
  52. $transport = $this->_stub('Swift_Transport');
  53. $ex = new Swift_TransportException('');
  54. $evt = $this->_dispatcher->createTransportExceptionEvent($transport, $ex);
  55. $this->assertIsA($evt, 'Swift_Events_TransportExceptionEvent');
  56. $this->assertSame($transport, $evt->getSource());
  57. $this->assertSame($ex, $evt->getException());
  58. }
  59. public function testListenersAreNotifiedOfDispatchedEvent()
  60. {
  61. $transport = $this->_stub('Swift_Transport');
  62. $evt = $this->_dispatcher->createTransportChangeEvent($transport);
  63. $listenerA = $this->_mock('Swift_Events_TransportChangeListener');
  64. $listenerB = $this->_mock('Swift_Events_TransportChangeListener');
  65. $this->_dispatcher->bindEventListener($listenerA);
  66. $this->_dispatcher->bindEventListener($listenerB);
  67. $this->_checking(Expectations::create()
  68. -> one($listenerA)->transportStarted($evt)
  69. -> one($listenerB)->transportStarted($evt)
  70. );
  71. $this->_dispatcher->dispatchEvent($evt, 'transportStarted');
  72. }
  73. public function testListenersAreOnlyCalledIfImplementingCorrectInterface()
  74. {
  75. $transport = $this->_stub('Swift_Transport');
  76. $message = $this->_stub('Swift_Mime_Message');
  77. $evt = $this->_dispatcher->createSendEvent($transport, $message);
  78. $targetListener = $this->_mock('Swift_Events_SendListener');
  79. $otherListener = $this->_mock('Swift_Events_TransportChangeListener');
  80. $this->_dispatcher->bindEventListener($targetListener);
  81. $this->_dispatcher->bindEventListener($otherListener);
  82. $this->_checking(Expectations::create()
  83. -> one($targetListener)->sendPerformed($evt)
  84. -> never($otherListener)
  85. );
  86. $this->_dispatcher->dispatchEvent($evt, 'sendPerformed');
  87. }
  88. public function testListenersCanCancelBubblingOfEvent()
  89. {
  90. $transport = $this->_stub('Swift_Transport');
  91. $message = $this->_stub('Swift_Mime_Message');
  92. $evt = $this->_dispatcher->createSendEvent($transport, $message);
  93. $listenerA = $this->_mock('Swift_Events_SendListener');
  94. $listenerB = $this->_mock('Swift_Events_SendListener');
  95. $this->_dispatcher->bindEventListener($listenerA);
  96. $this->_dispatcher->bindEventListener($listenerB);
  97. $this->_checking(Expectations::create()
  98. -> one($listenerA)->sendPerformed($evt) -> calls(array($this, '_cancelBubble'))
  99. -> never($listenerB)
  100. );
  101. $this->_dispatcher->dispatchEvent($evt, 'sendPerformed');
  102. $this->assertTrue($evt->bubbleCancelled());
  103. }
  104. public function testAddingListenerTwiceDoesNotReceiveEventTwice()
  105. {
  106. $transport = $this->_stub('Swift_Transport');
  107. $evt = $this->_dispatcher->createTransportChangeEvent($transport);
  108. $listener = $this->_mock('Swift_Events_TransportChangeListener');
  109. $this->_dispatcher->bindEventListener($listener);
  110. $this->_dispatcher->bindEventListener($listener);
  111. $this->_checking(Expectations::create()
  112. -> one($listener)->transportStarted($evt)
  113. -> never($listener)->transportStarted($evt)
  114. );
  115. $this->_dispatcher->dispatchEvent($evt, 'transportStarted');
  116. }
  117. // -- Mock callbacks
  118. public function _cancelBubble(Yay_Invocation $inv)
  119. {
  120. $args = $inv->getArguments();
  121. $args[0]->cancelBubble(true);
  122. }
  123. // -- Private methods
  124. private function _createDispatcher(array $map)
  125. {
  126. $dispatcher = new Swift_Events_SimpleEventDispatcher($map);
  127. return $dispatcher;
  128. }
  129. }