Реализован и оттестирован CleanupController

This commit is contained in:
ErickSkrauch 2016-11-23 23:41:33 +03:00
parent 5c8bd20761
commit 7c9e856453
6 changed files with 92 additions and 2 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace console\controllers;
use common\models\OauthAccessToken;
use yii\console\Controller;
class CleanupController extends Controller {
public function actionAccessTokens() {
$accessTokens = OauthAccessToken::find()
->andWhere(['<', 'expire_time', time()])
->each(1000);
foreach($accessTokens as $token) {
/** @var OauthAccessToken $token */
$token->delete();
}
return self::EXIT_CODE_NORMAL;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace tests\codeception\common\fixtures;
use common\models\OauthAccessToken;
use yii\test\ActiveFixture;
class OauthAccessTokenFixture extends ActiveFixture {
public $modelClass = OauthAccessToken::class;
public $dataFile = '@tests/codeception/common/fixtures/data/oauth-access-tokens.php';
public $depends = [
OauthSessionFixture::class,
];
}

View File

@ -0,0 +1,13 @@
<?php
return [
'admin-ely' => [
'access_token' => '07541285-831e-1e47-e314-b950309a6fca',
'session_id' => 1,
'expire_time' => time() + 3600,
],
'admin-ely-expired' => [
'access_token' => '2977ec21-3022-96f8-544db-2e1df936908',
'session_id' => 1,
'expire_time' => time() - 3600,
],
];

View File

@ -6,7 +6,7 @@ return [
'name' => 'Ely.by',
'description' => 'Всем знакомое елуби',
'redirect_uri' => 'http://ely.by',
'account_id' => NULL,
'account_id' => null,
'is_trusted' => 0,
'created_at' => 1455309271,
],
@ -16,7 +16,7 @@ return [
'name' => 'TLauncher',
'description' => 'Лучший альтернативный лаунчер для Minecraft с большим количеством версий и их модификаций, а также возмоностью входа как с лицензионным аккаунтом, так и без него.',
'redirect_uri' => '',
'account_id' => NULL,
'account_id' => null,
'is_trusted' => 0,
'created_at' => 1455318468,
],

View File

@ -1,3 +1,10 @@
<?php
return [
'admin-ely' => [
'id' => 1,
'owner_type' => 'user',
'owner_id' => 1,
'client_id' => 'ely',
'client_redirect_uri' => 'http://ely.by/authorization/oauth',
],
];

View File

@ -0,0 +1,31 @@
<?php
namespace codeception\console\unit\controllers;
use common\models\OauthAccessToken;
use console\controllers\CleanupController;
use tests\codeception\common\fixtures\OauthAccessTokenFixture;
use tests\codeception\console\unit\TestCase;
use Yii;
class CleanupControllerTest extends TestCase {
public function _fixtures() {
return [
'accessTokens' => OauthAccessTokenFixture::class,
];
}
public function testActionAccessTokens() {
/** @var OauthAccessToken $validAccessToken */
$validAccessToken = $this->tester->grabFixture('accessTokens', 'admin-ely');
/** @var OauthAccessToken $expiredAccessToken */
$expiredAccessToken = $this->tester->grabFixture('accessTokens', 'admin-ely-expired');
$controller = new CleanupController('cleanup', Yii::$app);
$this->assertEquals(0, $controller->actionAccessTokens());
$this->tester->canSeeRecord(OauthAccessToken::class, ['access_token' => $validAccessToken->access_token]);
$this->tester->cantSeeRecord(OauthAccessToken::class, ['access_token' => $expiredAccessToken->access_token]);
}
}