diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php index 6786239..4c239c5 100644 --- a/src/Service/EmailService.php +++ b/src/Service/EmailService.php @@ -26,16 +26,16 @@ class EmailService private string $from; - public function __construct() + public function __construct($dsn = null, $mailSenderName = null, $mailSender = null) { - $dsn = config('common.third_party.email.dsn'); + $dsn ??= config('common.third_party.email.dsn'); $transport = Transport::fromDsn($dsn); $this->mailer = new Mailer($transport); $this->from = sprintf( "%s <%s>", - config('common.third_party.email.mailer_sender_name'), - config('common.third_party.email.mailer_sender') + $mailSenderName ?? config('common.third_party.email.mailer_sender_name'), + $mailSender ?? config('common.third_party.email.mailer_sender') ); } diff --git a/tests/Unit/EmailServiceTest.php b/tests/Unit/EmailServiceTest.php new file mode 100644 index 0000000..004a063 --- /dev/null +++ b/tests/Unit/EmailServiceTest.php @@ -0,0 +1,65 @@ + + * Powered by PhpStorm + * Created on 2022/12/20 + */ + +namespace Singularity\HDK\Test\Core\Service; + +use Singularity\HDK\Core\Service\EmailService; +use Symfony\Component\Mailer\Exception\TransportException; + +$dsn = 'smtp://account@luxcreo.ai:Qfsd8866@smtp.qiye.aliyun.com:465'; +$mail_sender_name = 'LuxCreo'; +$mail_sender = 'account@luxcreo.ai'; +$email = new EmailService($dsn, $mail_sender_name, $mail_sender); + +it('assertions that send HTML is available', function () use ($email) { + $result = $email->sendHtml( + 'dongyun.li@luxcreo.ai', + 'HDK Unit Test HTML', + <<Hello, World! +HTML + ); + expect($result)->toBeTrue(); +}); + +it('assertions that send Text is available', function () use ($email) { + $result = $email->sendText( + 'dongyun.li@luxcreo.ai', + 'HDK Unit Test Text', + <<Hello, World! +Text + ); + expect($result)->toBeTrue(); +}); + +it('assertions Error Receiver can be catch', function () use ($email) { + try { + $email->sendHtml( + 'unknown@luxcreo.ai', + 'HDK Unit Test', + <<Hello, World! +HTML + ); + } catch (TransportException $t) { + expect($t->getCode())->toBe(554); + } + try { + $email->sendText( + 'unknown@luxcreo.ai', + 'HDK Unit Test', + <<Hello, World! +Text + ); + } catch (TransportException $t) { + expect($t->getCode())->toBe(554); + } +});