88use Illuminated \Console \Tests \App \Console \Commands \EmailNotificationsInvalidRecipientsCommand ;
99use Illuminated \Console \Tests \TestCase ;
1010use Monolog \Handler \DeduplicationHandler ;
11- use Monolog \Handler \SwiftMailerHandler ;
11+ use Monolog \Handler \Handler ;
12+ use Monolog \Handler \SymfonyMailerHandler ;
1213use Monolog \Logger ;
13- use Swift_Message ;
14+ use Symfony \ Component \ Mime \ Email ;
1415
1516class EmailChannelTest extends TestCase
1617{
@@ -19,22 +20,22 @@ public function it_validates_and_filters_notification_recipients()
1920 {
2021 /** @var EmailNotificationsInvalidRecipientsCommand $command */
2122 $ command = $ this ->runArtisan (new EmailNotificationsInvalidRecipientsCommand );
22- $ this ->assertNotInstanceOf (SwiftMailerHandler ::class, $ command ->emailChannelHandler ());
23+ $ this ->assertNotInstanceOf (SymfonyMailerHandler ::class, $ command ->emailChannelHandler ());
2324 }
2425
2526 /** @test */
26- public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver ()
27+ public function it_uses_configured_monolog_symfony_mailer_handler_on_mail_driver ()
2728 {
2829 config (['mail.driver ' => 'mail ' ]);
2930
3031 /** @var EmailNotificationsCommand $command */
3132 $ command = $ this ->runArtisan (new EmailNotificationsCommand );
3233
33- $ this ->assertMailerHandlersEqual ($ this ->composeSwiftMailerHandler (), $ command ->emailChannelHandler ());
34+ $ this ->assertMailerHandlersEqual ($ this ->composeSymfonyMailerHandler (), $ command ->emailChannelHandler ());
3435 }
3536
3637 /** @test */
37- public function it_uses_configured_monolog_swift_mailer_handler_on_smtp_driver ()
38+ public function it_uses_configured_monolog_symfony_mailer_handler_on_smtp_driver ()
3839 {
3940 config ([
4041 'mail.driver ' => 'smtp ' ,
@@ -45,18 +46,18 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_smtp_driver()
4546 /** @var EmailNotificationsCommand $command */
4647 $ command = $ this ->runArtisan (new EmailNotificationsCommand );
4748
48- $ this ->assertMailerHandlersEqual ($ this ->composeSwiftMailerHandler (), $ command ->emailChannelHandler ());
49+ $ this ->assertMailerHandlersEqual ($ this ->composeSymfonyMailerHandler (), $ command ->emailChannelHandler ());
4950 }
5051
5152 /** @test */
52- public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driver ()
53+ public function it_uses_configured_monolog_symfony_mailer_handler_on_sendmail_driver ()
5354 {
5455 config (['mail.driver ' => 'sendmail ' ]);
5556
5657 /** @var EmailNotificationsCommand $command */
5758 $ command = $ this ->runArtisan (new EmailNotificationsCommand );
5859
59- $ this ->assertMailerHandlersEqual ($ this ->composeSwiftMailerHandler (), $ command ->emailChannelHandler ());
60+ $ this ->assertMailerHandlersEqual ($ this ->composeSymfonyMailerHandler (), $ command ->emailChannelHandler ());
6061 }
6162
6263 /** @test */
@@ -73,14 +74,11 @@ public function it_uses_configured_monolog_deduplication_handler_if_deduplicatio
7374 }
7475
7576 /**
76- * Compose "swift mailer" handler.
77- *
78- * @param string $command
79- * @return \Monolog\Handler\SwiftMailerHandler
77+ * Compose "symfony mailer" handler.
8078 */
81- private function composeSwiftMailerHandler ( $ command = 'email-notifications-command ' )
79+ private function composeSymfonyMailerHandler ( string $ command = 'email-notifications-command ' ): SymfonyMailerHandler
8280 {
83- $ handler = new SwiftMailerHandler (app ('mailer ' )->getSwiftMailer (), $ this ->composeMailerHandlerMessage ($ command ), Logger::NOTICE );
81+ $ handler = new SymfonyMailerHandler (app ('mailer ' )->getSymfonyTransport (), $ this ->composeMailerHandlerMessage ($ command ), Logger::NOTICE );
8482
8583 $ handler ->setFormatter (new MonologHtmlFormatter );
8684
@@ -89,49 +87,29 @@ private function composeSwiftMailerHandler($command = 'email-notifications-comma
8987
9088 /**
9189 * Compose "deduplication" handler.
92- *
93- * @return \Monolog\Handler\DeduplicationHandler
9490 */
95- private function composeDeduplicationHandler ()
91+ private function composeDeduplicationHandler (): DeduplicationHandler
9692 {
9793 return new DeduplicationHandler (
98- $ this ->composeSwiftMailerHandler ('email-notifications-deduplication-command ' ), null , Logger::NOTICE , 60
94+ $ this ->composeSymfonyMailerHandler ('email-notifications-deduplication-command ' ), null , Logger::NOTICE , 60
9995 );
10096 }
10197
10298 /**
10399 * Compose mailer handler message.
104- *
105- * @param string $command
106- * @return \Swift_Message
107100 */
108- private function composeMailerHandlerMessage ($ command )
101+ private function composeMailerHandlerMessage (string $ command ): Email
109102 {
110- /** @var Swift_Message $message */
111- $ message = app ('mailer ' )->getSwiftMailer ()->createMessage ();
112- $ message ->setSubject ("[TESTING] %level_name% in ` {$ command }` command " );
113- $ message ->setFrom (to_swiftmailer_emails ([
114- 115- 'name ' => 'ICLogger Notification ' ,
116- ]));
117- $ message ->setTo (to_swiftmailer_emails ([
118- [
'address ' =>
'[email protected] ' ,
'name ' =>
'John Doe ' ],
119- [
'address ' =>
'[email protected] ' ,
'name ' =>
'Jane Smith ' ],
120- ]));
121- $ message ->setContentType ('text/html ' );
122- $ message ->setCharset ('utf-8 ' );
123-
124- return $ message ;
103+ return (new Email ())
104+ ->subject ("[TESTING] %level_name% in ` {$ command }` command " )
105+ ->
from (
'ICLogger Notification <[email protected] > ' )
106+ ->
to (
'John Doe <[email protected] > ' ,
'Jane Smith <[email protected] > ' );
125107 }
126108
127109 /**
128110 * Assert mailer handlers are equal.
129- *
130- * @param mixed $handler1
131- * @param mixed $handler2
132- * @return void
133111 */
134- protected function assertMailerHandlersEqual ($ handler1 , $ handler2 )
112+ protected function assertMailerHandlersEqual (Handler $ handler1 , Handler $ handler2 ): void
135113 {
136114 $ handler1 = $ this ->normalizeMailerHandlerDump (get_dump ($ handler1 ));
137115 $ handler2 = $ this ->normalizeMailerHandlerDump (get_dump ($ handler2 ));
@@ -141,13 +119,11 @@ protected function assertMailerHandlersEqual($handler1, $handler2)
141119 /**
142120 * Normalize the mailer handler dump.
143121 *
144- * @param string $dump
145- * @return string
122+ * @noinspection PhpUnnecessaryLocalVariableInspection
146123 */
147- private function normalizeMailerHandlerDump (string $ dump )
124+ private function normalizeMailerHandlerDump (string $ dump ): string
148125 {
149126 $ dump = preg_replace ('/{#\d*/ ' , '{ ' , $ dump );
150- $ dump =
preg_replace (
'/".*[email protected] "/ ' ,
'"normalized" ' ,
$ dump);
151127 $ dump = preg_replace ('/\+"date": ".*?\.\d*"/ ' , '+date: "normalized" ' , $ dump );
152128 $ dump = preg_replace ('/date: .*?\.\d*/ ' , 'date: "normalized" ' , $ dump );
153129 $ dump = preg_replace ('/-dateTime: DateTimeImmutable @\d*/ ' , '-dateTime: DateTimeImmutable @normalized ' , $ dump );
0 commit comments