Refactor emails models objects, rework related tests

This commit is contained in:
ErickSkrauch
2019-06-16 23:59:19 +03:00
parent 1bf249030f
commit 70d1999d55
24 changed files with 522 additions and 196 deletions

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace common\tests\_support;
use common\components\EmailsRenderer\Component;
class EmailsRenderer extends Component {
public function render(string $templateName, string $locale, array $params = []): string {
return 'template';
}
}

View File

@@ -1,47 +1,73 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails;
use common\emails\exceptions\CannotSendEmailException;
use common\emails\Template;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use Yii;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
class TemplateTest extends TestCase {
use ProtectedCaller;
public function testConstructor() {
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class, ['find-me'])->makePartial();
$this->assertSame('find-me', $template->getTo());
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
/**
* @var Template|\PHPUnit\Framework\MockObject\MockObject $template
*/
private $template;
/**
* @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $mailer;
/**
* @var string
*/
private $initialFromEmail;
public function testGetters() {
$this->assertSame(['find-me' => 'Ely.by Accounts'], $this->template->getFrom());
$this->assertSame([], $this->template->getParams());
}
public function testGetFrom() {
public function testSend() {
$this->runTestForSend(true);
}
public function testNotSend() {
$this->expectException(CannotSendEmailException::class);
$this->runTestForSend(false);
}
protected function _before() {
parent::_before();
$this->mailer = $this->createMock(MailerInterface::class);
$this->template = $this->getMockForAbstractClass(Template::class, [$this->mailer]);
$this->initialFromEmail = Yii::$app->params['fromEmail'];
Yii::$app->params['fromEmail'] = 'find-me';
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class)->makePartial();
$this->assertSame(['find-me' => 'Ely.by Accounts'], $template->getFrom());
}
public function testGetParams() {
/** @var Template|\Mockery\MockInterface $template */
$template = mock(Template::class)->makePartial();
$this->assertSame([], $template->getParams());
protected function _after() {
parent::_after();
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
}
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->assertSame(['to@ely.by' => 'To'], $message->getTo());
$this->assertSame(['from@ely.by' => 'Ely.by Accounts'], $message->getFrom());
$this->assertSame('mock-subject', $message->getSubject());
private function runTestForSend(bool $sendResult) {
$this->template->expects($this->once())->method('getSubject')->willReturn('mock-subject');
$this->template->expects($this->once())->method('getView')->willReturn('mock-view');
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $message */
$message = $this->createMock(MessageInterface::class);
$message->expects($this->once())->method('setTo')->with(['to@ely.by' => 'To'])->willReturnSelf();
$message->expects($this->once())->method('setFrom')->with(['find-me' => 'Ely.by Accounts'])->willReturnSelf();
$message->expects($this->once())->method('setSubject')->with('mock-subject')->willReturnSelf();
$message->expects($this->once())->method('send')->willReturn($sendResult);
$this->mailer->expects($this->once())->method('compose')->with('mock-view', [])->willReturn($message);
$this->template->send(['to@ely.by' => 'To']);
}
}

View File

@@ -1,49 +1,103 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails;
use common\components\EmailsRenderer\Component;
use common\emails\exceptions\CannotRenderEmailException;
use common\emails\RendererInterface;
use common\emails\TemplateWithRenderer;
use common\tests\_support\ProtectedCaller;
use common\tests\unit\TestCase;
use Ely\Email\TemplateBuilder;
use Exception;
use Yii;
use yii\mail\MailerInterface;
use yii\mail\MessageInterface;
class TemplateWithRendererTest extends TestCase {
use ProtectedCaller;
public function testConstructor() {
/** @var TemplateWithRenderer|\Mockery\MockInterface $template */
$template = mock(TemplateWithRenderer::class, ['mock-to', 'mock-locale'])->makePartial();
$this->assertSame('mock-to', $template->getTo());
$this->assertSame('mock-locale', $template->getLocale());
$this->assertInstanceOf(MailerInterface::class, $template->getMailer());
$this->assertInstanceOf(Component::class, $template->getRenderer());
/**
* @var TemplateWithRenderer|\PHPUnit\Framework\MockObject\MockObject $template
*/
private $template;
/**
* @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $mailer;
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
/**
* @var string
*/
private $initialFromEmail;
public function testGetLocale() {
$this->assertSame('en', $this->template->getLocale());
$this->template->setLocale('find me');
$this->assertSame('find me', $this->template->getLocale());
}
public function testCreateMessage() {
/** @var TemplateBuilder|\Mockery\MockInterface $templateBuilder */
$templateBuilder = mock(TemplateBuilder::class)->makePartial();
$templateBuilder->shouldReceive('render')->andReturn('mock-html');
public function testSend() {
$this->runTestForSend();
}
/** @var Component|\Mockery\MockInterface $renderer */
$renderer = mock(Component::class)->makePartial();
$renderer->shouldReceive('getTemplate')->with('mock-template')->andReturn($templateBuilder);
public function testSendWithRenderError() {
$renderException = new Exception('find me');
try {
$this->runTestForSend($renderException);
} catch (CannotRenderEmailException $e) {
// Catch exception manually to assert the previous exception
$this->assertSame('Unable to render a template', $e->getMessage());
$this->assertSame($renderException, $e->getPrevious());
/** @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->assertSame(['to@ely.by' => 'To'], $message->getTo());
$this->assertSame(['from@ely.by' => 'From'], $message->getFrom());
$this->assertSame('mock-subject', $message->getSubject());
$this->assertSame('mock-html', $message->getSwiftMessage()->getBody());
return;
}
$this->assertFalse(true, 'no exception was thrown');
}
protected function _before() {
parent::_before();
$this->mailer = $this->createMock(MailerInterface::class);
$this->renderer = $this->createMock(RendererInterface::class);
$this->template = $this->getMockForAbstractClass(TemplateWithRenderer::class, [$this->mailer, $this->renderer]);
$this->initialFromEmail = Yii::$app->params['fromEmail'];
Yii::$app->params['fromEmail'] = 'find-me';
}
protected function _after() {
parent::_after();
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
}
private function runTestForSend($renderException = null) {
$renderMethodExpectation = $this->renderer->expects($this->once())->method('render')->with('mock-template', 'mock-locale', []);
if ($renderException === null) {
$renderMethodExpectation->willReturn('mock-template-contents');
$times = [$this, 'once'];
} else {
$renderMethodExpectation->willThrowException($renderException);
$times = [$this, 'any'];
}
$this->template->expects($times())->method('getSubject')->willReturn('mock-subject');
$this->template->expects($times())->method('getTemplateName')->willReturn('mock-template');
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $message */
$message = $this->createMock(MessageInterface::class);
$message->expects($times())->method('setTo')->with(['to@ely.by' => 'To'])->willReturnSelf();
$message->expects($times())->method('setHtmlBody')->with('mock-template-contents')->willReturnSelf();
$message->expects($times())->method('setFrom')->with(['find-me' => 'Ely.by Accounts'])->willReturnSelf();
$message->expects($times())->method('setSubject')->with('mock-subject')->willReturnSelf();
$message->expects($times())->method('send')->willReturn(true);
$this->mailer->expects($times())->method('compose')->willReturn($message);
$this->template->setLocale('mock-locale');
$this->template->send(['to@ely.by' => 'To']);
}
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\templates\ChangeEmail;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ChangeEmailTest extends TestCase {
/**
* @var ChangeEmail()|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setKey('mock-key');
$params = $this->template->getParams();
$this->assertSame('mock-key', $params['key']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
$this->template = new ChangeEmail($mailer);
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\templates\ConfirmNewEmail;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ConfirmNewEmailTest extends TestCase {
/**
* @var ConfirmNewEmail|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setUsername('mock-username');
$this->template->setKey('mock-key');
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-key', $params['key']);
}
/**
* @dataProvider getInvalidCallsCases
*/
public function testInvalidCallOfParams(?string $username, ?string $key) {
$this->expectException(InvalidCallException::class);
$username !== null && $this->template->setUsername($username);
$key !== null && $this->template->setKey($key);
$this->template->getParams();
}
public function getInvalidCallsCases() {
yield [null, null];
yield ['value', null];
yield [null, 'value'];
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
$this->template = new ConfirmNewEmail($mailer);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\RendererInterface;
use common\emails\templates\ForgotPasswordEmail;
use common\emails\templates\ForgotPasswordParams;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class ForgotPasswordEmailTest extends TestCase {
/**
* @var ForgotPasswordEmail|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setParams(new ForgotPasswordParams('mock-username', 'mock-code', 'mock-link'));
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-code', $params['code']);
$this->assertSame('mock-link', $params['link']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
/** @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject $renderer */
$renderer = $this->createMock(RendererInterface::class);
$this->template = new ForgotPasswordEmail($mailer, $renderer);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\emails\templates;
use common\emails\RendererInterface;
use common\emails\templates\RegistrationEmail;
use common\emails\templates\RegistrationEmailParams;
use common\tests\unit\TestCase;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
class RegistrationEmailTest extends TestCase {
/**
* @var RegistrationEmail()|\PHPUnit\Framework\MockObject\MockObject
*/
private $template;
public function testParams() {
$this->template->setParams(new RegistrationEmailParams('mock-username', 'mock-code', 'mock-link'));
$params = $this->template->getParams();
$this->assertSame('mock-username', $params['username']);
$this->assertSame('mock-code', $params['code']);
$this->assertSame('mock-link', $params['link']);
}
public function testInvalidCallOfParams() {
$this->expectException(InvalidCallException::class);
$this->template->getParams();
}
protected function _before() {
parent::_before();
/** @var MailerInterface|\PHPUnit\Framework\MockObject\MockObject $mailer */
$mailer = $this->createMock(MailerInterface::class);
/** @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject $renderer */
$renderer = $this->createMock(RendererInterface::class);
$this->template = new RegistrationEmail($mailer, $renderer);
}
}

View File

@@ -1,14 +1,23 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\emails\RendererInterface;
use common\models\Account;
use common\models\confirmations\ForgotPassword;
use common\tasks\SendPasswordRecoveryEmail;
use common\tests\unit\TestCase;
use Yii;
use yii\queue\Queue;
class SendPasswordRecoveryEmailTest extends TestCase {
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
public function testCreateFromConfirmation() {
$account = new Account();
$account->username = 'mock-username';
@@ -21,7 +30,6 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$confirmation->shouldReceive('getAccount')->andReturn($account);
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
$this->assertInstanceOf(SendPasswordRecoveryEmail::class, $result);
$this->assertSame('mock-username', $result->username);
$this->assertSame('mock@ely.by', $result->email);
$this->assertSame('ABCDEFG', $result->code);
@@ -37,6 +45,12 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$task->link = 'https://account.ely.by/recover-password/ABCDEFG';
$task->locale = 'ru';
$this->renderer->expects($this->once())->method('render')->with('forgotPassword', 'ru', [
'username' => 'mock-username',
'code' => 'GFEDCBA',
'link' => 'https://account.ely.by/recover-password/ABCDEFG',
])->willReturn('mock-template');
$task->execute(mock(Queue::class));
$this->tester->canSeeEmailIsSent(1);
@@ -44,10 +58,14 @@ class SendPasswordRecoveryEmailTest extends TestCase {
$email = $this->tester->grabSentEmails()[0];
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
$this->assertSame('Ely.by Account forgot password', $email->getSubject());
$body = $email->getSwiftMessage()->getBody();
$this->assertContains('Привет, mock-username', $body);
$this->assertContains('GFEDCBA', $body);
$this->assertContains('https://account.ely.by/recover-password/ABCDEFG', $body);
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
}
protected function _before() {
parent::_before();
$this->renderer = $this->createMock(RendererInterface::class);
Yii::$app->set('emailsRenderer', $this->renderer);
}
}

View File

@@ -1,14 +1,23 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\emails\RendererInterface;
use common\models\Account;
use common\models\confirmations\RegistrationConfirmation;
use common\tasks\SendRegistrationEmail;
use common\tests\unit\TestCase;
use Yii;
use yii\queue\Queue;
class SendRegistrationEmailTest extends TestCase {
/**
* @var RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $renderer;
public function testCreateFromConfirmation() {
$account = new Account();
$account->username = 'mock-username';
@@ -21,7 +30,6 @@ class SendRegistrationEmailTest extends TestCase {
$confirmation->shouldReceive('getAccount')->andReturn($account);
$result = SendRegistrationEmail::createFromConfirmation($confirmation);
$this->assertInstanceOf(SendRegistrationEmail::class, $result);
$this->assertSame('mock-username', $result->username);
$this->assertSame('mock@ely.by', $result->email);
$this->assertSame('ABCDEFG', $result->code);
@@ -37,17 +45,27 @@ class SendRegistrationEmailTest extends TestCase {
$task->link = 'https://account.ely.by/activation/ABCDEFG';
$task->locale = 'ru';
$task->execute(mock(Queue::class));
$this->renderer->expects($this->once())->method('render')->with('register', 'ru', [
'username' => 'mock-username',
'code' => 'GFEDCBA',
'link' => 'https://account.ely.by/activation/ABCDEFG',
])->willReturn('mock-template');
$task->execute($this->createMock(Queue::class));
$this->tester->canSeeEmailIsSent(1);
/** @var \yii\swiftmailer\Message $email */
$email = $this->tester->grabSentEmails()[0];
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
$this->assertSame('Ely.by Account registration', $email->getSubject());
$body = $email->getSwiftMessage()->getBody();
$this->assertContains('Привет, mock-username', $body);
$this->assertContains('GFEDCBA', $body);
$this->assertContains('https://account.ely.by/activation/ABCDEFG', $body);
$this->assertSame('mock-template', $email->getSwiftMessage()->getBody());
}
protected function _before() {
parent::_before();
$this->renderer = $this->createMock(RendererInterface::class);
Yii::$app->set('emailsRenderer', $this->renderer);
}
}