TestCase::createController()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace yii2mod\user\tests;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii2mod\user\tests\data\Controller;
8
9
/**
10
 * This is the base class for all yii framework unit tests.
11
 */
12
class TestCase extends \PHPUnit\Framework\TestCase
13
{
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
        $this->mockApplication();
18
        $this->setupTestDbData();
19
        Yii::$app->mailer->fileTransportCallback = function () {
20
            return 'testing_message.eml';
21
        };
22
    }
23
24
    protected function tearDown()
25
    {
26
        $this->destroyApplication();
27
    }
28
29
    /**
30
     * Populates Yii::$app with a new application
31
     * The application will be destroyed on tearDown() automatically.
32
     *
33
     * @param array $config The application configuration, if needed
34
     * @param string $appClass name of the application class to create
35
     */
36
    protected function mockApplication($config = [], $appClass = '\yii\web\Application')
37
    {
38
        new $appClass(ArrayHelper::merge([
39
            'id' => 'testapp',
40
            'basePath' => __DIR__,
41
            'vendorPath' => $this->getVendorPath(),
42
            'components' => [
43
                'db' => [
44
                    'class' => 'yii\db\Connection',
45
                    'dsn' => 'sqlite::memory:',
46
                ],
47
                'user' => [
48
                    'identityClass' => 'yii2mod\user\models\UserModel',
49
                ],
50
                'request' => [
51
                    'hostInfo' => 'http://domain.com',
52
                    'scriptUrl' => 'index.php',
53
                ],
54
                'mailer' => [
55
                    'class' => 'yii\swiftmailer\Mailer',
56
                    'useFileTransport' => true,
57
                    'htmlLayout' => false,
58
                    'viewPath' => __DIR__ . '/data/mail',
59
                ],
60
                'i18n' => [
61
                    'translations' => [
62
                        'yii2mod.user' => [
63
                            'class' => 'yii\i18n\PhpMessageSource',
64
                            'basePath' => '@yii2mod/user/messages',
65
                        ],
66
                    ],
67
                ],
68
            ],
69
            'params' => [
70
                'adminEmail' => '[email protected]',
71
                'user.passwordResetTokenExpire' => 3600,
72
            ],
73
        ], $config));
74
    }
75
76
    /**
77
     * @return string vendor path
78
     */
79
    protected function getVendorPath()
80
    {
81
        return dirname(__DIR__) . '/vendor';
82
    }
83
84
    /**
85
     * Destroys application in Yii::$app by setting it to null.
86
     */
87
    protected function destroyApplication()
88
    {
89
        Yii::$app = null;
90
    }
91
92
    /**
93
     * @param array $config controller config
94
     *
95
     * @return Controller controller instance
96
     */
97
    protected function createController($config = [])
98
    {
99
        return new Controller('test', Yii::$app, $config);
100
    }
101
102
    /**
103
     * Setup tables for test ActiveRecord
104
     */
105
    protected function setupTestDbData()
106
    {
107
        $db = Yii::$app->getDb();
108
109
        // Structure :
110
111
        $db->createCommand()->createTable('user', [
112
            'id' => 'pk',
113
            'username' => 'string not null unique',
114
            'auth_key' => 'string(32) not null',
115
            'password_hash' => 'string not null',
116
            'password_reset_token' => 'string unique',
117
            'email' => 'string not null unique',
118
            'status' => 'integer not null default 1',
119
            'created_at' => 'integer not null',
120
            'updated_at' => 'integer not null',
121
            'last_login' => 'integer',
122
        ])->execute();
123
124
        // Data :
125
126
        $db->createCommand()->insert('user', [
127
            'username' => 'demo',
128
            'auth_key' => Yii::$app->getSecurity()->generateRandomString(),
129
            'password_hash' => Yii::$app->getSecurity()->generatePasswordHash('password'),
130
            'email' => '[email protected]',
131
            'created_at' => time(),
132
            'updated_at' => time(),
133
        ])->execute();
134
    }
135
}
136