2017-04-25 04:39:59 +05:30
|
|
|
<?php
|
2019-06-17 02:29:19 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\emails;
|
2017-04-25 04:39:59 +05:30
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
use common\emails\exceptions\CannotRenderEmailException;
|
|
|
|
use common\emails\RendererInterface;
|
2017-04-25 04:39:59 +05:30
|
|
|
use common\emails\TemplateWithRenderer;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\unit\TestCase;
|
2019-06-17 02:29:19 +05:30
|
|
|
use Exception;
|
|
|
|
use Yii;
|
2017-04-25 04:39:59 +05:30
|
|
|
use yii\mail\MailerInterface;
|
|
|
|
use yii\mail\MessageInterface;
|
|
|
|
|
|
|
|
class TemplateWithRendererTest extends TestCase {
|
2019-06-17 02:29:19 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 testSend() {
|
|
|
|
$this->runTestForSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
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;
|
2017-04-25 04:39:59 +05:30
|
|
|
}
|
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
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']);
|
2017-04-25 04:39:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|