TestEmailTransportTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.7.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Mailer\Email;
  17. use Cake\Network\Email\DebugTransport;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\TestSuite\TestEmailTransport;
  20. class TestEmailTransportTest extends TestCase
  21. {
  22. /**
  23. * setUp
  24. *
  25. * @return void
  26. */
  27. public function setUp()
  28. {
  29. parent::setUp();
  30. Email::drop('default');
  31. Email::drop('alternate');
  32. Email::dropTransport('transport_default');
  33. Email::dropTransport('transport_alternate');
  34. Email::setConfigTransport('transport_default', [
  35. 'className' => DebugTransport::class
  36. ]);
  37. Email::setConfigTransport('transport_alternate', [
  38. 'className' => DebugTransport::class
  39. ]);
  40. Email::setConfig('default', [
  41. 'transport' => 'transport_default',
  42. 'from' => 'default@example.com',
  43. ]);
  44. Email::setConfig('alternate', [
  45. 'transport' => 'transport_alternate',
  46. 'from' => 'alternate@example.com',
  47. ]);
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. parent::tearDown();
  57. Email::drop('default');
  58. Email::drop('alternate');
  59. Email::dropTransport('transport_default');
  60. Email::dropTransport('transport_alternate');
  61. }
  62. /**
  63. * tests replaceAllTransports
  64. *
  65. * @return void
  66. */
  67. public function testReplaceAllTransports()
  68. {
  69. TestEmailTransport::replaceAllTransports();
  70. $config = Email::getConfigTransport('transport_default');
  71. $this->assertSame(TestEmailTransport::class, $config['className']);
  72. $config = Email::getConfigTransport('transport_alternate');
  73. $this->assertSame(TestEmailTransport::class, $config['className']);
  74. }
  75. /**
  76. * tests sending an email through the transport, getting it, and clearing all emails
  77. *
  78. * @return void
  79. */
  80. public function testSendGetAndClear()
  81. {
  82. TestEmailTransport::replaceAllTransports();
  83. (new Email())
  84. ->setTo('test@example.com')
  85. ->send('test');
  86. $this->assertCount(1, TestEmailTransport::getEmails());
  87. TestEmailTransport::clearEmails();
  88. $this->assertCount(0, TestEmailTransport::getEmails());
  89. }
  90. }