yii2-netdisk/tests/unit/models/ContactFormTest.php

57 lines
1.5 KiB
PHP
Raw Normal View History

2013-12-02 19:25:21 +08:00
<?php
2016-07-16 09:13:51 +08:00
namespace tests\models;
2013-12-02 19:25:21 +08:00
2016-07-03 01:13:40 +08:00
use app\models\ContactForm;
2013-12-02 19:25:21 +08:00
2016-07-16 09:13:51 +08:00
class ContactFormTest extends \Codeception\Test\Unit
{
2016-07-16 09:13:51 +08:00
private $model;
2014-03-16 12:46:16 +08:00
2016-07-16 09:13:51 +08:00
protected function _before()
2014-03-16 12:46:16 +08:00
{
2016-07-16 09:13:51 +08:00
\Yii::$app->mailer->fileTransportCallback = function () {
2014-03-16 12:46:16 +08:00
return 'testing_message.eml';
};
}
2016-07-16 09:13:51 +08:00
protected function _after()
2014-03-16 12:46:16 +08:00
{
unlink($this->getMessageFile());
}
2016-07-16 09:13:51 +08:00
public function testEmailIsSentOnContact()
2014-03-16 12:46:16 +08:00
{
2016-07-03 01:13:40 +08:00
/** @var ContactForm $model */
2016-07-16 09:13:51 +08:00
$this->model = $this->getMockBuilder('app\models\ContactForm')
2016-07-03 01:13:40 +08:00
->setMethods(['validate'])
->getMock();
2014-03-16 12:46:16 +08:00
2016-07-16 09:13:51 +08:00
$this->model->expects($this->once())
->method('validate')
->will($this->returnValue(true));
$this->model->attributes = [
2014-03-16 12:46:16 +08:00
'name' => 'Tester',
'email' => 'tester@example.com',
'subject' => 'very important letter subject',
'body' => 'body of current message',
];
2016-07-16 09:13:51 +08:00
expect_that($this->model->contact('admin@example.com'));
expect_that(file_exists($this->getMessageFile()));
2014-03-16 12:46:16 +08:00
2016-07-16 09:13:51 +08:00
$emailMessage = file_get_contents($this->getMessageFile());
2014-03-16 12:46:16 +08:00
2016-07-16 09:13:51 +08:00
expect($emailMessage)->contains($this->model->name);
expect($emailMessage)->contains($this->model->email);
expect($emailMessage)->contains($this->model->subject);
expect($emailMessage)->contains($this->model->body);
2014-03-16 12:46:16 +08:00
}
private function getMessageFile()
{
2016-07-16 09:13:51 +08:00
return \Yii::getAlias(\Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
2014-03-16 12:46:16 +08:00
}
2013-12-02 19:25:21 +08:00
}