diff --git a/src/TestSuite/EmailTrait.php b/src/TestSuite/EmailTrait.php index d71f6ef4d6c..132ad07f728 100644 --- a/src/TestSuite/EmailTrait.php +++ b/src/TestSuite/EmailTrait.php @@ -26,14 +26,10 @@ * * **tests/bootstrap.php** * ``` - * use Cake\Mailer\Email; * use Cake\TestSuite\TestEmailTransport; * - * // replace with other transport configs if required - * $config = Email::getConfigTransport('default'); - * $config['className'] = TestEmailTransport::class; - * Email::dropTransport('default'); - * Email::setConfigTransport('default', $config); + * // replaces existing transports with the TestEmailTransport for email assertions + * TestEmailTransport::replaceAllTransports(); * ``` */ trait EmailTrait diff --git a/src/TestSuite/TestEmailTransport.php b/src/TestSuite/TestEmailTransport.php index b00414e729c..0413aa4d2e0 100644 --- a/src/TestSuite/TestEmailTransport.php +++ b/src/TestSuite/TestEmailTransport.php @@ -41,6 +41,23 @@ public function send(Email $email) return true; } + /** + * Replaces all currently configured transports with this one + * + * @return void + */ + public static function replaceAllTransports() + { + $configuredTransports = Email::configuredTransport(); + + foreach ($configuredTransports as $configuredTransport) { + $config = Email::getConfigTransport($configuredTransport); + $config['className'] = self::class; + Email::dropTransport($configuredTransport); + Email::setConfigTransport($configuredTransport, $config); + } + } + /** * Gets emails sent * diff --git a/tests/TestCase/TestSuite/TestEmailTransportTest.php b/tests/TestCase/TestSuite/TestEmailTransportTest.php new file mode 100644 index 00000000000..e08a9387d46 --- /dev/null +++ b/tests/TestCase/TestSuite/TestEmailTransportTest.php @@ -0,0 +1,103 @@ + DebugTransport::class + ]); + Email::setConfigTransport('transport_alternate', [ + 'className' => DebugTransport::class + ]); + + Email::setConfig('default', [ + 'transport' => 'transport_default', + 'from' => 'default@example.com', + ]); + Email::setConfig('alternate', [ + 'transport' => 'transport_alternate', + 'from' => 'alternate@example.com', + ]); + } + + /** + * tearDown + * + * @return void + */ + public function tearDown() + { + parent::tearDown(); + + Email::drop('default'); + Email::drop('alternate'); + Email::dropTransport('transport_default'); + Email::dropTransport('transport_alternate'); + } + + /** + * tests replaceAllTransports + * + * @return void + */ + public function testReplaceAllTransports() + { + TestEmailTransport::replaceAllTransports(); + + $config = Email::getConfigTransport('transport_default'); + $this->assertSame(TestEmailTransport::class, $config['className']); + + $config = Email::getConfigTransport('transport_alternate'); + $this->assertSame(TestEmailTransport::class, $config['className']); + } + + /** + * tests sending an email through the transport, getting it, and clearing all emails + * + * @return void + */ + public function testSendGetAndClear() + { + TestEmailTransport::replaceAllTransports(); + + (new Email()) + ->setTo('test@example.com') + ->send('test'); + $this->assertCount(1, TestEmailTransport::getEmails()); + + TestEmailTransport::clearEmails(); + $this->assertCount(0, TestEmailTransport::getEmails()); + } +}