Реализована форма смены ника пользователя

Добавлена базовая форма с запросом пароля
Валидация ника и email адреса вынесены из формы регистрации в модель аккаунта
Отрефакторен тест формы регистрации
Добавлены тесты для модели аккаунта
This commit is contained in:
ErickSkrauch
2016-03-20 02:25:26 +03:00
parent 8b1c3a477a
commit e67257b8aa
12 changed files with 431 additions and 55 deletions

View File

@@ -9,7 +9,7 @@ class BaseApiFormTest extends TestCase {
use Specify;
public function testLoad() {
$model = new DummyTestModel();
$model = new DummyBaseApiForm();
$this->specify('model should load data without ModelName array scope', function() use ($model) {
expect('model successful load data without prefix', $model->load(['field' => 'test-data']))->true();
expect('field is set as passed data', $model->field)->equals('test-data');
@@ -18,7 +18,7 @@ class BaseApiFormTest extends TestCase {
}
class DummyTestModel extends BaseApiForm {
class DummyBaseApiForm extends BaseApiForm {
public $field;

View File

@@ -0,0 +1,38 @@
<?php
namespace tests\codeception\api\models;
use api\models\BasePasswordProtectedForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\TestCase;
class BasePasswordProtectedFormTest extends TestCase {
use Specify;
public function testValidatePassword() {
$this->specify('error.password_invalid on passing invalid account password', function() {
$model = new DummyBasePasswordProtectedForm();
$model->password = 'some-invalid-password';
$model->validatePassword();
expect($model->getErrors('password'))->equals(['error.password_invalid']);
});
$this->specify('no errors on passing valid account password', function() {
$model = new DummyBasePasswordProtectedForm();
$model->password = 'password_0';
$model->validatePassword();
expect($model->getErrors('password'))->isEmpty();
});
}
}
class DummyBasePasswordProtectedForm extends BasePasswordProtectedForm {
protected function getAccount() {
return new Account([
'password' => 'password_0',
]);
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace tests\codeception\api\models;
use api\models\ChangeUsernameForm;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
/**
* @property array $accounts
*/
class ChangeUsernameFormTest extends DbTestCase {
use Specify;
public function fixtures() {
return [
'accounts' => [
'class' => AccountFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
public function testChange() {
$this->specify('successfully change username to new one', function() {
$model = new DummyChangeUsernameForm([
'password' => 'password_0',
'username' => 'my_new_nickname',
]);
expect($model->change())->true();
expect(Account::findOne(1)->username)->equals('my_new_nickname');
});
}
}
// TODO: тут образуется магическая переменная 1, что не круто. После перехода на php7 можно заюзать анонимный класс
// и создавать модель прямо внутри теста, где доступен объект фикстур с именами переменных
class DummyChangeUsernameForm extends ChangeUsernameForm {
protected function getAccount() {
return Account::findOne(1);
}
}

View File

@@ -41,46 +41,27 @@ class RegistrationFormTest extends DbTestCase {
];
}
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 testValidatePasswordAndRePasswordMatch() {
$this->specify('error.rePassword_does_not_match if password and rePassword not match', function() {
$model = new RegistrationForm([
'password' => 'enough-length',
'rePassword' => 'password',
]);
expect($model->validate(['rePassword']))->false();
expect($model->getErrors('rePassword'))->equals(['error.rePassword_does_not_match']);
});
$this->specify('no errors if password and rePassword match', function() {
$model = new RegistrationForm([
'password' => 'enough-length',
'rePassword' => 'enough-length',
]);
expect($model->validate(['rePassword']))->true();
expect($model->getErrors('rePassword'))->isEmpty();
});
}
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() {
public function testSignup() {
$model = new RegistrationForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
@@ -105,6 +86,8 @@ class RegistrationFormTest extends DbTestCase {
expect_file('message file exists', $this->getMessageFile())->exists();
}
// TODO: там в самой форме есть метод sendMail(), который рано или поздно должен переехать. К нему нужны будут тоже тесты
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;