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