Completed
Push — master ( 847a7b...91d06e )
by Kenji
18s queued 11s
created

CIPHPUnitTest::loadCodeIgniter()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
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
		self::defineConstants();
32
33
		// Fix CLI args
34
		$_server_backup = $_SERVER;
35
		$_SERVER['argv'] = [
36
			'index.php',
37
			'welcome'	// Dummy
38
		];
39
		$_SERVER['argc'] = 2;
40
41
		self::$autoload_dirs = $autoload_dirs;
42
43
		$cwd_backup = getcwd();
44
45
		// Load autoloader for ci-phpunit-test
46
		require __DIR__ . '/autoloader.php';
47
48
		self::loadTestCaseClasses();
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
		// Load ci-phpunit-test CI_Output
65
		require __DIR__ . '/replacing/core/Output.php';
66
67
		// Change current directory
68
		chdir(FCPATH);
69
70
		self::loadCodeIgniter();
71
72
		// Create CodeIgniter instance
73
		if (! self::wiredesignzHmvcInstalled())
74
		{
75
			new CI_Controller();
76
		}
77
		else
78
		{
79
			new MX_Controller();
80
		}
81
82
		// This code is here, not to cause errors with HMVC
83
		self::replaceLoader();
84
		if (self::wiredesignzHmvcInstalled()) {
85
			self::replaceConfig();
86
		}
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
	private static function loadCodeIgniter(){
96
		// Load constants.php before replacing helpers,
97
		// because config_item() loads config.php
98
		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
99
		{
100
			require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
101
		}
102
103
		if (file_exists(APPPATH.'config/constants.php'))
104
		{
105
			require_once(APPPATH.'config/constants.php');
106
		}
107
108
		// Replace helpers before loading CI (which could auto load helpers)
109
		self::replaceHelpers();
110
111
		/*
112
		 * --------------------------------------------------------------------
113
		 * LOAD THE BOOTSTRAP FILE
114
		 * --------------------------------------------------------------------
115
		 *
116
		 * And away we go...
117
		 */
118
		require __DIR__ . '/replacing/core/CodeIgniter.php';
119
	}
120
121
	private static function defineConstants()
122
	{
123
		if (! defined('TESTPATH')) {
124
			define('TESTPATH', APPPATH.'tests'.DIRECTORY_SEPARATOR);
125
		}
126
		// Current Bootstrap.php should define this, but in case it doesn't:
127
		if (! defined('CI_PHPUNIT_TESTPATH')) {
128
			define('CI_PHPUNIT_TESTPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
129
		}
130
	}
131
132
	private static function loadTestCaseClasses()
133
	{
134
		require TESTPATH . 'TestCase.php';
135
136
		$db_test_case_file = TESTPATH . 'DbTestCase.php';
137
		if (is_readable($db_test_case_file))
138
		{
139
			require $db_test_case_file;
140
		}
141
142
		$unit_test_case_file = TESTPATH . 'UnitTestCase.php';
143
		if (is_readable($unit_test_case_file))
144
		{
145
			require $unit_test_case_file;
146
		}
147
	}
148
149
	/**
150
	 * @param bool $use_my_controller
151
	 */
152
	public static function createCodeIgniterInstance($use_my_controller = false)
153
	{
154
		if (! self::wiredesignzHmvcInstalled())
155
		{
156
			if ($use_my_controller && self::hasMyController())
157
			{
158
				new self::$controller_class;
159
			}
160
			else
161
			{
162
				new CI_Controller();
163
			}
164
		}
165
		else
166
		{
167
			new CI();
168
			new MX_Controller();
169
		}
170
	}
171
172
	private static function hasMyController()
173
	{
174
		if (self::$controller_class !== null) {
175
			return self::$controller_class !== 'CI_Controller';
176
		}
177
178
		$my_controller_file =
179
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php';
180
181
		if (file_exists($my_controller_file))
182
		{
183
			$controller_class = config_item('subclass_prefix') . 'Controller';
184
			if ( ! class_exists($controller_class))
185
			{
186
				require $my_controller_file;
187
			}
188
189
			self::$controller_class = $controller_class;
190
			return true;
191
		}
192
193
		self::$controller_class = 'CI_Controller';
194
		return false;
195
	}
196
197
	public static function wiredesignzHmvcInstalled()
198
	{
199
		if (file_exists(APPPATH.'third_party/MX'))
200
		{
201
			return true;
202
		}
203
204
		return false;
205
	}
206
207
	public static function getAutoloadDirs()
208
	{
209
		return self::$autoload_dirs;
210
	}
211
212 View Code Duplication
	protected static function replaceLoader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
213
	{
214
		$my_loader_file =
215
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php';
216
217
		if (file_exists($my_loader_file))
218
		{
219
			self::$loader_class = config_item('subclass_prefix') . 'Loader';
220
			if ( ! class_exists(self::$loader_class))
221
			{
222
				require $my_loader_file;
223
			}
224
		}
225
		self::loadLoader();
226
	}
227
228 View Code Duplication
	protected static function replaceConfig()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
229
	{
230
		$my_config_file =
231
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Config.php';
232
233
		if (file_exists($my_config_file))
234
		{
235
			self::$config_class = config_item('subclass_prefix') . 'Config';
236
			if ( ! class_exists(self::$config_class))
237
			{
238
				require $my_config_file;
239
			}
240
		}
241
		self::loadConfig();
242
	}
243
244
	protected static function replaceHelpers()
245
	{
246
		$helpers = ['url_helper', 'download_helper'];
247
		foreach ($helpers as $helper) {
248
			static::loadHelper($helper);
249
		}
250
	}
251
252
	protected static function loadHelper($helper)
253
	{
254
		$my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . $helper . '.php';
255
		if (file_exists($my_helper_file))
256
		{
257
			require $my_helper_file;
258
		}
259
		require __DIR__ . '/replacing/helpers/' . $helper . '.php';
260
	}
261
262
	public static function setPatcherCacheDir($dir = null)
263
	{
264
		if ($dir === null)
265
		{
266
			$dir = CI_PHPUNIT_TESTPATH . 'tmp/cache';
267
		}
268
269
		MonkeyPatchManager::setCacheDir(
270
			$dir
271
		);
272
	}
273
274
	public static function loadLoader()
275
	{
276
		$loader = new self::$loader_class;
277
		load_class_instance('Loader', $loader);
278
	}
279
280
	public static function loadConfig()
281
	{
282
		$config= new self::$config_class;
283
		load_class_instance('Config', $config);
284
	}
285
}
286