MailerTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $mailer->send($message);
  41. }
  42. }
  43. public function testMessageIsPassedToTransport()
  44. {
  45. $transport = $this->_createTransport();
  46. $message = $this->_createMessage();
  47. $this->_checking(Expectations::create()
  48. -> one($transport)->send($message, optional())
  49. -> ignoring($transport)
  50. -> ignoring($message)
  51. );
  52. $mailer = $this->_createMailer($transport);
  53. $mailer->send($message);
  54. }
  55. public function testSendReturnsCountFromTransport()
  56. {
  57. $transport = $this->_createTransport();
  58. $message = $this->_createMessage();
  59. $this->_checking(Expectations::create()
  60. -> one($transport)->send($message, optional()) -> returns(57)
  61. -> ignoring($transport)
  62. -> ignoring($message)
  63. );
  64. $mailer = $this->_createMailer($transport);
  65. $this->assertEqual(57, $mailer->send($message));
  66. }
  67. public function testFailedRecipientReferenceIsPassedToTransport()
  68. {
  69. $failures = array();
  70. $transport = $this->_createTransport();
  71. $message = $this->_createMessage();
  72. $this->_checking(Expectations::create()
  73. -> one($transport)->send($message, reference($failures))
  74. -> ignoring($transport)
  75. -> ignoring($message)
  76. );
  77. $mailer = $this->_createMailer($transport);
  78. $mailer->send($message, $failures);
  79. }
  80. public function testSendRecordsRfcComplianceExceptionAsEntireSendFailure()
  81. {
  82. $failures = array();
  83. $rfcException = new Swift_RfcComplianceException('test');
  84. $transport = $this->_createTransport();
  85. $message = $this->_createMessage();
  86. $this->_checking(Expectations::create()
  87. -> allowing($message)->getTo() -> returns(array('foo&invalid' => 'Foo', 'bar@valid.tld' => 'Bar'))
  88. -> one($transport)->send($message, reference($failures)) -> throws($rfcException)
  89. -> ignoring($transport)
  90. -> ignoring($message)
  91. );
  92. $mailer = $this->_createMailer($transport);
  93. $this->assertEqual(0, $mailer->send($message, $failures), '%s: Should return 0');
  94. $this->assertEqual(array('foo&invalid', 'bar@valid.tld'), $failures, '%s: Failures should contain all addresses since the entire message failed to compile');
  95. }
  96. public function testRegisterPluginDelegatesToTransport()
  97. {
  98. $plugin = $this->_createPlugin();
  99. $transport = $this->_createTransport();
  100. $mailer = $this->_createMailer($transport);
  101. $this->_checking(Expectations::create()
  102. -> one($transport)->registerPlugin($plugin)
  103. );
  104. $mailer->registerPlugin($plugin);
  105. }
  106. // -- Creation methods
  107. private function _createPlugin()
  108. {
  109. return $this->_mock('Swift_Events_EventListener');
  110. }
  111. private function _createTransport()
  112. {
  113. return $this->_mock('Swift_Transport');
  114. }
  115. private function _createMessage()
  116. {
  117. return $this->_mock('Swift_Mime_Message');
  118. }
  119. private function _createIterator()
  120. {
  121. return $this->_mock('Swift_Mailer_RecipientIterator');
  122. }
  123. private function _createMailer(Swift_Transport $transport)
  124. {
  125. return new Swift_Mailer($transport);
  126. }
  127. }