LoginFormTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace tests\codeception\common\unit\models;
  3. use Codeception\Specify;
  4. use common\models\LoginForm;
  5. use tests\codeception\common\fixtures\UserFixture;
  6. use tests\codeception\common\unit\DbTestCase;
  7. use Yii;
  8. /**
  9. * Login form test.
  10. */
  11. class LoginFormTest extends DbTestCase
  12. {
  13. use Specify;
  14. public function setUp()
  15. {
  16. parent::setUp();
  17. Yii::configure(Yii::$app, [
  18. 'components' => [
  19. 'user' => [
  20. 'class' => 'yii\web\User',
  21. 'identityClass' => 'common\models\User',
  22. ],
  23. ],
  24. ]);
  25. }
  26. protected function tearDown()
  27. {
  28. Yii::$app->user->logout();
  29. parent::tearDown();
  30. }
  31. public function testLoginNoUser()
  32. {
  33. $model = new LoginForm([
  34. 'username' => 'not_existing_username',
  35. 'password' => 'not_existing_password',
  36. ]);
  37. $this->specify('user should not be able to login, when there is no identity', function () use ($model) {
  38. expect('model should not login user', $model->login())->false();
  39. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  40. });
  41. }
  42. public function testLoginWrongPassword()
  43. {
  44. $model = new LoginForm([
  45. 'username' => 'bayer.hudson',
  46. 'password' => 'wrong_password',
  47. ]);
  48. $this->specify('user should not be able to login with wrong password', function () use ($model) {
  49. expect('model should not login user', $model->login())->false();
  50. expect('error message should be set', $model->errors)->hasKey('password');
  51. expect('user should not be logged in', Yii::$app->user->isGuest)->true();
  52. });
  53. }
  54. public function testLoginCorrect()
  55. {
  56. $model = new LoginForm([
  57. 'username' => 'bayer.hudson',
  58. 'password' => 'password_0',
  59. ]);
  60. $this->specify('user should be able to login with correct credentials', function () use ($model) {
  61. expect('model should login user', $model->login())->true();
  62. expect('error message should not be set', $model->errors)->hasntKey('password');
  63. expect('user should be logged in', Yii::$app->user->isGuest)->false();
  64. });
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function fixtures()
  70. {
  71. return [
  72. 'user' => [
  73. 'class' => UserFixture::className(),
  74. 'dataFile' => '@tests/codeception/common/unit/fixtures/data/models/user.php',
  75. ],
  76. ];
  77. }
  78. }