forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
235 lines (211 loc) · 6.87 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit;
use Yii;
use yii\helpers\ArrayHelper;
/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
public static $params;
/**
* Clean up after test case.
*/
public static function tearDownAfterClass()
{
parent::tearDownAfterClass();
$logger = Yii::getLogger();
$logger->flush();
}
/**
* Returns a test configuration param from /data/config.php.
* @param string $name params name
* @param mixed $default default value to use when param is not set.
* @return mixed the value of the configuration param
*/
public static function getParam($name, $default = null)
{
if (static::$params === null) {
static::$params = require __DIR__ . '/data/config.php';
}
return isset(static::$params[$name]) ? static::$params[$name] : $default;
}
/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown()
{
parent::tearDown();
$this->destroyApplication();
}
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
* @param string $appClass name of the application class to create
*/
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => $this->getVendorPath(),
], $config));
}
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => $this->getVendorPath(),
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
],
],
], $config));
}
protected function getVendorPath()
{
$vendor = dirname(dirname(__DIR__)) . '/vendor';
if (!is_dir($vendor)) {
$vendor = dirname(dirname(dirname(dirname(__DIR__))));
}
return $vendor;
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
if (\Yii::$app && \Yii::$app->has('session', true)) {
\Yii::$app->session->close();
}
\Yii::$app = null;
}
/**
* Asserting two strings equality ignoring line endings.
* @param string $expected
* @param string $actual
* @param string $message
*/
protected function assertEqualsWithoutLE($expected, $actual, $message = '')
{
$expected = str_replace("\r\n", "\n", $expected);
$actual = str_replace("\r\n", "\n", $actual);
$this->assertEquals($expected, $actual, $message);
}
/**
* Asserts that a haystack contains a needle ignoring line endings.
*
* @param mixed $needle
* @param mixed $haystack
* @param string $message
*/
protected function assertContainsWithoutLE($needle, $haystack, $message = '')
{
$needle = str_replace("\r\n", "\n", $needle);
$haystack = str_replace("\r\n", "\n", $haystack);
$this->assertContains($needle, $haystack, $message);
}
/**
* Invokes a inaccessible method.
* @param $object
* @param $method
* @param array $args
* @param bool $revoke whether to make method inaccessible after execution
* @return mixed
* @since 2.0.11
*/
protected function invokeMethod($object, $method, $args = [], $revoke = true)
{
$reflection = new \ReflectionObject($object);
$method = $reflection->getMethod($method);
$method->setAccessible(true);
$result = $method->invokeArgs($object, $args);
if ($revoke) {
$method->setAccessible(false);
}
return $result;
}
/**
* Sets an inaccessible object property to a designated value.
* @param $object
* @param $propertyName
* @param $value
* @param bool $revoke whether to make property inaccessible after setting
* @since 2.0.11
*/
protected function setInaccessibleProperty($object, $propertyName, $value, $revoke = true)
{
$class = new \ReflectionClass($object);
while (!$class->hasProperty($propertyName)) {
$class = $class->getParentClass();
}
$property = $class->getProperty($propertyName);
$property->setAccessible(true);
$property->setValue($object, $value);
if ($revoke) {
$property->setAccessible(false);
}
}
/**
* Gets an inaccessible object property.
* @param $object
* @param $propertyName
* @param bool $revoke whether to make property inaccessible after getting
* @return mixed
*/
protected function getInaccessibleProperty($object, $propertyName, $revoke = true)
{
$class = new \ReflectionClass($object);
while (!$class->hasProperty($propertyName)) {
$class = $class->getParentClass();
}
$property = $class->getProperty($propertyName);
$property->setAccessible(true);
$result = $property->getValue($object);
if ($revoke) {
$property->setAccessible(false);
}
return $result;
}
/**
* Asserts that value is one of expected values.
*
* @param mixed $actual
* @param array $expected
* @param string $message
*/
public function assertIsOneOf($actual, array $expected, $message = '')
{
self::assertThat($actual, new IsOneOfAssert($expected), $message);
}
/**
* Changes db component config
* @param $db
*/
protected function switchDbConnection($db)
{
$databases = $this->getParam('databases');
if (isset($databases[$db])) {
$database = $databases[$db];
Yii::$app->db->close();
Yii::$app->db->dsn = isset($database['dsn']) ? $database['dsn'] : null;
Yii::$app->db->username = isset($database['username']) ? $database['username'] : null;
Yii::$app->db->password = isset($database['password']) ? $database['password'] : null;
}
}
}