PasswordResetRequestFormTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 5
dl 0
loc 77
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 6 1
A testSendEmailWrongUser() 0 20 1
A testSendEmailCorrectUser() 0 19 1
A fixtures() 0 9 1
A getMessageFile() 0 4 1
1
<?php
2
3
namespace tests\codeception\frontend\models;
4
5
use Yii;
6
use tests\codeception\frontend\unit\DbTestCase;
7
use frontend\models\PasswordResetRequestForm;
8
use tests\codeception\common\fixtures\UserFixture;
9
use common\models\User;
10
use Codeception\Specify;
11
12
class PasswordResetRequestFormTest extends DbTestCase
13
{
14
    use Specify;
15
16
    protected function setUp()
17
    {
18
        parent::setUp();
19
20
        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...
21
            return 'testing_message.eml';
22
        };
23
    }
24
25
    protected function tearDown()
26
    {
27
        @unlink($this->getMessageFile());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
28
29
        parent::tearDown();
30
    }
31
32
    public function testSendEmailWrongUser()
33
    {
34
        $this->specify('no user with such email, message should not be sent', function () {
35
36
            $model = new PasswordResetRequestForm();
37
            $model->email = '[email protected]';
38
39
            expect('email not sent', $model->sendEmail())->false();
40
41
        });
42
43
        $this->specify('user is not active, message should not be sent', function () {
44
45
            $model = new PasswordResetRequestForm();
46
            $model->email = $this->user[1]['email'];
47
48
            expect('email not sent', $model->sendEmail())->false();
49
50
        });
51
    }
52
53
    public function testSendEmailCorrectUser()
54
    {
55
        $model = new PasswordResetRequestForm();
56
        $model->email = $this->user[0]['email'];
57
        $user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
58
59
        expect('email sent', $model->sendEmail())->true();
60
        expect('user has valid token', $user->password_reset_token)->notNull();
61
62
        $this->specify('message has correct format', function () use ($model) {
63
64
            expect('message file exists', file_exists($this->getMessageFile()))->true();
65
66
            $message = file_get_contents($this->getMessageFile());
67
            expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
68
            expect('message "to" is correct', $message)->contains($model->email);
69
70
        });
71
    }
72
73
    public function fixtures()
74
    {
75
        return [
76
            'user' => [
77
                'class' => UserFixture::className(),
78
                'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php'
79
            ],
80
        ];
81
    }
82
83
    private function getMessageFile()
84
    {
85
        return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
86
    }
87
88
}
89