TestEmailTransportTest.php 2.9 KB

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