ContactFormTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace tests\unit\models;
  3. use app\models\ContactForm;
  4. use yii\mail\MessageInterface;
  5. class ContactFormTest extends \Codeception\Test\Unit
  6. {
  7. /**
  8. * @var \UnitTester
  9. */
  10. public $tester;
  11. public function testEmailIsSentOnContact()
  12. {
  13. $model = new ContactForm();
  14. $model->attributes = [
  15. 'name' => 'Tester',
  16. 'email' => 'tester@example.com',
  17. 'subject' => 'very important letter subject',
  18. 'body' => 'body of current message',
  19. 'verifyCode' => 'testme',
  20. ];
  21. expect_that($model->contact('admin@example.com'));
  22. // using Yii2 module actions to check email was sent
  23. $this->tester->seeEmailIsSent();
  24. /** @var MessageInterface $emailMessage */
  25. $emailMessage = $this->tester->grabLastSentEmail();
  26. expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
  27. expect($emailMessage->getTo())->hasKey('admin@example.com');
  28. expect($emailMessage->getFrom())->hasKey('noreply@example.com');
  29. expect($emailMessage->getReplyTo())->hasKey('tester@example.com');
  30. expect($emailMessage->getSubject())->equals('very important letter subject');
  31. expect($emailMessage->toString())->stringContainsString('body of current message');
  32. }
  33. }