Completed
Push — master ( 03203b...aac8a8 )
by Kenji
10s
created

CIPHPUnitTest::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 73
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 29
nc 4
nop 1
dl 0
loc 73
rs 9.0675
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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