forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCase.php
163 lines (142 loc) · 4.55 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
<?php
namespace yiiunit;
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;
/**
* 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(),
'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
*/
protected function assertEqualsWithoutLE($expected, $actual)
{
$expected = str_replace("\r\n", "\n", $expected);
$actual = str_replace("\r\n", "\n", $actual);
$this->assertEquals($expected, $actual);
}
/**
* Invokes a inaccessible method
* @param $object
* @param $method
* @param array $args
* @return mixed
* @since 2.0.11
*/
protected function invokeMethod($object, $method, $args = [])
{
$reflection = new \ReflectionClass($object->className());
$method = $reflection->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}
/**
* Sets an inaccessible object property to a designated value
* @param $object
* @param $propertyName
* @param $value
* @param bool $revoke
* @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($value);
if($revoke){
$property->setAccessible(false);
}
}
/**
* Gets an inaccessible object property
* @param $object
* @param $propertyName
* @return mixed
*/
protected function getInaccessibleProperty($object, $propertyName)
{
$class = new \ReflectionClass($object);
while (!$class->hasProperty($propertyName)) {
$class = $class->getParentClass();
}
$property = $class->getProperty($propertyName);
$property->setAccessible(true);
return $property->getValue($object);
}
}