diff --git a/common/emails/TemplateWithRenderer.php b/common/emails/TemplateWithRenderer.php index 0e0817f..33b40f1 100644 --- a/common/emails/TemplateWithRenderer.php +++ b/common/emails/TemplateWithRenderer.php @@ -40,7 +40,7 @@ abstract class TemplateWithRenderer extends Template { * * @return string */ - abstract protected function getTemplateName(): string; + abstract public function getTemplateName(): string; protected final function getView() { return $this->getTemplateName(); diff --git a/common/emails/templates/ForgotPasswordEmail.php b/common/emails/templates/ForgotPasswordEmail.php index b4a03c3..38def4d 100644 --- a/common/emails/templates/ForgotPasswordEmail.php +++ b/common/emails/templates/ForgotPasswordEmail.php @@ -19,7 +19,7 @@ class ForgotPasswordEmail extends TemplateWithRenderer { return 'Ely.by Account forgot password'; } - protected function getTemplateName(): string { + public function getTemplateName(): string { return 'forgotPassword'; } diff --git a/common/emails/templates/RegistrationEmail.php b/common/emails/templates/RegistrationEmail.php index 1ad0535..bf85617 100644 --- a/common/emails/templates/RegistrationEmail.php +++ b/common/emails/templates/RegistrationEmail.php @@ -19,7 +19,7 @@ class RegistrationEmail extends TemplateWithRenderer { return 'Ely.by Account registration'; } - protected function getTemplateName(): string { + public function getTemplateName(): string { return 'register'; } diff --git a/tests/codeception/common/unit/emails/EmailHelperTest.php b/tests/codeception/common/unit/emails/EmailHelperTest.php new file mode 100644 index 0000000..9b9d011 --- /dev/null +++ b/tests/codeception/common/unit/emails/EmailHelperTest.php @@ -0,0 +1,18 @@ +makePartial(); + $account->username = 'mock-username'; + $account->email = 'mock@ely.by'; + $this->assertEquals(['mock@ely.by' => 'mock-username'], EmailHelper::buildTo($account)); + } + +} diff --git a/tests/codeception/common/unit/emails/TemplateTest.php b/tests/codeception/common/unit/emails/TemplateTest.php new file mode 100644 index 0000000..d796b32 --- /dev/null +++ b/tests/codeception/common/unit/emails/TemplateTest.php @@ -0,0 +1,47 @@ +makePartial(); + $this->assertEquals('find-me', $template->getTo()); + $this->assertInstanceOf(MailerInterface::class, $template->getMailer()); + } + + public function testGetFrom() { + Yii::$app->params['fromEmail'] = 'find-me'; + /** @var Template|\Mockery\MockInterface $template */ + $template = mock(Template::class)->makePartial(); + $this->assertEquals(['find-me' => 'Ely.by Accounts'], $template->getFrom()); + } + + public function testGetParams() { + /** @var Template|\Mockery\MockInterface $template */ + $template = mock(Template::class)->makePartial(); + $this->assertEquals([], $template->getParams()); + } + + public function testCreateMessage() { + Yii::$app->params['fromEmail'] = 'from@ely.by'; + /** @var Template|\Mockery\MockInterface $template */ + $template = mock(Template::class, [['to@ely.by' => 'To']])->makePartial(); + $template->shouldReceive('getSubject')->andReturn('mock-subject'); + /** @var MessageInterface $message */ + $message = $this->callProtected($template, 'createMessage'); + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertEquals(['to@ely.by' => 'To'], $message->getTo()); + $this->assertEquals(['from@ely.by' => 'Ely.by Accounts'], $message->getFrom()); + $this->assertEquals('mock-subject', $message->getSubject()); + } + +} diff --git a/tests/codeception/common/unit/emails/TemplateWithRendererTest.php b/tests/codeception/common/unit/emails/TemplateWithRendererTest.php new file mode 100644 index 0000000..83f715c --- /dev/null +++ b/tests/codeception/common/unit/emails/TemplateWithRendererTest.php @@ -0,0 +1,49 @@ +makePartial(); + $this->assertEquals('mock-to', $template->getTo()); + $this->assertEquals('mock-locale', $template->getLocale()); + $this->assertInstanceOf(MailerInterface::class, $template->getMailer()); + $this->assertInstanceOf(EmailRenderer::class, $template->getEmailRenderer()); + } + + public function testCreateMessage() { + /** @var TemplateBuilder|\Mockery\MockInterface $templateBuilder */ + $templateBuilder = mock(TemplateBuilder::class)->makePartial(); + $templateBuilder->shouldReceive('render')->andReturn('mock-html'); + + /** @var EmailRenderer|\Mockery\MockInterface $renderer */ + $renderer = mock(EmailRenderer::class)->makePartial(); + $renderer->shouldReceive('getTemplate')->with('mock-template')->andReturn($templateBuilder); + + /** @var TemplateWithRenderer|\Mockery\MockInterface $template */ + $template = mock(TemplateWithRenderer::class, [['to@ely.by' => 'To'], 'mock-locale']); + $template->makePartial(); + $template->shouldReceive('getEmailRenderer')->andReturn($renderer); + $template->shouldReceive('getFrom')->andReturn(['from@ely.by' => 'From']); + $template->shouldReceive('getSubject')->andReturn('mock-subject'); + $template->shouldReceive('getTemplateName')->andReturn('mock-template'); + /** @var \yii\swiftmailer\Message $message */ + $message = $this->callProtected($template, 'createMessage'); + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertEquals(['to@ely.by' => 'To'], $message->getTo()); + $this->assertEquals(['from@ely.by' => 'From'], $message->getFrom()); + $this->assertEquals('mock-subject', $message->getSubject()); + $this->assertEquals('mock-html', $message->getSwiftMessage()->getBody()); + } + +}