|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kdn\yii2\validators; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class TestCase. |
|
11
|
|
|
* @package kdn\yii2\validators |
|
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::mockApplication(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Clean up after test. |
|
26
|
|
|
* By default the application created with `mockApplication` 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 mockApplication($config = [], $appClass = 'yii\console\Application') |
|
41
|
|
|
{ |
|
42
|
|
|
new $appClass( |
|
43
|
|
|
ArrayHelper::merge( |
|
44
|
|
|
[ |
|
45
|
|
|
'id' => 'test-app', |
|
46
|
|
|
'basePath' => __DIR__, |
|
47
|
|
|
'vendorPath' => static::getVendorPath(), |
|
48
|
|
|
], |
|
49
|
|
|
$config |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get path to "vendor" directory. |
|
56
|
|
|
* @return string path to "vendor" directory. |
|
57
|
|
|
*/ |
|
58
|
|
|
protected static function getVendorPath() |
|
59
|
|
|
{ |
|
60
|
|
|
return dirname(__DIR__) . '/vendor'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Destroys application in Yii::$app by setting it to null. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected static function destroyApplication() |
|
67
|
|
|
{ |
|
68
|
|
|
Yii::$app = null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add column to array. |
|
73
|
|
|
* @param array $array |
|
74
|
|
|
* @param mixed $value |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected static function arrayAddColumn($array, $value) |
|
78
|
|
|
{ |
|
79
|
|
|
return array_map( |
|
80
|
|
|
function ($data) use ($value) { |
|
81
|
|
|
$data[] = $value; |
|
82
|
|
|
return $data; |
|
83
|
|
|
}, |
|
84
|
|
|
$array |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* \u escape sequence for PHP. |
|
90
|
|
|
* @param string $text |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected static function u($text) |
|
94
|
|
|
{ |
|
95
|
|
|
return json_decode("\"$text\""); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|