2013-12-02 19:25:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace tests\unit\models;
|
|
|
|
|
2013-12-31 04:17:36 +08:00
|
|
|
use Yii;
|
2013-12-17 07:27:33 +08:00
|
|
|
use yii\codeception\TestCase;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2013-12-17 07:27:33 +08:00
|
|
|
class ContactFormTest extends TestCase
|
|
|
|
{
|
2013-12-31 04:17:36 +08:00
|
|
|
|
|
|
|
use \Codeception\Specify;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2014-01-01 03:13:47 +08:00
|
|
|
Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
|
2013-12-31 04:17:36 +08:00
|
|
|
return 'testing_message.eml';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
2014-01-01 03:02:10 +08:00
|
|
|
unlink($this->getMessageFile());
|
2013-12-31 04:17:36 +08:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testContact()
|
|
|
|
{
|
2014-01-01 03:02:10 +08:00
|
|
|
$model = $this->getMock('app\models\ContactForm', ['validate']);
|
|
|
|
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
|
2013-12-31 04:17:36 +08:00
|
|
|
|
|
|
|
$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 () {
|
2014-01-05 07:12:40 +08:00
|
|
|
expect('email file should exist', file_exists($this->getMessageFile()))->true();
|
2013-12-31 04:17:36 +08:00
|
|
|
});
|
|
|
|
|
2014-02-25 00:17:38 +08:00
|
|
|
$this->specify('message should contain correct data', function () use ($model) {
|
2013-12-31 04:17:36 +08:00
|
|
|
$emailMessage = file_get_contents($this->getMessageFile());
|
2014-01-05 07:12:40 +08:00
|
|
|
|
|
|
|
expect('email should contain user name', $emailMessage)->contains($model->name);
|
|
|
|
expect('email should contain sender email', $emailMessage)->contains($model->email);
|
|
|
|
expect('email should contain subject', $emailMessage)->contains($model->subject);
|
|
|
|
expect('email should contain body', $emailMessage)->contains($model->body);
|
2013-12-31 04:17:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMessageFile()
|
|
|
|
{
|
|
|
|
return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
|
|
|
|
}
|
|
|
|
|
2013-12-02 19:25:21 +08:00
|
|
|
}
|