1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of ci-phpunit-test |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class CIPHPUnitTest |
12
|
|
|
{ |
13
|
|
|
private static $loader_class = 'CI_Loader'; |
14
|
|
|
private static $controller_class; |
15
|
|
|
private static $autoload_dirs; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initialize CIPHPUnitTest |
19
|
|
|
* |
20
|
|
|
* @param array $autoload_dirs directories to search class file for autoloader |
21
|
|
|
*/ |
22
|
|
|
public static function init(array $autoload_dirs = null) |
23
|
|
|
{ |
24
|
|
|
if (! defined('TESTPATH')) { |
25
|
|
|
define('TESTPATH', APPPATH.'tests'.DIRECTORY_SEPARATOR); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Fix CLI args |
29
|
|
|
$_server_backup = $_SERVER; |
30
|
|
|
$_SERVER['argv'] = [ |
31
|
|
|
'index.php', |
32
|
|
|
'welcome' // Dummy |
33
|
|
|
]; |
34
|
|
|
$_SERVER['argc'] = 2; |
35
|
|
|
|
36
|
|
|
self::$autoload_dirs = $autoload_dirs; |
37
|
|
|
|
38
|
|
|
$cwd_backup = getcwd(); |
39
|
|
|
|
40
|
|
|
// Load autoloader for ci-phpunit-test |
41
|
|
|
require __DIR__ . '/autoloader.php'; |
42
|
|
|
|
43
|
|
|
require TESTPATH . 'TestCase.php'; |
44
|
|
|
|
45
|
|
|
$db_test_case_file = TESTPATH . 'DbTestCase.php'; |
46
|
|
|
if (is_readable($db_test_case_file)) |
47
|
|
|
{ |
48
|
|
|
require $db_test_case_file; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$unit_test_case_file = TESTPATH . 'UnitTestCase.php'; |
52
|
|
|
if (is_readable($unit_test_case_file)) |
53
|
|
|
{ |
54
|
|
|
require $unit_test_case_file; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Replace a few Common functions |
58
|
|
|
require __DIR__ . '/replacing/core/Common.php'; |
59
|
|
|
require BASEPATH . 'core/Common.php'; |
60
|
|
|
|
61
|
|
|
// Workaround for missing CodeIgniter's error handler |
62
|
|
|
// See https://github.com/kenjis/ci-phpunit-test/issues/37 |
63
|
|
|
set_error_handler('_error_handler'); |
64
|
|
|
|
65
|
|
|
// Load new functions of CIPHPUnitTest |
66
|
|
|
require __DIR__ . '/functions.php'; |
67
|
|
|
// Load ci-phpunit-test CI_Loader |
68
|
|
|
require __DIR__ . '/replacing/core/Loader.php'; |
69
|
|
|
// Load ci-phpunit-test CI_Input |
70
|
|
|
require __DIR__ . '/replacing/core/Input.php'; |
71
|
|
|
// Load ci-phpunit-test CI_Output |
72
|
|
|
require __DIR__ . '/replacing/core/Output.php'; |
73
|
|
|
|
74
|
|
|
// Change current directory |
75
|
|
|
chdir(FCPATH); |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* -------------------------------------------------------------------- |
79
|
|
|
* LOAD THE BOOTSTRAP FILE |
80
|
|
|
* -------------------------------------------------------------------- |
81
|
|
|
* |
82
|
|
|
* And away we go... |
83
|
|
|
*/ |
84
|
|
|
require __DIR__ . '/replacing/core/CodeIgniter.php'; |
85
|
|
|
|
86
|
|
|
self::replaceHelpers(); |
87
|
|
|
|
88
|
|
|
// Create CodeIgniter instance |
89
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
90
|
|
|
{ |
91
|
|
|
new CI_Controller(); |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
new MX_Controller(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// This code is here, not to cause errors with HMVC |
99
|
|
|
self::replaceLoader(); |
100
|
|
|
|
101
|
|
|
// Restore $_SERVER. We need this for NetBeans |
102
|
|
|
$_SERVER = $_server_backup; |
103
|
|
|
|
104
|
|
|
// Restore cwd to use `Usage: phpunit [options] <directory>` |
105
|
|
|
chdir($cwd_backup); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param bool $use_my_controller |
110
|
|
|
*/ |
111
|
|
|
public static function createCodeIgniterInstance($use_my_controller = false) |
112
|
|
|
{ |
113
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
114
|
|
|
{ |
115
|
|
|
if ($use_my_controller && self::hasMyController()) |
116
|
|
|
{ |
117
|
|
|
new self::$controller_class; |
118
|
|
|
} |
119
|
|
|
else |
120
|
|
|
{ |
121
|
|
|
new CI_Controller(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
else |
125
|
|
|
{ |
126
|
|
|
new CI(); |
127
|
|
|
new MX_Controller(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private static function hasMyController() |
132
|
|
|
{ |
133
|
|
|
if (self::$controller_class !== null) { |
134
|
|
|
return self::$controller_class !== 'CI_Controller'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$my_controller_file = |
138
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php'; |
139
|
|
|
|
140
|
|
|
if (file_exists($my_controller_file)) |
141
|
|
|
{ |
142
|
|
|
$controller_class = config_item('subclass_prefix') . 'Controller'; |
143
|
|
|
if ( ! class_exists($controller_class)) |
144
|
|
|
{ |
145
|
|
|
require $my_controller_file; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
self::$controller_class = $controller_class; |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
self::$controller_class = 'CI_Controller'; |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public static function wiredesignzHmvcInstalled() |
157
|
|
|
{ |
158
|
|
|
if (file_exists(APPPATH.'third_party/MX')) |
159
|
|
|
{ |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function getAutoloadDirs() |
167
|
|
|
{ |
168
|
|
|
return self::$autoload_dirs; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected static function replaceLoader() |
172
|
|
|
{ |
173
|
|
|
$my_loader_file = |
174
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; |
175
|
|
|
|
176
|
|
|
if (file_exists($my_loader_file)) |
177
|
|
|
{ |
178
|
|
|
self::$loader_class = config_item('subclass_prefix') . 'Loader'; |
179
|
|
|
if ( ! class_exists(self::$loader_class)) |
180
|
|
|
{ |
181
|
|
|
require $my_loader_file; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
self::loadLoader(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected static function replaceHelpers() |
188
|
|
|
{ |
189
|
|
|
$helpers = ['url_helper', 'download_helper']; |
190
|
|
|
foreach ($helpers as $helper) { |
191
|
|
|
static::loadHelper($helper); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected static function loadHelper($helper) |
196
|
|
|
{ |
197
|
|
|
$my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . $helper . '.php'; |
198
|
|
|
if (file_exists($my_helper_file)) |
199
|
|
|
{ |
200
|
|
|
require $my_helper_file; |
201
|
|
|
} |
202
|
|
|
require __DIR__ . '/replacing/helpers/' . $helper . '.php'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public static function setPatcherCacheDir($dir = null) |
206
|
|
|
{ |
207
|
|
|
if ($dir === null) |
208
|
|
|
{ |
209
|
|
|
$dir = TESTPATH . '_ci_phpunit_test/tmp/cache'; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
MonkeyPatchManager::setCacheDir( |
213
|
|
|
$dir |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public static function loadLoader() |
218
|
|
|
{ |
219
|
|
|
$loader = new self::$loader_class; |
220
|
|
|
load_class_instance('Loader', $loader); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|