ContactFormTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 5 1
B testContact() 0 26 1
A getMessageFile() 0 4 1
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $mailer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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