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