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