2013-12-02 19:25:21 +08:00
|
|
|
<?php
|
|
|
|
|
2014-08-24 18:14:35 +08:00
|
|
|
namespace tests\codeception\unit\models;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2016-07-03 01:13:40 +08:00
|
|
|
use app\models\ContactForm;
|
2013-12-31 04:17:36 +08:00
|
|
|
use Yii;
|
2013-12-17 07:27:33 +08:00
|
|
|
use yii\codeception\TestCase;
|
2014-06-09 20:10:16 +08:00
|
|
|
use Codeception\Specify;
|
2013-12-02 19:25:21 +08:00
|
|
|
|
2013-12-17 07:27:33 +08:00
|
|
|
class ContactFormTest extends TestCase
|
|
|
|
{
|
2014-06-09 20:10:16 +08:00
|
|
|
use Specify;
|
2014-03-16 12:46:16 +08:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2014-06-26 22:28:28 +08:00
|
|
|
Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
|
2014-03-16 12:46:16 +08:00
|
|
|
return 'testing_message.eml';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
unlink($this->getMessageFile());
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testContact()
|
|
|
|
{
|
2016-07-03 01:13:40 +08:00
|
|
|
/** @var ContactForm $model */
|
|
|
|
$model = $this->getMockBuilder('app\models\ContactForm')
|
|
|
|
->setMethods(['validate'])
|
|
|
|
->getMock();
|
2014-03-16 12:46:16 +08:00
|
|
|
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$model->attributes = [
|
|
|
|
'name' => 'Tester',
|
|
|
|
'email' => 'tester@example.com',
|
|
|
|
'subject' => 'very important letter subject',
|
|
|
|
'body' => 'body of current message',
|
|
|
|
];
|
|
|
|
|
2016-07-03 01:13:40 +08:00
|
|
|
$this->specify('email should be send', function () use ($model) {
|
|
|
|
expect('ContactForm::contact() should return true', $model->contact('admin@example.com'))->true();
|
2014-03-16 12:46:16 +08:00
|
|
|
expect('email file should exist', file_exists($this->getMessageFile()))->true();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('message should contain correct data', function () use ($model) {
|
|
|
|
$emailMessage = file_get_contents($this->getMessageFile());
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMessageFile()
|
|
|
|
{
|
2014-06-26 22:28:28 +08:00
|
|
|
return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
|
2014-03-16 12:46:16 +08:00
|
|
|
}
|
2014-06-09 20:10:16 +08:00
|
|
|
|
2013-12-02 19:25:21 +08:00
|
|
|
}
|