MailerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace yiiunit\extensions\swiftmailer;
  3. use Yii;
  4. use yii\swiftmailer\Mailer;
  5. Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer');
  6. class MailerTest extends TestCase
  7. {
  8. public function setUp()
  9. {
  10. $this->mockApplication([
  11. 'components' => [
  12. 'email' => $this->createTestEmailComponent()
  13. ]
  14. ]);
  15. }
  16. /**
  17. * @return Mailer test email component instance.
  18. */
  19. protected function createTestEmailComponent()
  20. {
  21. $component = new Mailer();
  22. return $component;
  23. }
  24. // Tests :
  25. public function testSetupTransport()
  26. {
  27. $mailer = new Mailer();
  28. $mailer->getSwiftMailer(); // make sure accessing SwiftMailer does not affect behavior of setTransport
  29. $transport = new \Swift_SendmailTransport();
  30. $mailer->setTransport($transport);
  31. $this->assertSame($transport, $mailer->getSwiftMailer()->getTransport(), 'Unable to setup transport!');
  32. }
  33. /**
  34. * @depends testSetupTransport
  35. */
  36. public function testConfigureTransport()
  37. {
  38. $mailer = new Mailer();
  39. $transportConfig = [
  40. 'class' => 'Swift_SmtpTransport',
  41. 'host' => 'localhost',
  42. 'username' => 'username',
  43. 'password' => 'password',
  44. ];
  45. $mailer->setTransport($transportConfig);
  46. $transport = $mailer->getTransport();
  47. $this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
  48. $this->assertEquals($transportConfig['class'], get_class($transport), 'Invalid transport class!');
  49. $this->assertEquals($transportConfig['host'], $transport->getHost(), 'Invalid transport host!');
  50. }
  51. /**
  52. * @depends testConfigureTransport
  53. */
  54. public function testConfigureTransportConstruct()
  55. {
  56. $mailer = new Mailer();
  57. $class = 'Swift_SmtpTransport';
  58. $host = 'some.test.host';
  59. $port = 999;
  60. $transportConfig = [
  61. 'class' => $class,
  62. 'constructArgs' => [
  63. $host,
  64. $port,
  65. ],
  66. ];
  67. $mailer->setTransport($transportConfig);
  68. $transport = $mailer->getTransport();
  69. $this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
  70. $this->assertEquals($class, get_class($transport), 'Invalid transport class!');
  71. $this->assertEquals($host, $transport->getHost(), 'Invalid transport host!');
  72. $this->assertEquals($port, $transport->getPort(), 'Invalid transport host!');
  73. }
  74. /**
  75. * @depends testConfigureTransportConstruct
  76. */
  77. public function testConfigureTransportWithPlugins()
  78. {
  79. $mailer = new Mailer();
  80. $pluginClass = 'Swift_Plugins_ThrottlerPlugin';
  81. $rate = 10;
  82. $transportConfig = [
  83. 'class' => 'Swift_SmtpTransport',
  84. 'plugins' => [
  85. [
  86. 'class' => $pluginClass,
  87. 'constructArgs' => [
  88. $rate,
  89. ],
  90. ],
  91. ],
  92. ];
  93. $mailer->setTransport($transportConfig);
  94. $transport = $mailer->getTransport();
  95. $this->assertTrue(is_object($transport), 'Unable to setup transport via config!');
  96. $this->assertContains(':' . $pluginClass . ':', print_r($transport, true), 'Plugin not added');
  97. }
  98. public function testGetSwiftMailer()
  99. {
  100. $mailer = new Mailer();
  101. $this->assertTrue(is_object($mailer->getSwiftMailer()), 'Unable to get Swift mailer instance!');
  102. }
  103. }