PasswordResetRequestFormTest.php 2.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace frontend\tests\unit\models;

use Yii;
use frontend\tests\unit\DbTestCase;
use frontend\models\PasswordResetRequestForm;
use common\tests\fixtures\UserFixture;
use common\models\User;

class PasswordResetRequestFormTest extends DbTestCase
{
13
    use \Codeception\Specify;
14

15 16 17 18 19 20 21
    protected function setUp()
    {
        parent::setUp();
        Yii::$app->mail->fileTransportCallback = function ($mailer, $message) {
            return 'testing_message.eml';
        };
    }
22

23 24 25 26 27
    protected function tearDown()
    {
        @unlink($this->getMessageFile());
        parent::tearDown();
    }
28

29 30 31 32 33
    public function testSendEmailWrongUser()
    {
        $this->specify('no user with such email, message should not be send', function () {
            $model = new PasswordResetRequestForm();
            $model->email = 'not-existing-email@example.com';
34

35 36
            expect('email not send', $model->sendEmail())->false();
        });
37

38 39 40
        $this->specify('user is not active, message should not be send', function () {
            $model = new PasswordResetRequestForm();
            $model->email = $this->user[1]['email'];
41

42 43 44
            expect('email not send', $model->sendEmail())->false();
        });
    }
45

46 47 48 49
    public function testSendEmailCorrectUser()
    {
        $model = new PasswordResetRequestForm();
        $model->email = $this->user[0]['email'];
Alexander Makarov committed
50
        $user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        expect('email sent', $model->sendEmail())->true();
        expect('user has valid token', $user->password_reset_token)->notNull();

        $this->specify('message has correct format', function () use ($model) {
            expect('message file exists', file_exists($this->getMessageFile()))->true();

            $message = file_get_contents($this->getMessageFile());
            expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
            expect('message "to" is correct', $message)->contains($model->email);
        });
    }

    public function fixtures()
    {
        return [
            'user' => [
                'class' => UserFixture::className(),
69
                'dataFile' => '@frontend/tests/unit/fixtures/data/user.php'
70 71 72 73 74 75 76 77
            ],
        ];
    }

    private function getMessageFile()
    {
        return Yii::getAlias(Yii::$app->mail->fileTransportPath) . '/testing_message.eml';
    }
78
}