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\CannotSendEmailException;
|
2017-04-25 04:39:59 +05:30
|
|
|
use common\emails\Template;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\unit\TestCase;
|
2024-12-02 15:40:55 +05:30
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-04-25 04:39:59 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\mail\MailerInterface;
|
|
|
|
use yii\mail\MessageInterface;
|
|
|
|
|
|
|
|
class TemplateTest extends TestCase {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private Template&MockObject $template;
|
2019-06-17 02:29:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private MailerInterface&MockObject $mailer;
|
2019-06-17 02:29:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private string $initialFromEmail;
|
2019-06-17 02:29:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetters(): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
$this->assertSame(['find-me' => 'Ely.by Accounts'], $this->template->getFrom());
|
|
|
|
$this->assertSame([], $this->template->getParams());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testSend(): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
$this->runTestForSend(true);
|
2017-04-25 04:39:59 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testNotSend(): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
$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'];
|
2017-04-25 04:39:59 +05:30
|
|
|
Yii::$app->params['fromEmail'] = 'find-me';
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
protected function _after(): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
parent::_after();
|
|
|
|
Yii::$app->params['fromEmail'] = $this->initialFromEmail;
|
2017-04-25 04:39:59 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private function runTestForSend(bool $sendResult): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
$this->template->expects($this->once())->method('getSubject')->willReturn('mock-subject');
|
|
|
|
$this->template->expects($this->once())->method('getView')->willReturn('mock-view');
|
|
|
|
|
|
|
|
$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']);
|
2017-04-25 04:39:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|