LoggerPluginTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Plugins/LoggerPlugin.php';
  4. require_once 'Swift/Plugins/Logger.php';
  5. require_once 'Swift/Events/CommandEvent.php';
  6. require_once 'Swift/Events/ResponseEvent.php';
  7. require_once 'Swift/Events/TransportChangeEvent.php';
  8. require_once 'Swift/Events/TransportExceptionEvent.php';
  9. require_once 'Swift/Transport.php';
  10. require_once 'Swift/TransportException.php';
  11. class Swift_Plugins_LoggerPluginTest extends Swift_Tests_SwiftUnitTestCase
  12. {
  13. public function testLoggerDelegatesAddingEntries()
  14. {
  15. $logger = $this->_createLogger();
  16. $this->_checking(Expectations::create()
  17. -> one($logger)->add('foo')
  18. );
  19. $plugin = $this->_createPlugin($logger);
  20. $plugin->add('foo');
  21. }
  22. public function testLoggerDelegatesDumpingEntries()
  23. {
  24. $logger = $this->_createLogger();
  25. $this->_checking(Expectations::create()
  26. -> one($logger)->dump() -> returns('foobar')
  27. );
  28. $plugin = $this->_createPlugin($logger);
  29. $this->assertEqual('foobar', $plugin->dump());
  30. }
  31. public function testLoggerDelegatesClearingEntries()
  32. {
  33. $logger = $this->_createLogger();
  34. $this->_checking(Expectations::create()
  35. -> one($logger)->clear()
  36. );
  37. $plugin = $this->_createPlugin($logger);
  38. $plugin->clear();
  39. }
  40. public function testCommandIsSentToLogger()
  41. {
  42. $evt = $this->_createCommandEvent("foo\r\n");
  43. $logger = $this->_createLogger();
  44. $this->_checking(Expectations::create()
  45. -> one($logger)->add(pattern('~foo\r\n~'))
  46. );
  47. $plugin = $this->_createPlugin($logger);
  48. $plugin->commandSent($evt);
  49. }
  50. public function testResponseIsSentToLogger()
  51. {
  52. $evt = $this->_createResponseEvent("354 Go ahead\r\n");
  53. $logger = $this->_createLogger();
  54. $this->_checking(Expectations::create()
  55. -> one($logger)->add(pattern('~354 Go ahead\r\n~'))
  56. );
  57. $plugin = $this->_createPlugin($logger);
  58. $plugin->responseReceived($evt);
  59. }
  60. public function testTransportBeforeStartChangeIsSentToLogger()
  61. {
  62. $evt = $this->_createTransportChangeEvent();
  63. $logger = $this->_createLogger();
  64. $this->_checking(Expectations::create()
  65. -> one($logger)->add(any())
  66. );
  67. $plugin = $this->_createPlugin($logger);
  68. $plugin->beforeTransportStarted($evt);
  69. }
  70. public function testTransportStartChangeIsSentToLogger()
  71. {
  72. $evt = $this->_createTransportChangeEvent();
  73. $logger = $this->_createLogger();
  74. $this->_checking(Expectations::create()
  75. -> one($logger)->add(any())
  76. );
  77. $plugin = $this->_createPlugin($logger);
  78. $plugin->transportStarted($evt);
  79. }
  80. public function testTransportStopChangeIsSentToLogger()
  81. {
  82. $evt = $this->_createTransportChangeEvent();
  83. $logger = $this->_createLogger();
  84. $this->_checking(Expectations::create()
  85. -> one($logger)->add(any())
  86. );
  87. $plugin = $this->_createPlugin($logger);
  88. $plugin->transportStopped($evt);
  89. }
  90. public function testTransportBeforeStopChangeIsSentToLogger()
  91. {
  92. $evt = $this->_createTransportChangeEvent();
  93. $logger = $this->_createLogger();
  94. $this->_checking(Expectations::create()
  95. -> one($logger)->add(any())
  96. );
  97. $plugin = $this->_createPlugin($logger);
  98. $plugin->beforeTransportStopped($evt);
  99. }
  100. public function testExceptionsArePassedToDelegateAndLeftToBubbleUp()
  101. {
  102. $transport = $this->_createTransport();
  103. $evt = $this->_createTransportExceptionEvent();
  104. $logger = $this->_createLogger();
  105. $this->_checking(Expectations::create()
  106. -> one($logger)->add(any())
  107. -> allowing($logger)
  108. );
  109. $plugin = $this->_createPlugin($logger);
  110. try
  111. {
  112. $plugin->exceptionThrown($evt);
  113. $this->fail('Exception should bubble up.');
  114. }
  115. catch (Swift_TransportException $ex)
  116. {
  117. }
  118. }
  119. // -- Creation Methods
  120. private function _createLogger()
  121. {
  122. return $this->_mock('Swift_Plugins_Logger');
  123. }
  124. private function _createPlugin($logger)
  125. {
  126. return new Swift_Plugins_LoggerPlugin($logger);
  127. }
  128. private function _createCommandEvent($command)
  129. {
  130. $evt = $this->_mock('Swift_Events_CommandEvent');
  131. $this->_checking(Expectations::create()
  132. -> ignoring($evt)->getCommand() -> returns($command)
  133. -> ignoring($evt)
  134. );
  135. return $evt;
  136. }
  137. private function _createResponseEvent($response)
  138. {
  139. $evt = $this->_mock('Swift_Events_ResponseEvent');
  140. $this->_checking(Expectations::create()
  141. -> ignoring($evt)->getResponse() -> returns($response)
  142. -> ignoring($evt)
  143. );
  144. return $evt;
  145. }
  146. private function _createTransport()
  147. {
  148. return $this->_mock('Swift_Transport');
  149. }
  150. private function _createTransportChangeEvent()
  151. {
  152. $evt = $this->_mock('Swift_Events_TransportChangeEvent');
  153. $this->_checking(Expectations::create()
  154. -> ignoring($evt)->getSource() -> returns($this->_createTransport())
  155. -> ignoring($evt)
  156. );
  157. return $evt;
  158. }
  159. private function _createTransportExceptionEvent()
  160. {
  161. $evt = $this->_mock('Swift_Events_TransportExceptionEvent');
  162. $this->_checking(Expectations::create()
  163. -> ignoring($evt)->getException() -> returns(new Swift_TransportException(''))
  164. -> ignoring($evt)
  165. );
  166. return $evt;
  167. }
  168. }