Completed
Pull Request — master (#327)
by
unknown
02:25
created

CIPHPUnitTest::wiredesignzHmvcInstalled()   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 0
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
	 * 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
	/**
123
	 * @param bool $use_my_controller
124
	 */
125
	public static function createCodeIgniterInstance($use_my_controller = false)
126
	{
127
		if (! self::wiredesignzHmvcInstalled())
128
		{
129
			if ($use_my_controller && self::hasMyController())
130
			{
131
				new self::$controller_class;
132
			}
133
			else
134
			{
135
				new CI_Controller();
136
			}
137
		}
138
		else
139
		{
140
			new CI();
141
			new MX_Controller();
142
		}
143
	}
144
145
	private static function hasMyController()
146
	{
147
		if (self::$controller_class !== null) {
148
			return self::$controller_class !== 'CI_Controller';
149
		}
150
151
		$my_controller_file =
152
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php';
153
154
		if (file_exists($my_controller_file))
155
		{
156
			$controller_class = config_item('subclass_prefix') . 'Controller';
157
			if ( ! class_exists($controller_class))
158
			{
159
				require $my_controller_file;
160
			}
161
162
			self::$controller_class = $controller_class;
163
			return true;
164
		}
165
166
		self::$controller_class = 'CI_Controller';
167
		return false;
168
	}
169
170
	public static function wiredesignzHmvcInstalled()
171
	{
172
		if (file_exists(APPPATH.'third_party/MX'))
173
		{
174
			return true;
175
		}
176
177
		return false;
178
	}
179
180
	public static function getAutoloadDirs()
181
	{
182
		return self::$autoload_dirs;
183
	}
184
185 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...
186
	{
187
		$my_loader_file =
188
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php';
189
190
		if (file_exists($my_loader_file))
191
		{
192
			self::$loader_class = config_item('subclass_prefix') . 'Loader';
193
			if ( ! class_exists(self::$loader_class))
194
			{
195
				require $my_loader_file;
196
			}
197
		}
198
		self::loadLoader();
199
	}
200 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...
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
	public static function loadConfig()
252
	{
253
		$config= new self::$config_class;
254
		load_class_instance('Config', $config);
255
	}
256
}
257