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 $config_class = 'CI_Config'; |
15
|
|
|
private static $controller_class; |
16
|
|
|
private static $autoload_dirs; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initialize CIPHPUnitTest |
20
|
|
|
* |
21
|
|
|
* @param array $autoload_dirs directories to search class file for autoloader |
22
|
|
|
* |
23
|
|
|
* Exclude from code coverage: This is test suite bootstrap code, so we |
24
|
|
|
* know it's executed, but because it's bootstrap code, it runs outside of |
25
|
|
|
* any coverage tracking. |
26
|
|
|
* |
27
|
|
|
* @codeCoverageIgnore |
28
|
|
|
*/ |
29
|
|
|
public static function init(array $autoload_dirs = null) |
30
|
|
|
{ |
31
|
|
|
if (! defined('TESTPATH')) { |
32
|
|
|
define('TESTPATH', APPPATH.'tests'.DIRECTORY_SEPARATOR); |
33
|
|
|
} |
34
|
|
|
// Current Bootstrap.php should define this, but in case it doesn't: |
35
|
|
|
if (! defined('CI_PHPUNIT_TESTPATH')) { |
36
|
|
|
define('CI_PHPUNIT_TESTPATH', dirname(__FILE__).DIRECTORY_SEPARATOR); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Fix CLI args |
40
|
|
|
$_server_backup = $_SERVER; |
41
|
|
|
$_SERVER['argv'] = [ |
42
|
|
|
'index.php', |
43
|
|
|
'welcome' // Dummy |
44
|
|
|
]; |
45
|
|
|
$_SERVER['argc'] = 2; |
46
|
|
|
|
47
|
|
|
self::$autoload_dirs = $autoload_dirs; |
48
|
|
|
|
49
|
|
|
$cwd_backup = getcwd(); |
50
|
|
|
|
51
|
|
|
// Load autoloader for ci-phpunit-test |
52
|
|
|
require __DIR__ . '/autoloader.php'; |
53
|
|
|
|
54
|
|
|
require TESTPATH . 'TestCase.php'; |
55
|
|
|
|
56
|
|
|
$db_test_case_file = TESTPATH . 'DbTestCase.php'; |
57
|
|
|
if (is_readable($db_test_case_file)) |
58
|
|
|
{ |
59
|
|
|
require $db_test_case_file; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$unit_test_case_file = TESTPATH . 'UnitTestCase.php'; |
63
|
|
|
if (is_readable($unit_test_case_file)) |
64
|
|
|
{ |
65
|
|
|
require $unit_test_case_file; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Replace a few Common functions |
69
|
|
|
require __DIR__ . '/replacing/core/Common.php'; |
70
|
|
|
require BASEPATH . 'core/Common.php'; |
71
|
|
|
|
72
|
|
|
// Workaround for missing CodeIgniter's error handler |
73
|
|
|
// See https://github.com/kenjis/ci-phpunit-test/issues/37 |
74
|
|
|
set_error_handler('_error_handler'); |
75
|
|
|
|
76
|
|
|
// Load new functions of CIPHPUnitTest |
77
|
|
|
require __DIR__ . '/functions.php'; |
78
|
|
|
// Load ci-phpunit-test CI_Loader |
79
|
|
|
require __DIR__ . '/replacing/core/Loader.php'; |
80
|
|
|
// Load ci-phpunit-test CI_Input |
81
|
|
|
require __DIR__ . '/replacing/core/Input.php'; |
82
|
|
|
// Load ci-phpunit-test CI_Output |
83
|
|
|
require __DIR__ . '/replacing/core/Output.php'; |
84
|
|
|
|
85
|
|
|
// Change current directory |
86
|
|
|
chdir(FCPATH); |
87
|
|
|
|
88
|
|
|
// Replace helpers before loading CI (which could auto load helpers) |
89
|
|
|
self::replaceHelpers(); |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* -------------------------------------------------------------------- |
93
|
|
|
* LOAD THE BOOTSTRAP FILE |
94
|
|
|
* -------------------------------------------------------------------- |
95
|
|
|
* |
96
|
|
|
* And away we go... |
97
|
|
|
*/ |
98
|
|
|
require __DIR__ . '/replacing/core/CodeIgniter.php'; |
99
|
|
|
|
100
|
|
|
// Create CodeIgniter instance |
101
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
102
|
|
|
{ |
103
|
|
|
new CI_Controller(); |
104
|
|
|
} |
105
|
|
|
else |
106
|
|
|
{ |
107
|
|
|
new MX_Controller(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// This code is here, not to cause errors with HMVC |
111
|
|
|
self::replaceLoader(); |
112
|
|
|
self::replaceConfig(); |
113
|
|
|
|
114
|
|
|
// Restore $_SERVER. We need this for NetBeans |
115
|
|
|
$_SERVER = $_server_backup; |
116
|
|
|
|
117
|
|
|
// Restore cwd to use `Usage: phpunit [options] <directory>` |
118
|
|
|
chdir($cwd_backup); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param bool $use_my_controller |
123
|
|
|
*/ |
124
|
|
|
public static function createCodeIgniterInstance($use_my_controller = false) |
125
|
|
|
{ |
126
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
127
|
|
|
{ |
128
|
|
|
if ($use_my_controller && self::hasMyController()) |
129
|
|
|
{ |
130
|
|
|
new self::$controller_class; |
131
|
|
|
} |
132
|
|
|
else |
133
|
|
|
{ |
134
|
|
|
new CI_Controller(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
else |
138
|
|
|
{ |
139
|
|
|
new CI(); |
140
|
|
|
new MX_Controller(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private static function hasMyController() |
145
|
|
|
{ |
146
|
|
|
if (self::$controller_class !== null) { |
147
|
|
|
return self::$controller_class !== 'CI_Controller'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$my_controller_file = |
151
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php'; |
152
|
|
|
|
153
|
|
|
if (file_exists($my_controller_file)) |
154
|
|
|
{ |
155
|
|
|
$controller_class = config_item('subclass_prefix') . 'Controller'; |
156
|
|
|
if ( ! class_exists($controller_class)) |
157
|
|
|
{ |
158
|
|
|
require $my_controller_file; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
self::$controller_class = $controller_class; |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
self::$controller_class = 'CI_Controller'; |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function wiredesignzHmvcInstalled() |
170
|
|
|
{ |
171
|
|
|
if (file_exists(APPPATH.'third_party/MX')) |
172
|
|
|
{ |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public static function getAutoloadDirs() |
180
|
|
|
{ |
181
|
|
|
return self::$autoload_dirs; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
View Code Duplication |
protected static function replaceLoader() |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$my_loader_file = |
187
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; |
188
|
|
|
|
189
|
|
|
if (file_exists($my_loader_file)) |
190
|
|
|
{ |
191
|
|
|
self::$loader_class = config_item('subclass_prefix') . 'Loader'; |
192
|
|
|
if ( ! class_exists(self::$loader_class)) |
193
|
|
|
{ |
194
|
|
|
require $my_loader_file; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
self::loadLoader(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
View Code Duplication |
protected static function replaceConfig() |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$my_config_file = |
203
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Config.php'; |
204
|
|
|
|
205
|
|
|
if (file_exists($my_config_file)) |
206
|
|
|
{ |
207
|
|
|
self::$config_class = config_item('subclass_prefix') . 'Config'; |
208
|
|
|
if ( ! class_exists(self::$config_class)) |
209
|
|
|
{ |
210
|
|
|
require $my_config_file; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
self::loadConfig(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected static function replaceHelpers() |
217
|
|
|
{ |
218
|
|
|
$helpers = ['url_helper', 'download_helper']; |
219
|
|
|
foreach ($helpers as $helper) { |
220
|
|
|
static::loadHelper($helper); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected static function loadHelper($helper) |
225
|
|
|
{ |
226
|
|
|
$my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . $helper . '.php'; |
227
|
|
|
if (file_exists($my_helper_file)) |
228
|
|
|
{ |
229
|
|
|
require $my_helper_file; |
230
|
|
|
} |
231
|
|
|
require __DIR__ . '/replacing/helpers/' . $helper . '.php'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public static function setPatcherCacheDir($dir = null) |
235
|
|
|
{ |
236
|
|
|
if ($dir === null) |
237
|
|
|
{ |
238
|
|
|
$dir = CI_PHPUNIT_TESTPATH . 'tmp/cache'; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
MonkeyPatchManager::setCacheDir( |
242
|
|
|
$dir |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public static function loadLoader() |
247
|
|
|
{ |
248
|
|
|
$loader = new self::$loader_class; |
249
|
|
|
load_class_instance('Loader', $loader); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public static function loadConfig() |
253
|
|
|
{ |
254
|
|
|
$config= new self::$config_class; |
255
|
|
|
load_class_instance('Config', $config); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.