LoginFormTest.php 1.83 KB
Newer Older
Mark committed
1 2
<?php

3
namespace tests\codeception\unit\models;
Mark committed
4

5
use Yii;
6
use yii\codeception\TestCase;
7 8
use app\models\LoginForm;
use Codeception\Specify;
Mark committed
9

10 11
class LoginFormTest extends TestCase
{
12
    use Specify;
13

14 15 16 17 18
    protected function tearDown()
    {
        Yii::$app->user->logout();
        parent::tearDown();
    }
Mark committed
19

20 21
    public function testLoginNoUser()
    {
22 23 24 25
        $model = new LoginForm([
            'username' => 'not_existing_username',
            'password' => 'not_existing_password',
        ]);
26

27 28 29 30 31
        $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();
        });
    }
32

33 34
    public function testLoginWrongPassword()
    {
35 36 37 38
        $model = new LoginForm([
            'username' => 'demo',
            'password' => 'wrong_password',
        ]);
39

40 41 42 43 44 45
        $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();
        });
    }
46

47 48
    public function testLoginCorrect()
    {
49 50 51 52
        $model = new LoginForm([
            'username' => 'demo',
            'password' => 'demo',
        ]);
53

54 55 56 57 58 59
        $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();
        });
    }
60

Luciano Baraglia committed
61
}