Completed
Pull Request — master (#311)
by
unknown
02:31
created

CIPHPUnitTest::loadHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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