TestEmailTransportTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Mailer\TransportFactory;
  18. use Cake\Network\Email\DebugTransport;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\TestSuite\TestEmailTransport;
  21. class TestEmailTransportTest extends TestCase
  22. {
  23. /**
  24. * setUp
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. Email::drop('default');
  32. Email::drop('alternate');
  33. TransportFactory::drop('transport_default');
  34. TransportFactory::drop('transport_alternate');
  35. TransportFactory::setConfig('transport_default', [
  36. 'className' => DebugTransport::class,
  37. ]);
  38. TransportFactory::setConfig('transport_alternate', [
  39. 'className' => DebugTransport::class,
  40. ]);
  41. Email::setConfig('default', [
  42. 'transport' => 'transport_default',
  43. 'from' => 'default@example.com',
  44. ]);
  45. Email::setConfig('alternate', [
  46. 'transport' => 'transport_alternate',
  47. 'from' => 'alternate@example.com',
  48. ]);
  49. }
  50. /**
  51. * tearDown
  52. *
  53. * @return void
  54. */
  55. public function tearDown()
  56. {
  57. parent::tearDown();
  58. Email::drop('default');
  59. Email::drop('alternate');
  60. TransportFactory::drop('transport_default');
  61. TransportFactory::drop('transport_alternate');
  62. }
  63. /**
  64. * tests replaceAllTransports
  65. *
  66. * @return void
  67. */
  68. public function testReplaceAllTransports()
  69. {
  70. TestEmailTransport::replaceAllTransports();
  71. $config = TransportFactory::getConfig('transport_default');
  72. $this->assertSame(TestEmailTransport::class, $config['className']);
  73. $config = TransportFactory::getConfig('transport_alternate');
  74. $this->assertSame(TestEmailTransport::class, $config['className']);
  75. }
  76. /**
  77. * tests sending an email through the transport, getting it, and clearing all emails
  78. *
  79. * @return void
  80. */
  81. public function testSendGetAndClear()
  82. {
  83. TestEmailTransport::replaceAllTransports();
  84. (new Email())
  85. ->setTo('test@example.com')
  86. ->send('test');
  87. $this->assertCount(1, TestEmailTransport::getEmails());
  88. TestEmailTransport::clearEmails();
  89. $this->assertCount(0, TestEmailTransport::getEmails());
  90. }
  91. }