1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tests\codeception\frontend\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use tests\codeception\frontend\unit\TestCase; |
7
|
|
|
use frontend\models\ContactForm; |
8
|
|
|
|
9
|
|
|
class ContactFormTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
use \Codeception\Specify; |
13
|
|
|
|
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) { |
|
|
|
|
18
|
|
|
return 'testing_message.eml'; |
19
|
|
|
}; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tearDown() |
23
|
|
|
{ |
24
|
|
|
unlink($this->getMessageFile()); |
25
|
|
|
parent::tearDown(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testContact() |
29
|
|
|
{ |
30
|
|
|
$model = new ContactForm(); |
31
|
|
|
|
32
|
|
|
$model->attributes = [ |
33
|
|
|
'name' => 'Tester', |
34
|
|
|
'email' => '[email protected]', |
35
|
|
|
'subject' => 'very important letter subject', |
36
|
|
|
'body' => 'body of current message', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$model->sendEmail('[email protected]'); |
40
|
|
|
|
41
|
|
|
$this->specify('email should be send', function () { |
42
|
|
|
expect('email file should exist', file_exists($this->getMessageFile()))->true(); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
$this->specify('message should contain correct data', function () use ($model) { |
46
|
|
|
$emailMessage = file_get_contents($this->getMessageFile()); |
47
|
|
|
|
48
|
|
|
expect('email should contain user name', $emailMessage)->contains($model->name); |
49
|
|
|
expect('email should contain sender email', $emailMessage)->contains($model->email); |
50
|
|
|
expect('email should contain subject', $emailMessage)->contains($model->subject); |
51
|
|
|
expect('email should contain body', $emailMessage)->contains($model->body); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getMessageFile() |
56
|
|
|
{ |
57
|
|
|
return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml'; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.