Completed
Push — master ( 7091fd...28899b )
by Kenji
03:43
created

CIPHPUnitTest::createCodeIgniterInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

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