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 $autoload_dirs; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Initialize CIPHPUnitTest |
18
|
|
|
* |
19
|
|
|
* @param array $autoload_dirs directories to search class file for autoloader |
20
|
|
|
*/ |
21
|
|
|
public static function init(array $autoload_dirs = null) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if (! defined('TESTPATH')) { |
24
|
|
|
define('TESTPATH', APPPATH.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Fix CLI args |
28
|
|
|
$_server_backup = $_SERVER; |
29
|
|
|
$_SERVER['argv'] = [ |
30
|
|
|
'index.php', |
31
|
|
|
'welcome' // Dummy |
32
|
|
|
]; |
33
|
|
|
$_SERVER['argc'] = 2; |
34
|
|
|
|
35
|
|
|
self::$autoload_dirs = $autoload_dirs; |
36
|
|
|
|
37
|
|
|
$cwd_backup = getcwd(); |
38
|
|
|
|
39
|
|
|
// Load autoloader for ci-phpunit-test |
40
|
|
|
require __DIR__ . '/autoloader.php'; |
41
|
|
|
|
42
|
|
|
require TESTPATH . 'TestCase.php'; |
43
|
|
|
|
44
|
|
|
$db_test_case_file = TESTPATH . 'DbTestCase.php'; |
45
|
|
|
if (is_readable($db_test_case_file)) |
46
|
|
|
{ |
47
|
|
|
require $db_test_case_file; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Replace a few Common functions |
51
|
|
|
require __DIR__ . '/replacing/core/Common.php'; |
52
|
|
|
require BASEPATH . 'core/Common.php'; |
53
|
|
|
|
54
|
|
|
// Workaround for missing CodeIgniter's error handler |
55
|
|
|
// See https://github.com/kenjis/ci-phpunit-test/issues/37 |
56
|
|
|
set_error_handler('_error_handler'); |
57
|
|
|
|
58
|
|
|
// Load new functions of CIPHPUnitTest |
59
|
|
|
require __DIR__ . '/functions.php'; |
60
|
|
|
// Load ci-phpunit-test CI_Loader |
61
|
|
|
require __DIR__ . '/replacing/core/Loader.php'; |
62
|
|
|
// Load ci-phpunit-test CI_Input |
63
|
|
|
require __DIR__ . '/replacing/core/Input.php'; |
64
|
|
|
|
65
|
|
|
// Change current directory |
66
|
|
|
chdir(FCPATH); |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* -------------------------------------------------------------------- |
70
|
|
|
* LOAD THE BOOTSTRAP FILE |
71
|
|
|
* -------------------------------------------------------------------- |
72
|
|
|
* |
73
|
|
|
* And away we go... |
74
|
|
|
*/ |
75
|
|
|
require __DIR__ . '/replacing/core/CodeIgniter.php'; |
76
|
|
|
|
77
|
|
|
self::replaceHelpers(); |
78
|
|
|
|
79
|
|
|
// Create CodeIgniter instance |
80
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
81
|
|
|
{ |
82
|
|
|
new CI_Controller(); |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
|
|
new MX_Controller(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// This code is here, not to cause errors with HMVC |
90
|
|
|
self::replaceLoader(); |
91
|
|
|
|
92
|
|
|
// Restore $_SERVER. We need this for NetBeans |
93
|
|
|
$_SERVER = $_server_backup; |
94
|
|
|
|
95
|
|
|
// Restore cwd to use `Usage: phpunit [options] <directory>` |
96
|
|
|
chdir($cwd_backup); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function createCodeIgniterInstance() |
100
|
|
|
{ |
101
|
|
|
if (! self::wiredesignzHmvcInstalled()) |
102
|
|
|
{ |
103
|
|
|
new CI_Controller(); |
104
|
|
|
} |
105
|
|
|
else |
106
|
|
|
{ |
107
|
|
|
new CI(); |
108
|
|
|
new MX_Controller(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function wiredesignzHmvcInstalled() |
113
|
|
|
{ |
114
|
|
|
if (file_exists(APPPATH.'third_party/MX')) |
115
|
|
|
{ |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function getAutoloadDirs() |
123
|
|
|
{ |
124
|
|
|
return self::$autoload_dirs; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected static function replaceLoader() |
128
|
|
|
{ |
129
|
|
|
$my_loader_file = |
130
|
|
|
APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php'; |
131
|
|
|
|
132
|
|
|
if (file_exists($my_loader_file)) |
133
|
|
|
{ |
134
|
|
|
self::$loader_class = config_item('subclass_prefix') . 'Loader'; |
135
|
|
|
if ( ! class_exists(self::$loader_class)) |
136
|
|
|
{ |
137
|
|
|
require $my_loader_file; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
self::loadLoader(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected static function replaceHelpers() |
144
|
|
|
{ |
145
|
|
|
$helpers = ['url_helper', 'download_helper']; |
146
|
|
|
foreach ($helpers as $helper) { |
147
|
|
|
static::loadHelper($helper); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected static function loadHelper($helper) |
152
|
|
|
{ |
153
|
|
|
$my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . $helper . '.php'; |
154
|
|
|
if (file_exists($my_helper_file)) |
155
|
|
|
{ |
156
|
|
|
require $my_helper_file; |
157
|
|
|
} |
158
|
|
|
require __DIR__ . '/replacing/helpers/' . $helper . '.php'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function setPatcherCacheDir($dir = null) |
162
|
|
|
{ |
163
|
|
|
if ($dir === null) |
164
|
|
|
{ |
165
|
|
|
$dir = TESTPATH . '_ci_phpunit_test/tmp/cache'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
MonkeyPatchManager::setCacheDir( |
169
|
|
|
$dir |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public static function loadLoader() |
174
|
|
|
{ |
175
|
|
|
$loader = new self::$loader_class; |
176
|
|
|
load_class_instance('Loader', $loader); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.