LoginFormTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace tests\codeception\unit\models;
  3. use Yii;
  4. use yii\codeception\TestCase;
  5. use app\models\forms\LoginForm;
  6. use Codeception\Specify;
  7. class LoginFormTest extends TestCase
  8. {
  9. use Specify;
  10. protected function tearDown()
  11. {
  12. Yii::$app->user->logout();
  13. parent::tearDown();
  14. }
  15. public function testLoginNoUser()
  16. {
  17. $model = new LoginForm([
  18. 'username' => 'not_existing_username',
  19. 'password' => 'not_existing_password',
  20. ]);
  21. $this->specify('user should not be able to login, when there is no identity', function () use ($model) {
  22. expect('model should not login user', $model->login())->false();
  23. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  24. });
  25. }
  26. // public function testLoginWrongPassword()
  27. // {
  28. // $model = new LoginForm([
  29. // 'username' => 'demo',
  30. // 'password' => 'wrong_password',
  31. // ]);
  32. //
  33. // $this->specify('user should not be able to login with wrong password', function () use ($model) {
  34. // expect('model should not login user', $model->login())->false();
  35. // expect('error message should be set', $model->errors)->hasKey('password');
  36. // expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  37. // });
  38. // }
  39. public function testLoginCorrect()
  40. {
  41. $model = new LoginForm([
  42. 'username' => 'demo',
  43. 'password' => 'demo',
  44. ]);
  45. $this->specify('user should be able to login with correct credentials', function () use ($model) {
  46. expect('model should login user', $model->login())->true();
  47. expect('error message should not be set', $model->errors)->hasntKey('password');
  48. expect('user should be logged in', Yii::$app->user->isGuest)->false();
  49. });
  50. }
  51. }