Drupal investigation

ContactFormTest.php 1.4KB

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