LoginFormTest.php 1.8 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. $this->specify('user should not be able to login with wrong password', function () use ($model) {
  33. expect('model should not login user', $model->login())->false();
  34. expect('error message should be set', $model->errors)->hasKey('password');
  35. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  36. });
  37. }
  38. public function testLoginCorrect()
  39. {
  40. $model = new LoginForm([
  41. 'username' => 'admin',
  42. 'password' => 'admin',
  43. ]);
  44. $this->specify('user should be able to login with correct credentials', function () use ($model) {
  45. expect('model should login user', $model->login())->true();
  46. expect('error message should not be set', $model->errors)->hasntKey('password');
  47. expect('user should be logged in', Yii::$app->user->isGuest)->false();
  48. });
  49. }
  50. }