• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kenjis / ci-phpunit-test / 1025

pending completion
1025

push

travis-ci-com

web-flow
Merge pull request #394 from dezsi-istvan/3.x

pre_controller can modify $class / $method

4 of 4 new or added lines in 1 file covered. (100.0%)

1321 of 1739 relevant lines covered (75.96%)

35.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.99
/application/tests/_ci_phpunit_test/CIPHPUnitTest.php
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 $config_class = 'CI_Config';
15
        private static $controller_class;
16
        private static $autoload_dirs;
17

18
        /**
19
         * Initialize CIPHPUnitTest
20
         *
21
         * @param array $autoload_dirs directories to search class file for autoloader
22
         *
23
         * Exclude from code coverage: This is test suite bootstrap code, so we
24
         * know it's executed, but because it's bootstrap code, it runs outside of
25
         * any coverage tracking.
26
         *
27
         * @codeCoverageIgnore
28
         */
29
        public static function init(array $autoload_dirs = null)
30
        {
31
                self::defineConstants();
32

33
                // Fix CLI args
34
                $_server_backup = $_SERVER;
35
                $_SERVER['argv'] = [
36
                        'index.php',
37
                        'welcome'        // Dummy
38
                ];
39
                $_SERVER['argc'] = 2;
40

41
                self::$autoload_dirs = $autoload_dirs;
42

43
                $cwd_backup = getcwd();
44

45
                // Load autoloader for ci-phpunit-test
46
                require __DIR__ . '/autoloader.php';
47

48
                self::loadTestCaseClasses();
49

50
                // Replace a few Common functions
51
                require __DIR__ . '/replacing/core/Common.php';
52
                require BASEPATH . 'core/Common.php';
53

54
                // Workaround for missing CodeIgniter's error handler
55
                // See https://github.com/kenjis/ci-phpunit-test/issues/37
56
                set_error_handler('_error_handler');
57

58
                // Load new functions of CIPHPUnitTest
59
                require __DIR__ . '/functions.php';
60
                // Load ci-phpunit-test CI_Loader
61
                require __DIR__ . '/replacing/core/Loader.php';
62
                // Load ci-phpunit-test CI_Input
63
                require __DIR__ . '/replacing/core/Input.php';
64
                // Load ci-phpunit-test CI_Output
65
                require __DIR__ . '/replacing/core/Output.php';
66

67
                // Change current directory
68
                chdir(FCPATH);
69

70
                self::loadCodeIgniter();
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
                if (self::wiredesignzHmvcInstalled()) {
85
                        self::replaceConfig();
86
                }
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
        private static function loadCodeIgniter(){
96
                // Load constants.php before replacing helpers,
97
                // because config_item() loads config.php
98
                if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
×
99
                {
100
                        require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
×
101
                }
102

103
                if (file_exists(APPPATH.'config/constants.php'))
×
104
                {
105
                        require_once(APPPATH.'config/constants.php');
×
106
                }
107

108
                // Replace helpers before loading CI (which could auto load helpers)
109
                self::replaceHelpers();
×
110

111
                /*
112
                 * --------------------------------------------------------------------
113
                 * LOAD THE BOOTSTRAP FILE
114
                 * --------------------------------------------------------------------
115
                 *
116
                 * And away we go...
117
                 */
118
                require __DIR__ . '/replacing/core/CodeIgniter.php';
×
119
        }
120

121
        private static function defineConstants()
122
        {
123
                if (! defined('TESTPATH')) {
×
124
                        define('TESTPATH', APPPATH.'tests'.DIRECTORY_SEPARATOR);
×
125
                }
126
                // Current Bootstrap.php should define this, but in case it doesn't:
127
                if (! defined('CI_PHPUNIT_TESTPATH')) {
×
128
                        define('CI_PHPUNIT_TESTPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
×
129
                }
130
        }
131

132
        private static function loadTestCaseClasses()
133
        {
134
                require TESTPATH . 'TestCase.php';
×
135

136
                $db_test_case_file = TESTPATH . 'DbTestCase.php';
×
137
                if (is_readable($db_test_case_file))
×
138
                {
139
                        require $db_test_case_file;
×
140
                }
141

142
                $unit_test_case_file = TESTPATH . 'UnitTestCase.php';
×
143
                if (is_readable($unit_test_case_file))
×
144
                {
145
                        require $unit_test_case_file;
×
146
                }
147
        }
148

149
        /**
150
         * @param bool $use_my_controller
151
         */
152
        public static function createCodeIgniterInstance($use_my_controller = false)
153
        {
154
                if (! self::wiredesignzHmvcInstalled())
25✔
155
                {
156
                        if ($use_my_controller && self::hasMyController())
25✔
157
                        {
158
                                new self::$controller_class;
1✔
159
                        }
160
                        else
161
                        {
162
                                new CI_Controller();
25✔
163
                        }
164
                }
165
                else
166
                {
167
                        new CI();
×
168
                        new MX_Controller();
×
169
                }
170
        }
171

172
        private static function hasMyController()
173
        {
174
                if (self::$controller_class !== null) {
1✔
175
                        return self::$controller_class !== 'CI_Controller';
×
176
                }
177

178
                $my_controller_file =
1✔
179
                        APPPATH . 'core/' . config_item('subclass_prefix') . 'Controller.php';
1✔
180

181
                if (file_exists($my_controller_file))
1✔
182
                {
183
                        $controller_class = config_item('subclass_prefix') . 'Controller';
1✔
184
                        if ( ! class_exists($controller_class))
1✔
185
                        {
186
                                require $my_controller_file;
×
187
                        }
188

189
                        self::$controller_class = $controller_class;
1✔
190
                        return true;
1✔
191
                }
192

193
                self::$controller_class = 'CI_Controller';
×
194
                return false;
×
195
        }
196

197
        public static function wiredesignzHmvcInstalled()
198
        {
199
                if (file_exists(APPPATH.'third_party/MX'))
167✔
200
                {
201
                        return true;
×
202
                }
203

204
                return false;
167✔
205
        }
206

207
        public static function getAutoloadDirs()
208
        {
209
                return self::$autoload_dirs;
×
210
        }
211

212
        protected static function replaceLoader()
213
        {
214
                $my_loader_file =
×
215
                        APPPATH . 'core/' . config_item('subclass_prefix') . 'Loader.php';
×
216

217
                if (file_exists($my_loader_file))
×
218
                {
219
                        self::$loader_class = config_item('subclass_prefix') . 'Loader';
×
220
                        if ( ! class_exists(self::$loader_class))
×
221
                        {
222
                                require $my_loader_file;
×
223
                        }
224
                }
225
                self::loadLoader();
×
226
        }
227

228
        protected static function replaceConfig()
229
        {
230
                $my_config_file =
×
231
                        APPPATH . 'core/' . config_item('subclass_prefix') . 'Config.php';
×
232

233
                if (file_exists($my_config_file))
×
234
                {
235
                        self::$config_class = config_item('subclass_prefix') . 'Config';
×
236
                        if ( ! class_exists(self::$config_class))
×
237
                        {
238
                                require $my_config_file;
×
239
                        }
240
                }
241
                self::loadConfig();
×
242
        }
243

244
        protected static function replaceHelpers()
245
        {
246
                $helpers = ['url_helper', 'download_helper'];
×
247
                foreach ($helpers as $helper) {
×
248
                        static::loadHelper($helper);
×
249
                }
250
        }
251

252
        protected static function loadHelper($helper)
253
        {
254
                $my_helper_file = APPPATH . 'helpers/' . config_item('subclass_prefix') . $helper . '.php';
×
255
                if (file_exists($my_helper_file))
×
256
                {
257
                        require $my_helper_file;
×
258
                }
259
                require __DIR__ . '/replacing/helpers/' . $helper . '.php';
×
260
        }
261

262
        public static function setPatcherCacheDir($dir = null)
263
        {
264
                if ($dir === null)
1✔
265
                {
266
                        $dir = CI_PHPUNIT_TESTPATH . 'tmp/cache';
×
267
                }
268

269
                MonkeyPatchManager::setCacheDir(
1✔
270
                        $dir
1✔
271
                );
1✔
272
        }
273

274
        public static function loadLoader()
275
        {
276
                $loader = new self::$loader_class;
160✔
277
                load_class_instance('Loader', $loader);
160✔
278
        }
279

280
        public static function loadConfig()
281
        {
282
                $config= new self::$config_class;
×
283
                load_class_instance('Config', $config);
×
284
        }
285
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc