1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kdn\yii2; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
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
|
|
|
*/ |
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
static::mockWebApplication(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Clean up after test. |
26
|
|
|
* By default the application created with `mockWebApplication` will be destroyed. |
27
|
|
|
*/ |
28
|
|
|
protected function tearDown() |
29
|
|
|
{ |
30
|
|
|
parent::tearDown(); |
31
|
|
|
static::destroyApplication(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Populates Yii::$app with a new application. |
36
|
|
|
* The application will be destroyed on tearDown() 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 a 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
|
|
|
|