2013-12-02 19:25:21 +08:00
|
|
|
<?php
|
|
|
|
|
2014-08-15 07:30:47 +08:00
|
|
|
namespace codeception\unit\models;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2014-01-01 03:02:10 +08:00
|
|
|
use Yii;
|
2013-12-17 07:27:33 +08:00
|
|
|
use yii\codeception\TestCase;
|
2014-06-09 20:10:16 +08:00
|
|
|
use app\models\LoginForm;
|
|
|
|
use Codeception\Specify;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2013-12-17 07:27:33 +08:00
|
|
|
class LoginFormTest extends TestCase
|
|
|
|
{
|
2014-06-09 20:10:16 +08:00
|
|
|
use Specify;
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
Yii::$app->user->logout();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2014-02-14 22:05:56 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
public function testLoginNoUser()
|
|
|
|
{
|
2014-06-09 20:10:16 +08:00
|
|
|
$model = new LoginForm([
|
|
|
|
'username' => 'not_existing_username',
|
|
|
|
'password' => 'not_existing_password',
|
|
|
|
]);
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
$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();
|
|
|
|
});
|
|
|
|
}
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
public function testLoginWrongPassword()
|
|
|
|
{
|
2014-06-09 20:10:16 +08:00
|
|
|
$model = new LoginForm([
|
|
|
|
'username' => 'demo',
|
|
|
|
'password' => 'wrong_password',
|
|
|
|
]);
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
$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();
|
|
|
|
});
|
|
|
|
}
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
public function testLoginCorrect()
|
|
|
|
{
|
2014-06-09 20:10:16 +08:00
|
|
|
$model = new LoginForm([
|
|
|
|
'username' => 'demo',
|
|
|
|
'password' => 'demo',
|
|
|
|
]);
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-03-16 12:46:16 +08:00
|
|
|
$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();
|
|
|
|
});
|
|
|
|
}
|
2013-12-31 04:17:36 +08:00
|
|
|
|
2014-01-20 00:35:36 +08:00
|
|
|
}
|