You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactFormTest.php 1.7KB

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