1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace miloschumanunit\highcharts; |
4
|
|
|
|
5
|
|
|
use yii\helpers\ArrayHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is the base class for all yii framework unit tests. |
9
|
|
|
*/ |
10
|
|
|
abstract class BaseTestCase extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
public static $params; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Clean up after test. |
16
|
|
|
* By default the application created with [[mockApplication]] will be destroyed. |
17
|
|
|
*/ |
18
|
|
|
protected function tearDown() |
19
|
|
|
{ |
20
|
|
|
parent::tearDown(); |
21
|
|
|
$this->destroyApplication(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns a test configuration param from /data/config.php |
26
|
|
|
* @param string $name params name |
27
|
|
|
* @param mixed $default default value to use when param is not set. |
28
|
|
|
* @return mixed the value of the configuration param |
29
|
|
|
*/ |
30
|
|
|
public static function getParam($name, $default = null) |
31
|
|
|
{ |
32
|
|
|
if (static::$params === null) { |
33
|
|
|
static::$params = require(__DIR__ . '/data/config.php'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return isset(static::$params[$name]) ? static::$params[$name] : $default; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Populates Yii::$app with a new application |
41
|
|
|
* The application will be destroyed on tearDown() automatically. |
42
|
|
|
* @param array $config The application configuration, if needed |
43
|
|
|
* @param string $appClass name of the application class to create |
44
|
|
|
*/ |
45
|
|
|
protected function mockApplication($config = [], $appClass = '\yii\console\Application') |
46
|
|
|
{ |
47
|
|
|
new $appClass(ArrayHelper::merge([ |
48
|
|
|
'id' => 'testapp', |
49
|
|
|
'basePath' => __DIR__, |
50
|
|
|
'vendorPath' => $this->getVendorPath(), |
51
|
|
|
], $config)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application') |
55
|
|
|
{ |
56
|
|
|
new $appClass(ArrayHelper::merge([ |
57
|
|
|
'id' => 'testapp', |
58
|
|
|
'basePath' => __DIR__, |
59
|
|
|
'vendorPath' => $this->getVendorPath(), |
60
|
|
|
'components' => [ |
61
|
|
|
'request' => [ |
62
|
|
|
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq', |
63
|
|
|
'scriptFile' => __DIR__ .'/index.php', |
64
|
|
|
'scriptUrl' => '/index.php', |
65
|
|
|
], |
66
|
|
|
] |
67
|
|
|
], $config)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getVendorPath() |
71
|
|
|
{ |
72
|
|
|
$vendor = dirname(dirname(__DIR__)) . '/vendor'; |
73
|
|
|
if (!is_dir($vendor)) { |
74
|
|
|
$vendor = dirname(dirname(dirname(dirname(__DIR__)))); |
75
|
|
|
} |
76
|
|
|
return $vendor; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Destroys application in Yii::$app by setting it to null. |
81
|
|
|
*/ |
82
|
|
|
protected function destroyApplication() |
83
|
|
|
{ |
84
|
|
|
if (\Yii::$app && \Yii::$app->has('session', true)) { |
85
|
|
|
\Yii::$app->session->close(); |
86
|
|
|
} |
87
|
|
|
\Yii::$app = null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Asserting two strings equality ignoring line endings |
92
|
|
|
* |
93
|
|
|
* @param string $expected |
94
|
|
|
* @param string $actual |
95
|
|
|
*/ |
96
|
|
|
public function assertEqualsWithoutLE($expected, $actual) |
97
|
|
|
{ |
98
|
|
|
$expected = str_replace("\r\n", "\n", $expected); |
99
|
|
|
$actual = str_replace("\r\n", "\n", $actual); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($expected, $actual); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|