Рефакторинг common unit тестов

This commit is contained in:
ErickSkrauch
2016-10-29 03:23:29 +03:00
parent 0e7013d9f5
commit d0548872f1
12 changed files with 88 additions and 103 deletions

View File

@ -8,4 +8,6 @@ class MojangUsernameFixture extends ActiveFixture {
public $modelClass = MojangUsername::class; public $modelClass = MojangUsername::class;
public $dataFile = '@tests/codeception/common/fixtures/data/mojang-usernames.php';
} }

View File

@ -8,6 +8,8 @@ class OauthClientFixture extends ActiveFixture {
public $modelClass = OauthClient::class; public $modelClass = OauthClient::class;
public $dataFile = '@tests/codeception/common/fixtures/data/oauth-clients.php';
public $depends = [ public $depends = [
AccountFixture::class, AccountFixture::class,
]; ];

View File

@ -9,6 +9,8 @@ class OauthSessionFixture extends ActiveFixture {
public $modelClass = OauthSession::class; public $modelClass = OauthSession::class;
public $dataFile = '@tests/codeception/common/fixtures/data/oauth-sessions.php';
public $depends = [ public $depends = [
OauthClientFixture::class, OauthClientFixture::class,
AccountFixture::class, AccountFixture::class,

View File

@ -1,6 +1,8 @@
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: UnitTester class_name: UnitTester
modules:
enabled:
- Yii2:
part: [orm, email, fixtures]
config:
Yii2:
configFile: '../config/api/unit.php'

View File

@ -1,11 +0,0 @@
<?php
namespace tests\codeception\common\unit;
/**
* @inheritdoc
*/
class DbTestCase extends \yii\codeception\DbTestCase
{
public $appConfig = '@tests/codeception/config/common/unit.php';
}

View File

@ -1,8 +1,22 @@
<?php <?php
namespace tests\codeception\common\unit; namespace tests\codeception\common\unit;
class TestCase extends \yii\codeception\TestCase { class TestCase extends \Codeception\Test\Unit {
public $appConfig = '@tests/codeception/config/common/unit.php'; /**
* @var \tests\codeception\common\UnitTester
*/
protected $tester;
/**
* Список фикстур, что будут загружены перед тестом, но после зачистки базы данных
*
* @url http://codeception.com/docs/modules/Yii2#fixtures
*
* @return array
*/
public function _fixtures() {
return [];
}
} }

View File

@ -13,23 +13,19 @@ class DataBehaviorTest extends TestCase {
use ProtectedCaller; use ProtectedCaller;
public function testSetKey() { public function testSetKey() {
$this->specify('setting value should change model data field', function() {
$model = $this->createModel(); $model = $this->createModel();
/** @var DataBehavior $behavior */ /** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior']; $behavior = $model->behaviors['dataBehavior'];
$this->callProtected($behavior, 'setKey', 'my-key', 'my-value'); $this->callProtected($behavior, 'setKey', 'my-key', 'my-value');
expect($model->_data)->equals(serialize(['my-key' => 'my-value'])); $this->assertEquals(serialize(['my-key' => 'my-value']), $model->_data);
});
} }
public function testGetKey() { public function testGetKey() {
$this->specify('getting value from exists data should work', function() {
$model = $this->createModel(); $model = $this->createModel();
$model->_data = serialize(['some-key' => 'some-value']); $model->_data = serialize(['some-key' => 'some-value']);
/** @var DataBehavior $behavior */ /** @var DataBehavior $behavior */
$behavior = $model->behaviors['dataBehavior']; $behavior = $model->behaviors['dataBehavior'];
expect($this->callProtected($behavior, 'getKey', 'some-key'))->equals('some-value'); $this->assertEquals('some-value', $this->callProtected($behavior, 'getKey', 'some-key'));
});
} }
public function testGetData() { public function testGetData() {

View File

@ -12,12 +12,10 @@ class EmailActivationExpirationBehaviorTest extends TestCase {
use ProtectedCaller; use ProtectedCaller;
public function testCalculateTime() { public function testCalculateTime() {
$this->specify('just use create_time and plus passed time', function() {
$behavior = $this->createBehavior(); $behavior = $this->createBehavior();
$time = time(); $time = time();
$behavior->owner->created_at = $time; $behavior->owner->created_at = $time;
expect($this->callProtected($behavior, 'calculateTime', 10))->equals($time + 10); $this->assertEquals($time + 10, $this->callProtected($behavior, 'calculateTime', 10));
});
} }
public function testCompareTime() { public function testCompareTime() {

View File

@ -1,35 +1,27 @@
<?php <?php
namespace codeception\common\unit\models; namespace codeception\common\unit\models;
use Codeception\Specify;
use common\models\AccountSession; use common\models\AccountSession;
use tests\codeception\common\unit\TestCase; use tests\codeception\common\unit\TestCase;
class AccountSessionTest extends TestCase { class AccountSessionTest extends TestCase {
use Specify;
public function testGenerateRefreshToken() { public function testGenerateRefreshToken() {
$this->specify('method call will set refresh_token value', function() {
$model = new AccountSession(); $model = new AccountSession();
$model->generateRefreshToken(); $model->generateRefreshToken();
expect($model->refresh_token)->notNull(); $this->assertNotNull($model->refresh_token, 'method call will set refresh_token value');
});
} }
public function testSetIp() { public function testSetIp() {
$this->specify('method should convert passed ip string to long', function() {
$model = new AccountSession(); $model = new AccountSession();
$model->setIp('127.0.0.1'); $model->setIp('127.0.0.1');
expect($model->last_used_ip)->equals(2130706433); $this->assertEquals(2130706433, $model->last_used_ip, 'method should convert passed ip string to long');
});
} }
public function testGetReadableIp() { public function testGetReadableIp() {
$this->specify('method should convert stored ip long into readable ip string', function() {
$model = new AccountSession(); $model = new AccountSession();
$model->last_used_ip = 2130706433; $model->last_used_ip = 2130706433;
expect($model->getReadableIp())->equals('127.0.0.1'); $this->assertEquals('127.0.0.1', $model->getReadableIp(), 'method should convert stored long into readable ip');
});
} }
} }

View File

@ -6,27 +6,17 @@ use common\components\UserPass;
use common\models\Account; use common\models\Account;
use tests\codeception\common\fixtures\AccountFixture; use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\MojangUsernameFixture; use tests\codeception\common\fixtures\MojangUsernameFixture;
use tests\codeception\common\unit\DbTestCase; use tests\codeception\common\unit\TestCase;
use Yii; use Yii;
use const common\LATEST_RULES_VERSION; use const common\LATEST_RULES_VERSION;
/** class AccountTest extends TestCase {
* @property array $accounts
* @property array $mojangAccounts
*/
class AccountTest extends DbTestCase {
use Specify; use Specify;
public function fixtures() { public function _fixtures() {
return [ return [
'accounts' => [ 'accounts' => AccountFixture::class,
'class' => AccountFixture::class, 'mojangAccounts' => MojangUsernameFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
'mojangAccounts' => [
'class' => MojangUsernameFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/mojang-usernames.php',
],
]; ];
} }
@ -73,7 +63,8 @@ class AccountTest extends DbTestCase {
}); });
$this->specify('username should be unique', function() { $this->specify('username should be unique', function() {
$model = new Account(['username' => $this->accounts['admin']['username']]); $model = new Account();
$model->username = $this->tester->grabFixture('accounts', 'admin')['username'];
expect($model->validate('username'))->false(); expect($model->validate('username'))->false();
expect($model->getErrors('username'))->equals(['error.username_not_available']); expect($model->getErrors('username'))->equals(['error.username_not_available']);
}); });
@ -111,7 +102,7 @@ class AccountTest extends DbTestCase {
}); });
$this->specify('email should be unique', function() { $this->specify('email should be unique', function() {
$model = new Account(['email' => $this->accounts['admin']['email']]); $model = new Account(['email' => $this->tester->grabFixture('accounts', 'admin')['email']]);
expect($model->validate('email'))->false(); expect($model->validate('email'))->false();
expect($model->getErrors('email'))->equals(['error.email_not_available']); expect($model->getErrors('email'))->equals(['error.email_not_available']);
}); });

View File

@ -1,31 +1,35 @@
<?php <?php
namespace codeception\common\unit\models; namespace codeception\common\unit\models;
use Codeception\Specify; use common\models\confirmations;
use common\models\confirmations\ForgotPassword;
use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation; use common\models\EmailActivation;
use tests\codeception\common\fixtures\EmailActivationFixture; use tests\codeception\common\fixtures\EmailActivationFixture;
use tests\codeception\common\unit\DbTestCase; use tests\codeception\common\unit\TestCase;
class EmailActivationTest extends DbTestCase { class EmailActivationTest extends TestCase {
use Specify;
public function fixtures() { public function _fixtures() {
return [ return [
'emailActivations' => EmailActivationFixture::class, 'emailActivations' => EmailActivationFixture::class,
]; ];
} }
public function testInstantiate() { public function testInstantiate() {
$this->specify('return valid model type', function() { $this->assertInstanceOf(confirmations\RegistrationConfirmation::class, EmailActivation::findOne([
expect(EmailActivation::findOne([
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION, 'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
]))->isInstanceOf(RegistrationConfirmation::class); ]));
expect(EmailActivation::findOne([
$this->assertInstanceOf(confirmations\ForgotPassword::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY, 'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
]))->isInstanceOf(ForgotPassword::class); ]));
});
$this->assertInstanceOf(confirmations\CurrentEmailConfirmation::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
]));
$this->assertInstanceOf(confirmations\NewEmailConfirmation::class, EmailActivation::findOne([
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
]));
} }
} }

View File

@ -11,25 +11,18 @@ class LanguageValidatorTest extends TestCase {
use ProtectedCaller; use ProtectedCaller;
public function testGetFilesNames() { public function testGetFilesNames() {
$this->specify('get list of 2 languages: ru and en', function() {
$model = $this->createModelWithFixturePath(); $model = $this->createModelWithFixturePath();
expect($this->callProtected($model, 'getFilesNames'))->equals(['en', 'ru']); $this->assertEquals(['en', 'ru'], $this->callProtected($model, 'getFilesNames'));
});
} }
public function testValidateValue() { public function testValidateValueSupportedLanguage() {
$this->specify('get null, because language is supported', function() {
$model = $this->createModelWithFixturePath(); $model = $this->createModelWithFixturePath();
expect($this->callProtected($model, 'validateValue', 'ru'))->null(); $this->assertNull($this->callProtected($model, 'validateValue', 'ru'));
}); }
$this->specify('get error message, because language is unsupported', function() { public function testValidateNotSupportedLanguage() {
$model = $this->createModelWithFixturePath(); $model = $this->createModelWithFixturePath();
expect($this->callProtected($model, 'validateValue', 'by'))->equals([ $this->assertEquals([$model->message, []], $this->callProtected($model, 'validateValue', 'by'));
$model->message,
[],
]);
});
} }
/** /**