ContactFormTest.php 1.56 KB
Newer Older
Mark committed
1 2 3 4
<?php

namespace tests\unit\models;

5
use Yii;
6
use yii\codeception\TestCase;
Mark committed
7

8 9
class ContactFormTest extends TestCase
{
10 11 12 13 14 15

	use \Codeception\Specify;

	protected function setUp()
	{
		parent::setUp();
Mark committed
16
		Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
17 18 19 20 21 22
			return 'testing_message.eml';
		};
	}

	protected function tearDown()
	{
23
		unlink($this->getMessageFile());
24 25 26 27 28
		parent::tearDown();
	}

	public function testContact()
	{
29 30
		$model = $this->getMock('app\models\ContactForm', ['validate']);
		$model->expects($this->once())->method('validate')->will($this->returnValue(true));
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

		$model->attributes = [
			'name' => 'Tester',
			'email' => 'tester@example.com',
			'subject' => 'very important letter subject',
			'body' => 'body of current message',
		];

		$model->contact('admin@example.com');

		$this->specify('email should be send', function () {
			$this->assertFileExists($this->getMessageFile(), 'email file should exist');
		});

		$this->specify('message should contain correct data', function () use($model) {
			$emailMessage = file_get_contents($this->getMessageFile());
			$this->assertContains($model->name, $emailMessage, 'email should contain user name');
			$this->assertContains($model->email, $emailMessage, 'email should contain sender email');
			$this->assertContains($model->subject, $emailMessage, 'email should contain subject');
			$this->assertContains($model->body, $emailMessage, 'email should contain body');
		});
	}

	private function getMessageFile()
	{
		return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
	}

Mark committed
59
}