| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace tests\codeception\common\unit\models;
- use Codeception\Specify;
- use common\models\LoginForm;
- use tests\codeception\common\fixtures\UserFixture;
- use tests\codeception\common\unit\DbTestCase;
- use Yii;
- /**
- * Login form test.
- */
- class LoginFormTest extends DbTestCase
- {
- use Specify;
- public function setUp()
- {
- parent::setUp();
- Yii::configure(Yii::$app, [
- 'components' => [
- 'user' => [
- 'class' => 'yii\web\User',
- 'identityClass' => 'common\models\User',
- ],
- ],
- ]);
- }
- protected function tearDown()
- {
- Yii::$app->user->logout();
- parent::tearDown();
- }
- public function testLoginNoUser()
- {
- $model = new LoginForm([
- 'username' => 'not_existing_username',
- '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 LoginForm([
- 'username' => 'bayer.hudson',
- '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 LoginForm([
- 'username' => 'bayer.hudson',
- '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();
- });
- }
- /**
- * {@inheritdoc}
- */
- public function fixtures()
- {
- return [
- 'user' => [
- 'class' => UserFixture::className(),
- 'dataFile' => '@tests/codeception/common/unit/fixtures/data/models/user.php',
- ],
- ];
- }
- }
|