1
0

LoginFormTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace common\tests\unit\models;
  3. use Yii;
  4. use common\models\LoginForm;
  5. use common\fixtures\UserFixture;
  6. /**
  7. * Login form test
  8. */
  9. class LoginFormTest extends \Codeception\Test\Unit
  10. {
  11. /**
  12. * @var \common\tests\UnitTester
  13. */
  14. protected $tester;
  15. /**
  16. * @return array
  17. */
  18. public function _fixtures()
  19. {
  20. return [
  21. 'user' => [
  22. 'class' => UserFixture::className(),
  23. 'dataFile' => codecept_data_dir() . 'user.php'
  24. ]
  25. ];
  26. }
  27. public function testLoginNoUser()
  28. {
  29. $model = new LoginForm([
  30. 'username' => 'not_existing_username',
  31. 'password' => 'not_existing_password',
  32. ]);
  33. expect('model should not login user', $model->login())->false();
  34. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  35. }
  36. public function testLoginWrongPassword()
  37. {
  38. $model = new LoginForm([
  39. 'username' => 'bayer.hudson',
  40. 'password' => 'wrong_password',
  41. ]);
  42. expect('model should not login user', $model->login())->false();
  43. expect('error message should be set', $model->errors)->hasKey('password');
  44. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  45. }
  46. public function testLoginCorrect()
  47. {
  48. $model = new LoginForm([
  49. 'username' => 'bayer.hudson',
  50. 'password' => 'password_0',
  51. ]);
  52. expect('model should login user', $model->login())->true();
  53. expect('error message should not be set', $model->errors)->hasntKey('password');
  54. expect('user should be logged in', Yii::$app->user->isGuest)->false();
  55. }
  56. }