2017-11-27 04:59:15 +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\tasks;
|
2017-11-27 04:59:15 +05:30
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
use common\emails\RendererInterface;
|
2017-11-27 04:59:15 +05:30
|
|
|
use common\models\Account;
|
2019-12-14 00:57:13 +05:30
|
|
|
use common\models\AccountQuery;
|
2017-11-27 04:59:15 +05:30
|
|
|
use common\models\confirmations\ForgotPassword;
|
|
|
|
use common\tasks\SendPasswordRecoveryEmail;
|
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;
|
2019-06-17 02:29:19 +05:30
|
|
|
use Yii;
|
2017-11-27 04:59:15 +05:30
|
|
|
use yii\queue\Queue;
|
|
|
|
|
|
|
|
class SendPasswordRecoveryEmailTest extends TestCase {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private RendererInterface&MockObject $renderer;
|
2019-06-17 02:29:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testCreateFromConfirmation(): void {
|
2017-11-27 04:59:15 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->username = 'mock-username';
|
|
|
|
$account->email = 'mock@ely.by';
|
|
|
|
$account->lang = 'id';
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
$accountQuery = $this->createMock(AccountQuery::class);
|
|
|
|
$accountQuery->method('findFor')->willReturn($account);
|
|
|
|
|
|
|
|
$confirmation = $this->createPartialMock(ForgotPassword::class, ['getAccount']);
|
|
|
|
$confirmation->method('getAccount')->willReturn($accountQuery);
|
2017-11-27 04:59:15 +05:30
|
|
|
$confirmation->key = 'ABCDEFG';
|
|
|
|
|
|
|
|
$result = SendPasswordRecoveryEmail::createFromConfirmation($confirmation);
|
|
|
|
$this->assertSame('mock-username', $result->username);
|
|
|
|
$this->assertSame('mock@ely.by', $result->email);
|
|
|
|
$this->assertSame('ABCDEFG', $result->code);
|
|
|
|
$this->assertSame('http://localhost/recover-password/ABCDEFG', $result->link);
|
|
|
|
$this->assertSame('id', $result->locale);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testExecute(): void {
|
2017-11-27 04:59:15 +05:30
|
|
|
$task = new SendPasswordRecoveryEmail();
|
|
|
|
$task->username = 'mock-username';
|
|
|
|
$task->email = 'mock@ely.by';
|
|
|
|
$task->code = 'GFEDCBA';
|
|
|
|
$task->link = 'https://account.ely.by/recover-password/ABCDEFG';
|
|
|
|
$task->locale = 'ru';
|
|
|
|
|
2019-06-17 02:29:19 +05:30
|
|
|
$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');
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
$task->execute($this->createMock(Queue::class));
|
2017-11-27 04:59:15 +05:30
|
|
|
|
|
|
|
$this->tester->canSeeEmailIsSent(1);
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @var \yii\symfonymailer\Message $email */
|
2017-11-27 04:59:15 +05:30
|
|
|
$email = $this->tester->grabSentEmails()[0];
|
|
|
|
$this->assertSame(['mock@ely.by' => 'mock-username'], $email->getTo());
|
|
|
|
$this->assertSame('Ely.by Account forgot password', $email->getSubject());
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->assertSame('mock-template', $email->getSymfonyEmail()->getHtmlBody());
|
2019-06-17 02:29:19 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
protected function _before(): void {
|
2019-06-17 02:29:19 +05:30
|
|
|
parent::_before();
|
|
|
|
|
|
|
|
$this->renderer = $this->createMock(RendererInterface::class);
|
|
|
|
Yii::$app->set('emailsRenderer', $this->renderer);
|
2017-11-27 04:59:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|