Passed
Push — master ( 00f279...d30425 )
by Dmitry
11:08
created

TestCase::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace kdn\yii2;
4
5
use PHPUnit_Framework_TestCase;
1 ignored issue
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Yii;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * Class TestCase.
11
 * @package kdn\yii2
12
 */
13
abstract class TestCase extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * Mock Yii application.
17
     * @before
18
     */
19
    protected function prepare()
20
    {
21
        static::mockWebApplication();
22
    }
23
24
    /**
25
     * Clean up after test.
26
     * By default, the application created with `mockWebApplication` will be destroyed.
27
     * @after
28
     */
29
    protected function clear()
30
    {
31
        static::destroyApplication();
32
    }
33
34
    /**
35
     * Populates Yii::$app with a new application.
36
     * The application will be destroyed on clear() automatically.
37
     * @param array $config the application configuration, if needed
38
     * @param string $appClass name of the application class to create
39
     */
40
    protected static function mockWebApplication($config = [], $appClass = 'yii\web\Application')
41
    {
42
        new $appClass(
43
            ArrayHelper::merge(
44
                [
45
                    'id' => 'test-app',
46
                    'basePath' => __DIR__,
47
                    'components' => [
48
                        'assetManager' => [
49
                            'linkAssets' => true,
50
                        ],
51
                        'request' => [
52
                            'scriptFile' => static::getTestsRuntimePath() . '/index.php',
53
                            'scriptUrl' => '/index.php',
54
                        ],
55
                    ],
56
                    'vendorPath' => static::getVendorPath(),
57
                ],
58
                $config
59
            )
60
        );
61
    }
62
63
    /**
64
     * Get path to "vendor" directory.
65
     * @return string path to "vendor" directory.
66
     */
67
    protected static function getVendorPath()
68
    {
69
        return dirname(__DIR__) . '/vendor';
70
    }
71
72
    /**
73
     * Get path to tests "runtime" directory.
74
     * @return string path to tests "runtime" directory.
75
     */
76
    protected static function getTestsRuntimePath()
77
    {
78
        return __DIR__ . '/runtime';
79
    }
80
81
    /**
82
     * Destroys application in Yii::$app by setting it to null.
83
     */
84
    protected static function destroyApplication()
85
    {
86
        Yii::$app = null;
87
    }
88
89
    /**
90
     * Asserts that the contents of a string is equal to the contents of an HTML file.
91
     * @param string $fileRoot name of file without extension, "stem"
92
     * @param string $actualString
93
     * @param string $message
94
     * @param bool $canonicalize
95
     * @param bool $ignoreCase
96
     */
97
    public static function assertStringEqualsHtmlFile(
98
        $fileRoot,
99
        $actualString,
100
        $message = '',
101
        $canonicalize = false,
102
        $ignoreCase = false
103
    ) {
104
        static::assertStringEqualsFile(
105
            __DIR__ . "/expected/$fileRoot.html",
106
            $actualString,
107
            $message,
108
            $canonicalize,
109
            $ignoreCase
110
        );
111
    }
112
113
    /**
114
     * Asserts that the contents of a string is equal to the contents of a JavaScript file.
115
     * @param string $fileRoot name of file without extension, "stem"
116
     * @param string $actualString
117
     * @param string $message
118
     * @param bool $canonicalize
119
     * @param bool $ignoreCase
120
     */
121
    public static function assertStringEqualsJsFile(
122
        $fileRoot,
123
        $actualString,
124
        $message = '',
125
        $canonicalize = false,
126
        $ignoreCase = false
127
    ) {
128
        static::assertStringEqualsFile(
129
            __DIR__ . "/expected/$fileRoot.js",
130
            $actualString,
131
            $message,
132
            $canonicalize,
133
            $ignoreCase
134
        );
135
    }
136
}
137