Completed
Branch master (b8f3f5)
by Alexey
14:37
created

TestCase::mockWebApplication()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 19
nc 1
nop 2
1
<?php
2
3
namespace tests;
4
5
use yii\helpers\ArrayHelper;
6
use yii\web\View;
7
8
/**
9
 * Class TestCase
10
 * @package tests
11
 */
12
abstract class TestCase extends \PHPUnit\Framework\TestCase
13
{
14
    public static $params;
15
16
    /**
17
     * Mock application prior running tests.
18
     */
19
    protected function setUp()
20
    {
21
        $this->mockWebApplication(
22
            [
23
                'components' => [
24
                    'request' => [
25
                        'class' => 'yii\web\Request',
26
                        'url' => '/test',
27
                        'enableCsrfValidation' => false,
28
                    ],
29
                    'response' => [
30
                        'class' => 'yii\web\Response',
31
                    ],
32
                ],
33
            ]
34
        );
35
    }
36
37
    /**
38
     * Clean up after test.
39
     * By default the application created with [[mockApplication]] will be destroyed.
40
     */
41
    protected function tearDown()
42
    {
43
        parent::tearDown();
44
        $this->destroyApplication();
45
    }
46
47
    protected function mockApplication($config = [], $appClass = '\yii\console\Application')
48
    {
49
        new $appClass(
50
            ArrayHelper::merge(
51
                [
52
                    'id' => 'testapp',
53
                    'basePath' => __DIR__,
54
                    'vendorPath' => $this->getVendorPath(),
55
                ],
56
                $config
57
            )
58
        );
59
    }
60
61
    protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
62
    {
63
        new $appClass(
64
            ArrayHelper::merge(
65
                [
66
                    'id' => 'testapp',
67
                    'basePath' => __DIR__,
68
                    'vendorPath' => $this->getVendorPath(),
69
                    'aliases' => [
70
                        '@bower' => '@vendor/bower-asset',
71
                        '@npm' => '@vendor/npm-asset',
72
                    ],
73
                    'components' => [
74
                        'request' => [
75
                            'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
76
                            'scriptFile' => __DIR__ . '/index.php',
77
                            'scriptUrl' => '/index.php',
78
                        ],
79
                        'assetManager' => [
80
                            'class' => 'tests\AssetManager',
81
                            'basePath' => '@tests/assets',
82
                            'baseUrl' => '/',
83
                        ]
84
                    ]
85
                ],
86
                $config
87
            )
88
        );
89
    }
90
91
    protected function getVendorPath()
92
    {
93
        return dirname(dirname(__DIR__)) . '/vendor';
94
    }
95
96
    /**
97
     * Destroys application in Yii::$app by setting it to null.
98
     */
99
    protected function destroyApplication()
100
    {
101
        \Yii::$app = null;
102
    }
103
104
    /**
105
     * Creates a view for testing purposes
106
     *
107
     * @return View
108
     */
109
    protected function getView()
110
    {
111
        $view = new View();
112
        $view->setAssetManager(
113
            new AssetManager(
114
                [
115
                    'basePath' => '@tests/assets',
116
                    'baseUrl' => '/',
117
                ]
118
            )
119
        );
120
        return $view;
121
    }
122
123
    /**
124
     * Asserting two strings equality ignoring line endings
125
     *
126
     * @param string $expected
127
     * @param string $actual
128
     */
129
    public function assertEqualsWithoutLE($expected, $actual)
130
    {
131
        $expected = str_replace("\r\n", "\n", $expected);
132
        $actual = str_replace("\r\n", "\n", $actual);
133
        $this->assertEquals($expected, $actual);
134
    }
135
}