MailerTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 testRegisterPluginDelegatesToTransport()
  98. {
  99. $plugin = $this->_createPlugin();
  100. $transport = $this->_createTransport();
  101. $mailer = $this->_createMailer($transport);
  102. $this->_checking(Expectations::create()
  103. -> one($transport)->registerPlugin($plugin)
  104. );
  105. $mailer->registerPlugin($plugin);
  106. }
  107. // -- Creation methods
  108. private function _createPlugin()
  109. {
  110. return $this->_mock('Swift_Events_EventListener');
  111. }
  112. private function _createTransport()
  113. {
  114. return $this->_mock('Swift_Transport');
  115. }
  116. private function _createMessage()
  117. {
  118. return $this->_mock('Swift_Mime_Message');
  119. }
  120. private function _createIterator()
  121. {
  122. return $this->_mock('Swift_Mailer_RecipientIterator');
  123. }
  124. private function _createMailer(Swift_Transport $transport)
  125. {
  126. return new Swift_Mailer($transport);
  127. }
  128. }