MailerTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mailer.php';
  4. require_once 'Swift/RfcComplianceException.php';
  5. require_once 'Swift/Transport.php';
  6. require_once 'Swift/Mime/Message.php';
  7. require_once 'Swift/Mailer/RecipientIterator.php';
  8. require_once 'Swift/Events/EventListener.php';
  9. class Swift_MailerTest extends Swift_Tests_SwiftUnitTestCase
  10. {
  11. public function testTransportIsStartedWhenSending()
  12. {
  13. $transport = $this->_createTransport();
  14. $message = $this->_createMessage();
  15. $con = $this->_states('Connection')->startsAs('off');
  16. $this->_checking(Expectations::create()
  17. -> allowing($transport)->isStarted() -> returns(false) -> when($con->is('off'))
  18. -> allowing($transport)->isStarted() -> returns(false) -> when($con->is('on'))
  19. -> one($transport)->start() -> when($con->is('off')) -> then($con->is('on'))
  20. -> ignoring($transport)
  21. -> ignoring($message)
  22. );
  23. $mailer = $this->_createMailer($transport);
  24. $mailer->send($message);
  25. }
  26. public function testTransportIsOnlyStartedOnce()
  27. {
  28. $transport = $this->_createTransport();
  29. $message = $this->_createMessage();
  30. $con = $this->_states('Connection')->startsAs('off');
  31. $this->_checking(Expectations::create()
  32. -> allowing($transport)->isStarted() -> returns(false) -> when($con->is('off'))
  33. -> allowing($transport)->isStarted() -> returns(false) -> when($con->is('on'))
  34. -> one($transport)->start() -> when($con->is('off')) -> then($con->is('on'))
  35. -> ignoring($transport)
  36. -> ignoring($message)
  37. );
  38. $mailer = $this->_createMailer($transport);
  39. for ($i = 0; $i < 10; ++$i)
  40. {
  41. $mailer->send($message);
  42. }
  43. }
  44. public function testMessageIsPassedToTransport()
  45. {
  46. $transport = $this->_createTransport();
  47. $message = $this->_createMessage();
  48. $this->_checking(Expectations::create()
  49. -> one($transport)->send($message, optional())
  50. -> ignoring($transport)
  51. -> ignoring($message)
  52. );
  53. $mailer = $this->_createMailer($transport);
  54. $mailer->send($message);
  55. }
  56. public function testSendReturnsCountFromTransport()
  57. {
  58. $transport = $this->_createTransport();
  59. $message = $this->_createMessage();
  60. $this->_checking(Expectations::create()
  61. -> one($transport)->send($message, optional()) -> returns(57)
  62. -> ignoring($transport)
  63. -> ignoring($message)
  64. );
  65. $mailer = $this->_createMailer($transport);
  66. $this->assertEqual(57, $mailer->send($message));
  67. }
  68. public function testFailedRecipientReferenceIsPassedToTransport()
  69. {
  70. $failures = array();
  71. $transport = $this->_createTransport();
  72. $message = $this->_createMessage();
  73. $this->_checking(Expectations::create()
  74. -> one($transport)->send($message, reference($failures))
  75. -> ignoring($transport)
  76. -> ignoring($message)
  77. );
  78. $mailer = $this->_createMailer($transport);
  79. $mailer->send($message, $failures);
  80. }
  81. public function testSendRecordsRfcComplianceExceptionAsEntireSendFailure()
  82. {
  83. $failures = array();
  84. $rfcException = new Swift_RfcComplianceException('test');
  85. $transport = $this->_createTransport();
  86. $message = $this->_createMessage();
  87. $this->_checking(Expectations::create()
  88. -> allowing($message)->getTo() -> returns(array('foo&invalid' => 'Foo', 'bar@valid.tld' => 'Bar'))
  89. -> one($transport)->send($message, reference($failures)) -> throws($rfcException)
  90. -> ignoring($transport)
  91. -> ignoring($message)
  92. );
  93. $mailer = $this->_createMailer($transport);
  94. $this->assertEqual(0, $mailer->send($message, $failures), '%s: Should return 0');
  95. $this->assertEqual(array('foo&invalid', 'bar@valid.tld'), $failures, '%s: Failures should contain all addresses since the entire message failed to compile');
  96. }
  97. public function testSendRecordsRfcComplianceExceptionAsEntireSendFailure()
  98. {
  99. $failures = array();
  100. $rfcException = new Swift_RfcComplianceException('test');
  101. $transport = $this->_createTransport();
  102. $message = $this->_createMessage();
  103. $this->_checking(Expectations::create()
  104. -> allowing($message)->getTo() -> returns(array('foo&invalid' => 'Foo', 'bar@valid.tld' => 'Bar'))
  105. -> one($transport)->send($message, reference($failures)) -> throws($rfcException)
  106. -> ignoring($transport)
  107. -> ignoring($message)
  108. );
  109. $mailer = $this->_createMailer($transport);
  110. $this->assertEqual(0, $mailer->send($message, $failures), '%s: Should return 0');
  111. $this->assertEqual(array('foo&invalid', 'bar@valid.tld'), $failures, '%s: Failures should contain all addresses since the entire message failed to compile');
  112. }
  113. public function testBatchSendRecordsRfcComplianceExceptionAsIndividualRecipientFailure()
  114. {
  115. $failures = array();
  116. $rfcException = new Swift_RfcComplianceException('test');
  117. $transport = $this->_createTransport();
  118. $message = $this->_createMessage();
  119. $this->_checking(Expectations::create()
  120. -> one($message)->getTo() -> returns(array('foo&invalid' => 'Foo', 'bar@valid.tld' => 'Bar'))
  121. -> one($message)->setTo(array('foo&invalid' => 'Foo'))
  122. -> one($message)->getTo() -> returns(array('foo&invalid' => 'Foo'))
  123. -> one($message)->setTo(array('bar@valid.tld' => 'Bar'))
  124. -> one($transport)->send($message, reference($failures)) -> throws($rfcException)
  125. -> one($transport)->send($message, reference($failures)) -> returns(1)
  126. -> ignoring($transport)
  127. -> ignoring($message)
  128. );
  129. $mailer = $this->_createMailer($transport);
  130. $this->assertEqual(1, $mailer->batchSend($message, $failures), '%s: Should return just 1');
  131. $this->assertEqual(array('foo&invalid'), $failures, '%s: Failures should contain the non-compliant address');
  132. }
  133. public function testRegisterPluginDelegatesToTransport()
  134. {
  135. $plugin = $this->_createPlugin();
  136. $transport = $this->_createTransport();
  137. $mailer = $this->_createMailer($transport);
  138. $this->_checking(Expectations::create()
  139. -> one($transport)->registerPlugin($plugin)
  140. );
  141. $mailer->registerPlugin($plugin);
  142. }
  143. // -- Creation methods
  144. private function _createPlugin()
  145. {
  146. return $this->_mock('Swift_Events_EventListener');
  147. }
  148. private function _createTransport()
  149. {
  150. return $this->_mock('Swift_Transport');
  151. }
  152. private function _createMessage()
  153. {
  154. return $this->_mock('Swift_Mime_Message');
  155. }
  156. private function _createIterator()
  157. {
  158. return $this->_mock('Swift_Mailer_RecipientIterator');
  159. }
  160. private function _createMailer(Swift_Transport $transport)
  161. {
  162. return new Swift_Mailer($transport);
  163. }
  164. }