mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Обновлены тесты для всех форм, что отправляли письма
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\_support\queue;
|
||||
|
||||
use Codeception\Exception\ModuleException;
|
||||
use Codeception\Module;
|
||||
use Codeception\Module\Yii2;
|
||||
|
||||
class CodeceptionQueueHelper extends Module {
|
||||
|
||||
/**
|
||||
* Returns last sent message
|
||||
*
|
||||
* @return \yii\queue\JobInterface|null
|
||||
*/
|
||||
public function grabLastQueuedJob() {
|
||||
$messages = $this->grabQueueJobs();
|
||||
return end($messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of all sent amqp messages.
|
||||
* Each message is `\PhpAmqpLib\Message\AMQPMessage` instance.
|
||||
* Useful to perform additional checks using `Asserts` module.
|
||||
*
|
||||
* @param string|null $exchange
|
||||
* @return \yii\queue\JobInterface[]
|
||||
* @throws ModuleException
|
||||
*/
|
||||
public function grabQueueJobs() {
|
||||
$amqp = $this->grabComponent('queue');
|
||||
if (!$amqp instanceof Queue) {
|
||||
throw new ModuleException($this, 'AMQP module is not mocked, can\'t test messages');
|
||||
}
|
||||
|
||||
return $amqp->getMessages();
|
||||
}
|
||||
|
||||
private function grabComponent(string $component) {
|
||||
return $this->getYii2()->grabComponent($component);
|
||||
}
|
||||
|
||||
private function getYii2(): Yii2 {
|
||||
$yii2 = $this->getModule('Yii2');
|
||||
if (!$yii2 instanceof Yii2) {
|
||||
throw new ModuleException($this, 'Yii2 module must be configured');
|
||||
}
|
||||
|
||||
return $yii2;
|
||||
}
|
||||
|
||||
}
|
32
tests/codeception/common/_support/queue/Queue.php
Normal file
32
tests/codeception/common/_support/queue/Queue.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\_support\queue;
|
||||
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\queue\Queue as BaseQueue;
|
||||
|
||||
class Queue extends BaseQueue {
|
||||
|
||||
private $messages = [];
|
||||
|
||||
public function push($job) {
|
||||
$this->messages[] = $job;
|
||||
}
|
||||
|
||||
public function status($id) {
|
||||
throw new NotSupportedException('Status is not supported in the driver.');
|
||||
}
|
||||
|
||||
public function getMessages() {
|
||||
return $this->messages;
|
||||
}
|
||||
|
||||
protected function pushMessage($message, $ttr, $delay, $priority) {
|
||||
// This function is abstract, but will be not called
|
||||
}
|
||||
|
||||
public function __set($name, $value) {
|
||||
// Yii2 components may contains some configuration
|
||||
// But we just ignore it for this mock component
|
||||
}
|
||||
|
||||
}
|
@@ -2,17 +2,12 @@
|
||||
namespace tests\codeception\common\unit\emails;
|
||||
|
||||
use common\emails\EmailHelper;
|
||||
use common\models\Account;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
|
||||
class EmailHelperTest extends TestCase {
|
||||
|
||||
public function testBuildTo() {
|
||||
/** @var Account|\Mockery\MockInterface $account */
|
||||
$account = mock(Account::class)->makePartial();
|
||||
$account->username = 'mock-username';
|
||||
$account->email = 'mock@ely.by';
|
||||
$this->assertEquals(['mock@ely.by' => 'mock-username'], EmailHelper::buildTo($account));
|
||||
$this->assertSame(['mock@ely.by' => 'username'], EmailHelper::buildTo('username', 'mock@ely.by'));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\tasks\SendCurrentEmailConfirmation;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class SendCurrentEmailConfirmationTest extends TestCase {
|
||||
|
||||
public function testCreateFromConfirmation() {
|
||||
$account = new Account();
|
||||
$account->username = 'mock-username';
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|CurrentEmailConfirmation $confirmation */
|
||||
$confirmation = mock(CurrentEmailConfirmation::class)->makePartial();
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
|
||||
$result = SendCurrentEmailConfirmation::createFromConfirmation($confirmation);
|
||||
$this->assertInstanceOf(SendCurrentEmailConfirmation::class, $result);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
$this->assertSame('mock@ely.by', $result->email);
|
||||
$this->assertSame('ABCDEFG', $result->code);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new SendCurrentEmailConfirmation();
|
||||
$task->username = 'mock-username';
|
||||
$task->email = 'mock@ely.by';
|
||||
$task->code = 'GFEDCBA';
|
||||
|
||||
$task->execute(mock(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 change E-mail confirmation', $email->getSubject());
|
||||
$children = $email->getSwiftMessage()->getChildren()[0];
|
||||
$this->assertContains('GFEDCBA', $children->getBody());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\tasks\SendNewEmailConfirmation;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class SendNewEmailConfirmationTest extends TestCase {
|
||||
|
||||
public function testCreateFromConfirmation() {
|
||||
$account = new Account();
|
||||
$account->username = 'mock-username';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|NewEmailConfirmation $confirmation */
|
||||
$confirmation = mock(NewEmailConfirmation::class)->makePartial();
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$confirmation->shouldReceive('getAccount')->andReturn($account);
|
||||
$confirmation->shouldReceive('getNewEmail')->andReturn('new-email@ely.by');
|
||||
|
||||
$result = SendNewEmailConfirmation::createFromConfirmation($confirmation);
|
||||
$this->assertInstanceOf(SendNewEmailConfirmation::class, $result);
|
||||
$this->assertSame('mock-username', $result->username);
|
||||
$this->assertSame('new-email@ely.by', $result->email);
|
||||
$this->assertSame('ABCDEFG', $result->code);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new SendNewEmailConfirmation();
|
||||
$task->username = 'mock-username';
|
||||
$task->email = 'mock@ely.by';
|
||||
$task->code = 'GFEDCBA';
|
||||
|
||||
$task->execute(mock(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 new E-mail confirmation', $email->getSubject());
|
||||
$children = $email->getSwiftMessage()->getChildren()[0];
|
||||
$this->assertContains('GFEDCBA', $children->getBody());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\tasks\SendPasswordRecoveryEmail;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class SendPasswordRecoveryEmailTest extends TestCase {
|
||||
|
||||
public function testCreateFromConfirmation() {
|
||||
$account = new Account();
|
||||
$account->username = 'mock-username';
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'id';
|
||||
|
||||
/** @var \Mockery\Mock|ForgotPassword $confirmation */
|
||||
$confirmation = mock(ForgotPassword::class)->makePartial();
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$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);
|
||||
$this->assertSame('http://localhost/recover-password/ABCDEFG', $result->link);
|
||||
$this->assertSame('id', $result->locale);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$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';
|
||||
|
||||
$task->execute(mock(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 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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class SendRegistrationEmailTest extends TestCase {
|
||||
|
||||
public function testCreateFromConfirmation() {
|
||||
$account = new Account();
|
||||
$account->username = 'mock-username';
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'ru';
|
||||
|
||||
/** @var \Mockery\Mock|RegistrationConfirmation $confirmation */
|
||||
$confirmation = mock(RegistrationConfirmation::class)->makePartial();
|
||||
$confirmation->key = 'ABCDEFG';
|
||||
$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);
|
||||
$this->assertSame('http://localhost/activation/ABCDEFG', $result->link);
|
||||
$this->assertSame('ru', $result->locale);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new SendRegistrationEmail();
|
||||
$task->username = 'mock-username';
|
||||
$task->email = 'mock@ely.by';
|
||||
$task->code = 'GFEDCBA';
|
||||
$task->link = 'https://account.ely.by/activation/ABCDEFG';
|
||||
$task->locale = 'ru';
|
||||
|
||||
$task->execute(mock(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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user