mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Rework tests structure. Upgrade codeception to 2.5.3. Merge params configuration into app configuration.
This commit is contained in:
44
common/tests/unit/tasks/ClearAccountSessionsTest.php
Normal file
44
common/tests/unit/tasks/ClearAccountSessionsTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\tasks\ClearAccountSessions;
|
||||
use common\tests\fixtures;
|
||||
use common\tests\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
/**
|
||||
* @covers \common\tasks\ClearAccountSessions
|
||||
*/
|
||||
class ClearAccountSessionsTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'accounts' => fixtures\AccountFixture::class,
|
||||
'oauthSessions' => fixtures\OauthSessionFixture::class,
|
||||
'minecraftAccessKeys' => fixtures\MinecraftAccessKeyFixture::class,
|
||||
'authSessions' => fixtures\AccountSessionFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateFromAccount() {
|
||||
$account = new Account();
|
||||
$account->id = 123;
|
||||
$task = ClearAccountSessions::createFromAccount($account);
|
||||
$this->assertSame(123, $task->accountId);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
/** @var \common\models\Account $bannedAccount */
|
||||
$bannedAccount = $this->tester->grabFixture('accounts', 'banned-account');
|
||||
$task = new ClearAccountSessions();
|
||||
$task->accountId = $bannedAccount->id;
|
||||
$task->execute(mock(Queue::class));
|
||||
$this->assertEmpty($bannedAccount->sessions);
|
||||
$this->assertEmpty($bannedAccount->minecraftAccessKeys);
|
||||
$this->assertEmpty($bannedAccount->oauthSessions);
|
||||
}
|
||||
|
||||
}
|
55
common/tests/unit/tasks/ClearOauthSessionsTest.php
Normal file
55
common/tests/unit/tasks/ClearOauthSessionsTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\OauthClient;
|
||||
use common\models\OauthSession;
|
||||
use common\tasks\ClearOauthSessions;
|
||||
use common\tests\fixtures;
|
||||
use common\tests\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class ClearOauthSessionsTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'oauthClients' => fixtures\OauthClientFixture::class,
|
||||
'oauthSessions' => fixtures\OauthSessionFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateFromClient() {
|
||||
$client = new OauthClient();
|
||||
$client->id = 'mocked-id';
|
||||
|
||||
$result = ClearOauthSessions::createFromOauthClient($client);
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $result);
|
||||
$this->assertSame('mocked-id', $result->clientId);
|
||||
$this->assertNull($result->notSince);
|
||||
|
||||
$result = ClearOauthSessions::createFromOauthClient($client, time());
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $result);
|
||||
$this->assertSame('mocked-id', $result->clientId);
|
||||
$this->assertEquals(time(), $result->notSince, '', 1);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->notSince = 1519510065;
|
||||
$task->execute(mock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['id' => 3])->exists());
|
||||
$this->assertTrue(OauthSession::find()->andWhere(['id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->execute(mock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'some-not-exists-client-id';
|
||||
$task->execute(mock(Queue::class));
|
||||
}
|
||||
|
||||
}
|
91
common/tests/unit/tasks/CreateWebHooksDeliveriesTest.php
Normal file
91
common/tests/unit/tasks/CreateWebHooksDeliveriesTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\tasks\CreateWebHooksDeliveries;
|
||||
use common\tasks\DeliveryWebHook;
|
||||
use common\tests\fixtures;
|
||||
use common\tests\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
/**
|
||||
* @covers \common\tasks\CreateWebHooksDeliveries
|
||||
*/
|
||||
class CreateWebHooksDeliveriesTest extends TestCase {
|
||||
|
||||
public function _fixtures(): array {
|
||||
return [
|
||||
'webhooks' => fixtures\WebHooksFixture::class,
|
||||
'webhooksEvents' => fixtures\WebHooksEventsFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateAccountEdit() {
|
||||
$account = new Account();
|
||||
$account->id = 123;
|
||||
$account->username = 'mock-username';
|
||||
$account->uuid = 'afc8dc7a-4bbf-4d3a-8699-68890088cf84';
|
||||
$account->email = 'mock@ely.by';
|
||||
$account->lang = 'en';
|
||||
$account->status = Account::STATUS_ACTIVE;
|
||||
$account->created_at = 1531008814;
|
||||
$changedAttributes = [
|
||||
'username' => 'old-username',
|
||||
'uuid' => 'e05d33e9-ff91-4d26-9f5c-8250f802a87a',
|
||||
'email' => 'old-email@ely.by',
|
||||
'status' => 0,
|
||||
];
|
||||
$result = CreateWebHooksDeliveries::createAccountEdit($account, $changedAttributes);
|
||||
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $result);
|
||||
$this->assertSame('account.edit', $result->type);
|
||||
$this->assertArraySubset([
|
||||
'id' => 123,
|
||||
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
|
||||
'username' => 'mock-username',
|
||||
'email' => 'mock@ely.by',
|
||||
'lang' => 'en',
|
||||
'isActive' => true,
|
||||
'registered' => '2018-07-08T00:13:34+00:00',
|
||||
'changedAttributes' => $changedAttributes,
|
||||
], $result->payloads);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new CreateWebHooksDeliveries();
|
||||
$task->type = 'account.edit';
|
||||
$task->payloads = [
|
||||
'id' => 123,
|
||||
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
|
||||
'username' => 'mock-username',
|
||||
'email' => 'mock@ely.by',
|
||||
'lang' => 'en',
|
||||
'isActive' => true,
|
||||
'registered' => '2018-07-08T00:13:34+00:00',
|
||||
'changedAttributes' => [
|
||||
'username' => 'old-username',
|
||||
'uuid' => 'e05d33e9-ff91-4d26-9f5c-8250f802a87a',
|
||||
'email' => 'old-email@ely.by',
|
||||
'status' => 0,
|
||||
],
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
/** @var DeliveryWebHook[] $tasks */
|
||||
$tasks = $this->tester->grabQueueJobs();
|
||||
$this->assertCount(2, $tasks);
|
||||
|
||||
$this->assertInstanceOf(DeliveryWebHook::class, $tasks[0]);
|
||||
$this->assertSame($task->type, $tasks[0]->type);
|
||||
$this->assertSame($task->payloads, $tasks[0]->payloads);
|
||||
$this->assertSame('http://localhost:80/webhooks/ely', $tasks[0]->url);
|
||||
$this->assertSame('my-secret', $tasks[0]->secret);
|
||||
|
||||
$this->assertInstanceOf(DeliveryWebHook::class, $tasks[1]);
|
||||
$this->assertSame($task->type, $tasks[1]->type);
|
||||
$this->assertSame($task->payloads, $tasks[1]->payloads);
|
||||
$this->assertSame('http://localhost:81/webhooks/ely', $tasks[1]->url);
|
||||
$this->assertNull($tasks[1]->secret);
|
||||
}
|
||||
|
||||
}
|
133
common/tests/unit/tasks/DeliveryWebHookTest.php
Normal file
133
common/tests/unit/tasks/DeliveryWebHookTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\tasks\DeliveryWebHook;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use common\tests\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
/**
|
||||
* @covers \common\tasks\DeliveryWebHook
|
||||
*/
|
||||
class DeliveryWebHookTest extends TestCase {
|
||||
|
||||
private $historyContainer = [];
|
||||
|
||||
/**
|
||||
* @var Response|\GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
private $response;
|
||||
|
||||
public function testCanRetry() {
|
||||
$task = new DeliveryWebHook();
|
||||
$this->assertFalse($task->canRetry(1, new \Exception()));
|
||||
$request = new Request('POST', 'http://localhost');
|
||||
$this->assertTrue($task->canRetry(4, new ConnectException('', $request)));
|
||||
$this->assertTrue($task->canRetry(4, new ServerException('', $request)));
|
||||
$this->assertFalse($task->canRetry(5, new ConnectException('', $request)));
|
||||
$this->assertFalse($task->canRetry(5, new ServerException('', $request)));
|
||||
}
|
||||
|
||||
public function testExecuteSuccessDelivery() {
|
||||
$this->response = new Response();
|
||||
$task = $this->createMockedTask();
|
||||
$task->type = 'account.edit';
|
||||
$task->url = 'http://localhost:81/webhooks/ely';
|
||||
$task->payloads = [
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
/** @var Request $request */
|
||||
$request = $this->historyContainer[0]['request'];
|
||||
$this->assertSame('http://localhost:81/webhooks/ely', (string)$request->getUri());
|
||||
$this->assertStringStartsWith('Account-Ely-Hookshot/', $request->getHeaders()['User-Agent'][0]);
|
||||
$this->assertSame('account.edit', $request->getHeaders()['X-Ely-Accounts-Event'][0]);
|
||||
$this->assertSame('application/x-www-form-urlencoded', $request->getHeaders()['Content-Type'][0]);
|
||||
$this->assertArrayNotHasKey('X-Hub-Signature', $request->getHeaders());
|
||||
$this->assertEquals('key=value&another=value', (string)$request->getBody());
|
||||
}
|
||||
|
||||
public function testExecuteSuccessDeliveryWithSignature() {
|
||||
$this->response = new Response();
|
||||
$task = $this->createMockedTask();
|
||||
$task->type = 'account.edit';
|
||||
$task->url = 'http://localhost:81/webhooks/ely';
|
||||
$task->secret = 'secret';
|
||||
$task->payloads = [
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
/** @var Request $request */
|
||||
$request = $this->historyContainer[0]['request'];
|
||||
$this->assertSame('http://localhost:81/webhooks/ely', (string)$request->getUri());
|
||||
$this->assertStringStartsWith('Account-Ely-Hookshot/', $request->getHeaders()['User-Agent'][0]);
|
||||
$this->assertSame('account.edit', $request->getHeaders()['X-Ely-Accounts-Event'][0]);
|
||||
$this->assertSame('application/x-www-form-urlencoded', $request->getHeaders()['Content-Type'][0]);
|
||||
$this->assertSame('sha1=3c0b1eef564b2d3a5e9c0f2a8302b1b42b3d4784', $request->getHeaders()['X-Hub-Signature'][0]);
|
||||
$this->assertEquals('key=value&another=value', (string)$request->getBody());
|
||||
}
|
||||
|
||||
public function testExecuteHandleClientException() {
|
||||
$this->response = new Response(403);
|
||||
$task = $this->createMockedTask();
|
||||
$task->type = 'account.edit';
|
||||
$task->url = 'http://localhost:81/webhooks/ely';
|
||||
$task->secret = 'secret';
|
||||
$task->payloads = [
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\ServerException
|
||||
*/
|
||||
public function testExecuteUnhandledException() {
|
||||
$this->response = new Response(502);
|
||||
$task = $this->createMockedTask();
|
||||
$task->type = 'account.edit';
|
||||
$task->url = 'http://localhost:81/webhooks/ely';
|
||||
$task->secret = 'secret';
|
||||
$task->payloads = [
|
||||
'key' => 'value',
|
||||
'another' => 'value',
|
||||
];
|
||||
$task->execute(mock(Queue::class));
|
||||
}
|
||||
|
||||
private function createMockedTask(): DeliveryWebHook {
|
||||
$container = &$this->historyContainer;
|
||||
$response = $this->response;
|
||||
|
||||
return new class($container, $response) extends DeliveryWebHook {
|
||||
private $historyContainer;
|
||||
|
||||
private $response;
|
||||
|
||||
public function __construct(array &$historyContainer, $response) {
|
||||
$this->historyContainer = &$historyContainer;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
protected function createStack(): HandlerStack {
|
||||
$stack = parent::createStack();
|
||||
$stack->setHandler(new MockHandler([$this->response]));
|
||||
$stack->push(Middleware::history($this->historyContainer));
|
||||
|
||||
return $stack;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
148
common/tests/unit/tasks/PullMojangUsernameTest.php
Normal file
148
common/tests/unit/tasks/PullMojangUsernameTest.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\components\Mojang\Api;
|
||||
use common\components\Mojang\exceptions\NoContentException;
|
||||
use common\components\Mojang\response\UsernameToUUIDResponse;
|
||||
use common\models\Account;
|
||||
use common\models\MojangUsername;
|
||||
use common\tasks\PullMojangUsername;
|
||||
use common\tests\fixtures\MojangUsernameFixture;
|
||||
use common\tests\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
/**
|
||||
* @covers \common\tasks\PullMojangUsername
|
||||
*/
|
||||
class PullMojangUsernameTest extends TestCase {
|
||||
|
||||
private $expectedResponse;
|
||||
|
||||
/**
|
||||
* @var PullMojangUsername
|
||||
*/
|
||||
private $task;
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'mojangUsernames' => MojangUsernameFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
|
||||
/** @var PullMojangUsername|\PHPUnit_Framework_MockObject_MockObject $task */
|
||||
$task = $this->getMockBuilder(PullMojangUsername::class)
|
||||
->setMethods(['createMojangApi'])
|
||||
->getMock();
|
||||
|
||||
/** @var Api|\PHPUnit_Framework_MockObject_MockObject $apiMock */
|
||||
$apiMock = $this->getMockBuilder(Api::class)
|
||||
->setMethods(['usernameToUUID'])
|
||||
->getMock();
|
||||
|
||||
$apiMock
|
||||
->expects($this->any())
|
||||
->method('usernameToUUID')
|
||||
->willReturnCallback(function() {
|
||||
if ($this->expectedResponse === false) {
|
||||
throw new NoContentException();
|
||||
}
|
||||
|
||||
return $this->expectedResponse;
|
||||
});
|
||||
|
||||
$task
|
||||
->expects($this->any())
|
||||
->method('createMojangApi')
|
||||
->willReturn($apiMock);
|
||||
|
||||
$this->task = $task;
|
||||
}
|
||||
|
||||
public function testCreateFromAccount() {
|
||||
$account = new Account();
|
||||
$account->username = 'find-me';
|
||||
$result = PullMojangUsername::createFromAccount($account);
|
||||
$this->assertSame('find-me', $result->username);
|
||||
}
|
||||
|
||||
public function testExecuteUsernameExists() {
|
||||
$expectedResponse = new UsernameToUUIDResponse();
|
||||
$expectedResponse->id = '069a79f444e94726a5befca90e38aaf5';
|
||||
$expectedResponse->name = 'Notch';
|
||||
$this->expectedResponse = $expectedResponse;
|
||||
|
||||
/** @var \common\models\MojangUsername $mojangUsernameFixture */
|
||||
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
||||
$this->task->username = 'Notch';
|
||||
$this->task->execute(mock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Notch');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
$this->assertGreaterThan($mojangUsernameFixture->last_pulled_at, $mojangUsername->last_pulled_at);
|
||||
$this->assertLessThanOrEqual(time(), $mojangUsername->last_pulled_at);
|
||||
}
|
||||
|
||||
public function testExecuteChangedUsernameExists() {
|
||||
$expectedResponse = new UsernameToUUIDResponse();
|
||||
$expectedResponse->id = '069a79f444e94726a5befca90e38aaf5';
|
||||
$expectedResponse->name = 'Notch';
|
||||
$this->expectedResponse = $expectedResponse;
|
||||
|
||||
/** @var MojangUsername $mojangUsernameFixture */
|
||||
$mojangUsernameFixture = $this->tester->grabFixture('mojangUsernames', 'Notch');
|
||||
$this->task->username = 'Notch';
|
||||
$this->task->execute(mock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Notch');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
$this->assertGreaterThan($mojangUsernameFixture->last_pulled_at, $mojangUsername->last_pulled_at);
|
||||
$this->assertLessThanOrEqual(time(), $mojangUsername->last_pulled_at);
|
||||
}
|
||||
|
||||
public function testExecuteChangedUsernameNotExists() {
|
||||
$expectedResponse = new UsernameToUUIDResponse();
|
||||
$expectedResponse->id = '607153852b8c4909811f507ed8ee737f';
|
||||
$expectedResponse->name = 'Chest';
|
||||
$this->expectedResponse = $expectedResponse;
|
||||
|
||||
$this->task->username = 'Chest';
|
||||
$this->task->execute(mock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne('Chest');
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
}
|
||||
|
||||
public function testExecuteRemoveIfExistsNoMore() {
|
||||
$this->expectedResponse = false;
|
||||
|
||||
$username = $this->tester->grabFixture('mojangUsernames', 'not-exists')['username'];
|
||||
$this->task->username = $username;
|
||||
$this->task->execute(mock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne($username);
|
||||
$this->assertNull($mojangUsername);
|
||||
}
|
||||
|
||||
public function testExecuteUuidUpdated() {
|
||||
$expectedResponse = new UsernameToUUIDResponse();
|
||||
$expectedResponse->id = 'f498513ce8c84773be26ecfc7ed5185d';
|
||||
$expectedResponse->name = 'jeb';
|
||||
$this->expectedResponse = $expectedResponse;
|
||||
|
||||
/** @var MojangUsername $mojangInfo */
|
||||
$mojangInfo = $this->tester->grabFixture('mojangUsernames', 'uuid-changed');
|
||||
$username = $mojangInfo['username'];
|
||||
$this->task->username = $username;
|
||||
$this->task->execute(mock(Queue::class));
|
||||
/** @var MojangUsername|null $mojangUsername */
|
||||
$mojangUsername = MojangUsername::findOne($username);
|
||||
$this->assertInstanceOf(MojangUsername::class, $mojangUsername);
|
||||
$this->assertNotEquals($mojangInfo->uuid, $mojangUsername->uuid);
|
||||
}
|
||||
|
||||
}
|
47
common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php
Normal file
47
common/tests/unit/tasks/SendCurrentEmailConfirmationTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
use common\tasks\SendCurrentEmailConfirmation;
|
||||
use common\tests\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());
|
||||
}
|
||||
|
||||
}
|
47
common/tests/unit/tasks/SendNewEmailConfirmationTest.php
Normal file
47
common/tests/unit/tasks/SendNewEmailConfirmationTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\tasks\SendNewEmailConfirmation;
|
||||
use common\tests\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());
|
||||
}
|
||||
|
||||
}
|
53
common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php
Normal file
53
common/tests/unit/tasks/SendPasswordRecoveryEmailTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\tasks\SendPasswordRecoveryEmail;
|
||||
use common\tests\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);
|
||||
}
|
||||
|
||||
}
|
53
common/tests/unit/tasks/SendRegistrationEmailTest.php
Normal file
53
common/tests/unit/tasks/SendRegistrationEmailTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace common\tests\unit\tasks;
|
||||
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\RegistrationConfirmation;
|
||||
use common\tasks\SendRegistrationEmail;
|
||||
use common\tests\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