Completed
Push — master ( 5536b6...134bc1 )
by Kenji
02:32
created

CIPHPUnitTest::createCodeIgniterInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 9.4285
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

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