Отрефакторены тесты

Удалено тестовое окружение acceptance
Удалена часть потенциально ненужных тестов
Добавлена логика для формы регистрации
Добавлена таблица для хранения ключей активации по E-mail
Добавлены тесты для формы регистрации
Реорганизован роутинг
Добавлен компонент для ReCaptcha2
This commit is contained in:
ErickSkrauch
2016-01-15 12:21:27 +03:00
parent 45c31dfbbe
commit 44aaea2c08
56 changed files with 1075 additions and 972 deletions

View File

@ -0,0 +1,61 @@
<?php
namespace tests\codeception\api\models;
use api\models\LoginForm;
use Codeception\Specify;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
class LoginFormTest extends DbTestCase {
use Specify;
protected function tearDown() {
Yii::$app->user->logout();
parent::tearDown();
}
public function fixtures() {
return [
'account' => [
'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
protected function createModel($login = '', $password = '') {
return new LoginForm([
'login' => $login,
'password' => $password,
]);
}
public function testIncorrectLogin() {
$model = $this->createModel('not-esist-login', 'fully-invalid-password');
$this->specify('get errors and don\'t log in into account with wrong credentials', function () use ($model) {
expect('model should not login user', $model->login())->false();
expect('error messages should be set', $model->errors)->notEmpty();
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
});
}
public function testLoginByUsernameCorrect() {
$model = $this->createModel('Admin', 'password_0');
$this->specify('user should be able to login with correct username and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function testLoginByEmailCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct email and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
}

View File

@ -0,0 +1,111 @@
<?php
namespace tests\codeception\api\models;
use api\models\RegistrationForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
class RegistrationFormTest extends DbTestCase {
use Specify;
public function setUp() {
parent::setUp();
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
$mailer->fileTransportCallback = function () {
return 'testing_message.eml';
};
}
protected function tearDown() {
if (file_exists($this->getMessageFile())) {
unlink($this->getMessageFile());
}
parent::tearDown();
}
public function fixtures() {
return [
'account' => [
'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
public function testNotCorrectRegistration() {
$model = new RegistrationForm([
'username' => 'valid_nickname',
'email' => 'correct-email@ely.by',
'password' => 'enough-length',
'rePassword' => 'password',
'rulesAgreement' => true,
]);
$this->specify('username and email in use, passwords not math - model is not created', function() use ($model) {
expect($model->signup())->null();
expect($model->getErrors())->notEmpty();
expect_file($this->getMessageFile())->notExists();
});
}
public function testUsernameValidators() {
$shouldBeValid = [
'русский_ник', 'русский_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!?#$%^&*()[]', '[ESP]Эрик',
'Свят_помидор;', роблена_ў_беларусі:)',
];
$shouldBeInvalid = [
'nick@name', 'spaced nick', ' ', 'sh', ' sh ',
];
foreach($shouldBeValid as $nickname) {
$model = new RegistrationForm([
'username' => $nickname,
]);
expect($nickname . ' passed validation', $model->validate(['username']))->true();
}
foreach($shouldBeInvalid as $nickname) {
$model = new RegistrationForm([
'username' => $nickname,
]);
expect($nickname . ' fail validation', $model->validate('username'))->false();
}
}
public function testCorrectSignup() {
$model = new RegistrationForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
'rePassword' => 'some_password',
'rulesAgreement' => true,
]);
$user = $model->signup();
expect('user should be valid', $user)->isInstanceOf(Account::class);
expect('password should be correct', $user->validatePassword('some_password'))->true();
expect('user model exists in database', Account::find()->andWhere([
'username' => 'some_username',
'email' => 'some_email@example.com',
])->exists())->true();
expect('email activation code exists in database', EmailActivation::find()->andWhere([
'account_id' => $user->id,
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
])->exists())->true();
expect_file('message file exists', $this->getMessageFile())->exists();
}
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace tests\codeception\api\unit\models;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\UserFixture;
use Codeception\Specify;
use api\models\SignupForm;
class SignupFormTest extends DbTestCase
{
use Specify;
public function testCorrectSignup()
{
$model = new SignupForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
]);
$user = $model->signup();
$this->assertInstanceOf('common\models\User', $user, 'user should be valid');
expect('email should be correct', $user->email)->equals('some_email@example.com');
expect('password should be correct', $user->validatePassword('some_password'))->true();
}
public function testNotCorrectSignup()
{
$model = new SignupForm([
'username' => 'troy.becker',
'email' => 'nicolas.dianna@hotmail.com',
'password' => 'some_password',
]);
expect('username and email are in use, user should not be created', $model->signup())->null();
}
public function fixtures()
{
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/user.php',
],
];
}
}