SignupFormTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace frontend\tests\unit\models;
  3. use common\fixtures\UserFixture;
  4. use frontend\models\SignupForm;
  5. class SignupFormTest extends \Codeception\Test\Unit
  6. {
  7. /**
  8. * @var \frontend\tests\UnitTester
  9. */
  10. protected $tester;
  11. public function _before()
  12. {
  13. $this->tester->haveFixtures([
  14. 'user' => [
  15. 'class' => UserFixture::className(),
  16. 'dataFile' => codecept_data_dir() . 'user.php'
  17. ]
  18. ]);
  19. }
  20. public function testCorrectSignup()
  21. {
  22. $model = new SignupForm([
  23. 'username' => 'some_username',
  24. 'email' => 'some_email@example.com',
  25. 'password' => 'some_password',
  26. ]);
  27. $user = $model->signup();
  28. expect($user)->true();
  29. /** @var \common\models\User $user */
  30. $user = $this->tester->grabRecord('common\models\User', [
  31. 'username' => 'some_username',
  32. 'email' => 'some_email@example.com',
  33. 'status' => \common\models\User::STATUS_INACTIVE
  34. ]);
  35. $this->tester->seeEmailIsSent();
  36. $mail = $this->tester->grabLastSentEmail();
  37. expect($mail)->isInstanceOf('yii\mail\MessageInterface');
  38. expect($mail->getTo())->hasKey('some_email@example.com');
  39. expect($mail->getFrom())->hasKey(\Yii::$app->params['supportEmail']);
  40. expect($mail->getSubject())->equals('Account registration at ' . \Yii::$app->name);
  41. expect($mail->toString())->stringContainsString($user->verification_token);
  42. }
  43. public function testNotCorrectSignup()
  44. {
  45. $model = new SignupForm([
  46. 'username' => 'troy.becker',
  47. 'email' => 'nicolas.dianna@hotmail.com',
  48. 'password' => 'some_password',
  49. ]);
  50. expect_not($model->signup());
  51. expect_that($model->getErrors('username'));
  52. expect_that($model->getErrors('email'));
  53. expect($model->getFirstError('username'))
  54. ->equals('This username has already been taken.');
  55. expect($model->getFirstError('email'))
  56. ->equals('This email address has already been taken.');
  57. }
  58. }