ContactFormTest.php 1.2 KB

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