TestCase::mockApplication()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-mobile-first
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\test\unit\mobileFirst;
9
10
use vxm\mobileFirst\AdaptiveFilter;
11
use vxm\mobileFirst\ViewRenderBehavior;
12
use Yii;
13
14
use yii\helpers\ArrayHelper;
15
16
use PHPUnit\Framework\TestCase as BaseTestCase;
17
18
/**
19
 * Class TestCase
20
 *
21
 * @author Vuong Minh <[email protected]>
22
 * @since 1.0
23
 */
24
class TestCase extends BaseTestCase
25
{
26
27
28
    public function setUp(): void
29
    {
30
        parent::setUp();
31
32
        $this->mockApplication();
33
    }
34
35
    public function tearDown(): void
36
    {
37
        parent::tearDown();
38
39
        $this->destroyApplication();
40
    }
41
42
    /**
43
     * Populates Yii::$app with a new application
44
     * The application will be destroyed on tearDown() automatically.
45
     * @param array $config The application configuration, if needed
46
     * @param string $appClass name of the application class to create
47
     */
48
    protected function mockApplication($config = [], $appClass = '\yii\web\Application'): void
49
    {
50
        new $appClass(ArrayHelper::merge([
51
            'id' => 'test',
52
            'basePath' => __DIR__,
53
            'controllerMap' => [
54
                'test' => TestController::class
55
            ],
56
            'vendorPath' => dirname(__DIR__) . '/vendor',
57
            'components' => [
58
                'request' => [
59
                    'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
60
                    'scriptFile' => __DIR__ . '/index.php',
61
                    'scriptUrl' => '/index.php',
62
                    'url' => 'http://abc.test'
63
                ],
64
                'view' => [
65
                    'as mobileFirst' => [
66
                        'class' => ViewRenderBehavior::class,
67
                        'dirMap' => [
68
                            'mobile' => 'mobile',
69
                            'tablet' => 'tablet'
70
                        ]
71
                    ],
72
                    'renderers' => [
73
                        'php' => TestRenderer::class
74
                    ]
75
                ]
76
            ],
77
            'as mobileFirst' => [
78
                'class' => AdaptiveFilter::class,
79
                'redirectUrl' => 'https://abc.com'
80
            ]
81
        ], $config));
82
    }
83
84
    /**
85
     * Destroys application in Yii::$app by setting it to null.
86
     */
87
    protected function destroyApplication(): void
88
    {
89
        $_SERVER['HTTP_USER_AGENT'] = null;
90
        $_SERVER['REQUEST_METHOD'] = 'GET';
91
92
        Yii::$app = null;
93
    }
94
95
96
}
97