mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Refactor emails models objects, rework related tests
This commit is contained in:
@@ -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']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user