ContactFormTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. private $model;
  8. /**
  9. * @var \UnitTester
  10. */
  11. public $tester;
  12. public function testEmailIsSentOnContact()
  13. {
  14. /** @var ContactForm $model */
  15. $this->model = $this->getMockBuilder('app\models\ContactForm')
  16. ->setMethods(['validate'])
  17. ->getMock();
  18. $this->model->expects($this->once())
  19. ->method('validate')
  20. ->willReturn(true);
  21. $this->model->attributes = [
  22. 'name' => 'Tester',
  23. 'email' => 'tester@example.com',
  24. 'subject' => 'very important letter subject',
  25. 'body' => 'body of current message',
  26. ];
  27. expect_that($this->model->contact('admin@example.com'));
  28. // using Yii2 module actions to check email was sent
  29. $this->tester->seeEmailIsSent();
  30. /** @var MessageInterface $emailMessage */
  31. $emailMessage = $this->tester->grabLastSentEmail();
  32. expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
  33. expect($emailMessage->getTo())->hasKey('admin@example.com');
  34. expect($emailMessage->getFrom())->hasKey('noreply@example.com');
  35. expect($emailMessage->getReplyTo())->hasKey('tester@example.com');
  36. expect($emailMessage->getSubject())->equals('very important letter subject');
  37. expect($emailMessage->toString())->contains('body of current message');
  38. }
  39. }