From f3259c9c0e20a9a15233a3e09c28beb4bcf2519b Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Tue, 25 Apr 2017 02:09:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=BE=D0=B2=D0=BE=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/emails/TemplateWithRenderer.php | 2 +- .../emails/templates/ForgotPasswordEmail.php | 2 +- common/emails/templates/RegistrationEmail.php | 2 +- .../common/unit/emails/EmailHelperTest.php | 18 +++++++ .../common/unit/emails/TemplateTest.php | 47 ++++++++++++++++++ .../unit/emails/TemplateWithRendererTest.php | 49 +++++++++++++++++++ 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 tests/codeception/common/unit/emails/EmailHelperTest.php create mode 100644 tests/codeception/common/unit/emails/TemplateTest.php create mode 100644 tests/codeception/common/unit/emails/TemplateWithRendererTest.php 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()); + } + +}