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

64 lines
2.0 KiB
PHP
Raw Normal View History

2013-12-02 19:25:21 +08:00
<?php
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;
use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
2013-12-02 19:25:21 +08:00
class ContactFormTest extends TestCase
{
use Specify;
2014-03-16 12:46:16 +08:00
protected function setUp()
{
parent::setUp();
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()
{
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
}