Completed
Push — master ( cda6f6...6b8ea4 )
by Kenji
03:20
created

CIPHPUnitTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 135
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 65 2
A wiredesignzHmvcInstalled() 0 9 2
A getAutoloadDirs() 0 4 1
A replaceLoader() 0 15 3
A replaceHelpers() 0 9 2
A setPatcherCacheDir() 0 11 2
A loadLoader() 0 5 1
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
56
		// Change current directroy
57
		chdir(FCPATH);
58
59
		/*
60
		 * --------------------------------------------------------------------
61
		 * LOAD THE BOOTSTRAP FILE
62
		 * --------------------------------------------------------------------
63
		 *
64
		 * And away we go...
65
		 */
66
		require __DIR__ . '/replacing/core/CodeIgniter.php';
67
68
		self::replaceHelpers();
69
70
		// Create CodeIgniter instance
71
		if (! self::wiredesignzHmvcInstalled())
72
		{
73
			new CI_Controller();
74
		}
75
		else
76
		{
77
			new MX_Controller();
78
		}
79
80
		// This code is here, not to cause errors with HMVC
81
		self::replaceLoader();
82
83
		// Restore $_SERVER. We need this for NetBeans
84
		$_SERVER = $_server_backup;
85
	}
86
87
	public static function wiredesignzHmvcInstalled()
88
	{
89
		if (file_exists(APPPATH.'third_party/MX'))
90
		{
91
			return true;
92
		}
93
		
94
		return false;
95
	}
96
97
	public static function getAutoloadDirs()
98
	{
99
		return self::$autoload_dirs;
100
	}
101
102
	protected static function replaceLoader()
103
	{
104
		$my_loader_file = 
105
			APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php';
106
107
		if (file_exists($my_loader_file))
108
		{
109
			self::$loader_class = config_item('subclass_prefix') . 'Loader';
110
			if ( ! class_exists(self::$loader_class))
111
			{
112
				require $my_loader_file;
113
			}
114
		}
115
		self::loadLoader();
116
	}
117
118
	protected static function replaceHelpers()
119
	{
120
		$my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . 'url_helper.php';
121
		if (file_exists($my_helper_file))
122
		{
123
			require $my_helper_file;
124
		}
125
		require __DIR__ . '/replacing/helpers/url_helper.php';
126
	}
127
128
	public static function setPatcherCacheDir($dir = null)
129
	{
130
		if ($dir === null)
131
		{
132
			$dir = APPPATH . 'tests/_ci_phpunit_test/tmp/cache';
133
		}
134
135
		MonkeyPatchManager::setCacheDir(
136
			$dir
137
		);
138
	}
139
140
	public static function loadLoader()
141
	{
142
		$loader = new self::$loader_class;
143
		load_class_instance('Loader', $loader);
144
	}
145
}
146