TestEmailTransportTest.php 2.9 KB

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