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

3
namespace codeception\common\unit\models;
Mark committed
4 5

use Yii;
6
use codeception\common\unit\DbTestCase;
7 8
use Codeception\Specify;
use common\models\LoginForm;
9
use codeception\common\fixtures\UserFixture;
Mark committed
10

11 12 13
/**
 * Login form test
 */
14
class LoginFormTest extends DbTestCase
Mark committed
15
{
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    use Specify;

    public function setUp()
    {
        parent::setUp();

        Yii::configure(Yii::$app, [
            'components' => [
                'user' => [
                    'class' => 'yii\web\User',
                    'identityClass' => 'common\models\User',
                ],
            ],
        ]);
    }
32 33 34 35 36 37 38 39 40

    protected function tearDown()
    {
        Yii::$app->user->logout();
        parent::tearDown();
    }

    public function testLoginNoUser()
    {
41 42 43 44
        $model = new LoginForm([
            'username' => 'not_existing_username',
            'password' => 'not_existing_password',
        ]);
45 46 47 48 49 50 51 52 53

        $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()
    {
54 55 56 57
        $model = new LoginForm([
            'username' => 'bayer.hudson',
            'password' => 'wrong_password',
        ]);
58 59 60 61 62 63 64 65 66 67 68

        $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()
    {

69 70 71 72
        $model = new LoginForm([
            'username' => 'bayer.hudson',
            'password' => 'password_0',
        ]);
73 74 75 76 77 78 79 80

        $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();
        });
    }

81 82 83
    /**
     * @inheritdoc
     */
84
    public function fixtures()
85
    {
86 87 88
        return [
            'user' => [
                'class' => UserFixture::className(),
89
                'dataFile' => '@codeception/common/unit/fixtures/data/models/user.php'
90 91
            ],
        ];
92
    }
93

Mark committed
94
}