LoginFormTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace tests\unit\models;
  3. use yii\codeception\TestCase;
  4. use app\models\LoginForm;
  5. use app\models\User;
  6. use AspectMock\Test as test;
  7. class LoginFormTest extends TestCase
  8. {
  9. use \Codeception\Specify;
  10. protected function tearDown()
  11. {
  12. test::clean();
  13. parent::tearDown();
  14. }
  15. public function testLoginNoUser()
  16. {
  17. $user = $this->mockUser(null);
  18. $model = new LoginForm();
  19. $model->username = 'some_username';
  20. $model->password = 'some_password';
  21. $this->specify('user should not be able to login, when there is no identity' , function () use ($user,$model) {
  22. $this->assertFalse($model->login());
  23. $user->verifyInvoked('findByUsername',['some_username']);
  24. });
  25. }
  26. public function testLoginWrongPassword()
  27. {
  28. $this->mockUser(new User);
  29. $model = new LoginForm();
  30. $model->username = 'demo';
  31. $model->password = 'wrong-password';
  32. $this->specify('user should not be able to login with wrong password', function () use ($model){
  33. $this->assertFalse($model->login());
  34. $this->assertArrayHasKey('password',$model->errors);
  35. });
  36. }
  37. public function testLoginCorrect()
  38. {
  39. $this->mockUser(new User(['password' => 'demo']));
  40. $model = new LoginForm();
  41. $model->username = 'demo';
  42. $model->password = 'demo';
  43. $this->specify('user should not be able to login with correct credentials', function() use($model) {
  44. $this->assertTrue($model->login());
  45. $this->assertArrayNotHasKey('password',$model->errors);
  46. });
  47. }
  48. private function mockUser($user)
  49. {
  50. return test::double('app\models\User', [
  51. 'findByUsername' => $user,
  52. ]);
  53. }
  54. }