Completed
Pull Request — master (#271)
by Kenji
02:40
created

CIPHPUnitTest::hasMyController()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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