mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Add tests for the legacy tokens
This commit is contained in:
@@ -52,7 +52,11 @@ class FixtureHelper extends Module {
|
||||
'usernamesHistory' => fixtures\UsernameHistoryFixture::class,
|
||||
'oauthClients' => fixtures\OauthClientFixture::class,
|
||||
'oauthSessions' => fixtures\OauthSessionFixture::class,
|
||||
'legacyOauthSessionsScopes' => fixtures\LegacyOauthSessionScopeFixtures::class,
|
||||
'legacyOauthAccessTokens' => fixtures\LegacyOauthAccessTokenFixture::class,
|
||||
'legacyOauthAccessTokensScopes' => fixtures\LegacyOauthAccessTokenScopeFixture::class,
|
||||
'oauthRefreshTokens' => fixtures\OauthRefreshTokensFixture::class,
|
||||
'legacyOauthRefreshTokens' => fixtures\LegacyOauthRefreshTokenFixture::class,
|
||||
'minecraftAccessKeys' => fixtures\MinecraftAccessKeyFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
82
common/tests/_support/Redis/Fixture.php
Normal file
82
common/tests/_support/Redis/Fixture.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\_support\Redis;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use yii\base\ArrayAccessTrait;
|
||||
use yii\di\Instance;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\Json;
|
||||
use yii\redis\Connection;
|
||||
use yii\test\FileFixtureTrait;
|
||||
use yii\test\Fixture as BaseFixture;
|
||||
|
||||
class Fixture extends BaseFixture {
|
||||
use ArrayAccessTrait;
|
||||
use FileFixtureTrait;
|
||||
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
public $redis = 'redis';
|
||||
|
||||
public $keysPrefix = '';
|
||||
|
||||
public $keysPostfix = '';
|
||||
|
||||
public $data = [];
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
$this->redis = Instance::ensure($this->redis, Connection::class);
|
||||
}
|
||||
|
||||
public function load() {
|
||||
$this->data = [];
|
||||
foreach ($this->getData() as $key => $data) {
|
||||
$key = $this->buildKey($key);
|
||||
$preparedData = $this->prepareData($data);
|
||||
if (is_array($preparedData)) {
|
||||
$this->redis->sadd($key, ...$preparedData);
|
||||
} else {
|
||||
$this->redis->set($key, $preparedData);
|
||||
}
|
||||
|
||||
$this->data[$key] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
public function unload() {
|
||||
$this->redis->flushdb();
|
||||
}
|
||||
|
||||
protected function getData(): array {
|
||||
return $this->loadData($this->dataFile);
|
||||
}
|
||||
|
||||
protected function prepareData($input) {
|
||||
if (is_string($input)) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
if (is_int($input) || is_bool($input)) {
|
||||
return (string)$input;
|
||||
}
|
||||
|
||||
if (is_array($input)) {
|
||||
if (!ArrayHelper::isAssociative($input)) {
|
||||
return $input;
|
||||
}
|
||||
|
||||
return Json::encode($input);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unsupported input type');
|
||||
}
|
||||
|
||||
protected function buildKey($key): string {
|
||||
return $this->keysPrefix . $key . $this->keysPostfix;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user