Переработана структура тестов для формы аутентификации

This commit is contained in:
ErickSkrauch 2016-01-03 20:37:55 +03:00
parent 7b650e2654
commit c6a6f35be6
2 changed files with 88 additions and 107 deletions

View File

@ -17,23 +17,17 @@ class AuthenticationForm extends Model {
return [ return [
['email', 'required', 'message' => 'error.email_required'], ['email', 'required', 'message' => 'error.email_required'],
['email', 'email', 'message' => 'error.email_invalid'], ['email', 'email', 'message' => 'error.email_invalid'],
['email', 'validateEmail'], ['email', 'exist', 'targetClass' => Account::class, 'skipOnError' => true, 'message' => 'error.email_not_exist'],
['password', 'required', 'message' => 'error.password_required'], ['password', 'required', 'when' => function(self $model) {
return !$model->hasErrors();
}, 'message' => 'error.password_required'],
['password', 'validatePassword'], ['password', 'validatePassword'],
['rememberMe', 'boolean'], ['rememberMe', 'boolean'],
]; ];
} }
public function validateEmail($attribute) {
if (!$this->hasErrors()) {
if ($this->getAccount() === NULL) {
$this->addError($attribute, 'error.email_not_exist');
}
}
}
public function validatePassword($attribute) { public function validatePassword($attribute) {
if (!$this->hasErrors()) { if (!$this->hasErrors()) {
$account = $this->getAccount(); $account = $this->getAccount();

View File

@ -16,109 +16,96 @@ class AuthenticationFormTest extends DbTestCase {
parent::tearDown(); parent::tearDown();
} }
public function testValidateEmail() {
$model = new AuthenticationForm();
$this->specify('error.email_required expected if email is not set', function() use ($model) {
$model->validate(['email']);
expect($model->getErrors('email'))->equals(['error.email_required']);
});
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
$model->email = 'wrong-email-string';
$model->validate(['email']);
expect($model->getErrors('email'))->equals(['error.email_invalid']);
$model->email = 'wrong@email';
$model->validate(['email']);
expect($model->getErrors('email'))->equals(['error.email_invalid']);
});
$this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) {
$model->email = 'not-exist@user.com';
$model->validate(['email']);
expect($model->getErrors('email'))->equals(['error.email_not_exist']);
});
$this->specify('no errors if email is correct and exists in database', function() use ($model) {
$model->email = 'admin@ely.by';
$model->validate(['email']);
expect($model->getErrors('email'))->isEmpty();
});
}
public function testValidatePassword() {
$model = new AuthenticationForm();
$this->specify('error.password_required expected if password is not set', function() use ($model) {
$model->validate(['password']);
expect($model->getErrors('password'))->equals(['error.password_required']);
});
$this->specify('error.password_incorrect expected if password not correct for passed email', function() use ($model) {
$model->email = 'non-exist@valid.mail';
$model->password = 'wrong-password';
$model->validate(['password']);
expect('if email incorrect, the error should be displayed in any case,', $model->getErrors('password'))
->equals(['error.password_incorrect']);
$model->email = 'admin@ely.by';
$model->password = 'wrong-password';
$model->validate(['password']);
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
$this->specify('no errors if email and password is correct and exists in database', function() use ($model) {
$model->email = 'admin@ely.by';
$model->password = 'password_0';
$model->validate(['password']);
expect($model->getErrors('password'))->isEmpty();
});
}
public function testLoginNoUser() {
$model = new AuthenticationForm([
'email' => 'non-exist@valid.mail',
'password' => 'not_existing_password',
]);
$this->specify('user should not be able to login, when there is no identity', function () use ($model) {
expect('model should not login user', $model->login())->false();
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
});
}
public function testLoginWrongPassword() {
$model = new AuthenticationForm([
'email' => 'admin@ely.by',
'password' => 'wrong_password',
]);
$this->specify('user should not be able to login with wrong password', function () use ($model) {
expect('model should not login user', $model->login())->false();
expect('error message should be set', $model->errors)->hasKey('password');
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
});
}
public function testLoginCorrect() {
$model = new AuthenticationForm([
'email' => 'admin@ely.by',
'password' => 'password_0',
]);
$this->specify('user should be able to login with correct credentials', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->hasntKey('password');
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function fixtures() { public function fixtures() {
return [ return [
'user' => [ 'account' => [
'class' => AccountFixture::className(), 'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/accounts.php' 'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/accounts.php'
], ],
]; ];
} }
protected function createModel($email = '', $password = '') {
return new AuthenticationForm([
'email' => $email,
'password' => $password,
]);
}
public function testLoginEmail() {
$model = $this->createModel();
$this->specify('error.email_required expected if email is not set', function() use ($model) {
expect($model->login())->false();
expect($model->getErrors('email'))->equals(['error.email_required']);
expect(Yii::$app->user->isGuest)->true();
});
$model = $this->createModel('wrong-email-string');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']);
});
$model = $this->createModel('wrong@email');
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_invalid']);
});
$model = $this->createModel('not-exist@user.com');
$this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->equals(['error.email_not_exist']);
});
$model = $this->createModel('admin@ely.by');
$this->specify('no errors on email field if email is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('email'))->isEmpty();
});
}
public function testLoginPassword() {
$model = $this->createModel();
$this->specify('password don\'t has errors if email not set', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('wrong-email-string', 'random-password');
$this->specify('password don\'t has errors if email invalid', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('not-exist@user.com', 'random-password');
$this->specify('password don\'t has errors if email not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('admin@ely.by', 'wrong-password');
$this->specify('error.password_incorrect expected if email correct, but password wrong', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
}
public function testLoginCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct credentials', 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();
});
}
} }