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,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']);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user