ContactFormTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace tests\unit\models;
  3. use Yii;
  4. use yii\codeception\TestCase;
  5. use app\models\ContactForm;
  6. use AspectMock\Test as test;
  7. class ContactFormTest extends TestCase
  8. {
  9. use \Codeception\Specify;
  10. protected function setUp()
  11. {
  12. parent::setUp();
  13. Yii::$app->mail->fileTransportCallback = function ($mailer, $message)
  14. {
  15. return 'testing_message.eml';
  16. };
  17. }
  18. protected function tearDown()
  19. {
  20. unlink(Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml');
  21. test::clean();
  22. parent::tearDown();
  23. }
  24. public function testContact()
  25. {
  26. test::double('app\models\ContactForm',['validate' => true]);
  27. $model = new ContactForm();
  28. $model->attributes = [
  29. 'name' => 'Tester',
  30. 'email' => 'tester@example.com',
  31. 'subject' => 'very important letter subject',
  32. 'body' => 'body of current message',
  33. ];
  34. $model->contact('admin@example.com');
  35. $this->specify('email should be send', function () {
  36. $this->assertFileExists($this->getMessageFile(), 'email file should exist');
  37. });
  38. $this->specify('message should contain correct data', function () use($model) {
  39. $emailMessage = file_get_contents($this->getMessageFile());
  40. $this->assertContains($model->name, $emailMessage, 'email should contain user name');
  41. $this->assertContains($model->email, $emailMessage, 'email should contain sender email');
  42. $this->assertContains($model->subject, $emailMessage, 'email should contain subject');
  43. $this->assertContains($model->body, $emailMessage, 'email should contain body');
  44. });
  45. }
  46. private function getMessageFile()
  47. {
  48. return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
  49. }
  50. }