• 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

85.71
/application/tests/_ci_phpunit_test/replacing/core/Common.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
// Load this file before loading "system/core/Common.php"
12

13
/**
14
 * Class registry
15
 *
16
 * @staticvar array $_classes
17
 *
18
 * @param string $class     the class name being requested
19
 * @param string $directory the directory where the class should be found
20
 * @param mixed  $param     an optional argument to pass to the class constructor
21
 * @param bool   $reset
22
 * @param object $obj
23
 *
24
 * @return object
25
 */
26
function &load_class(
27
        $class,
28
        $directory = 'libraries',
29
        $param = NULL,
30
        $reset = FALSE,
161✔
31
        $obj = NULL
160✔
32
)
33
{
34
        static $_classes = array();
35

36
        if ($reset)
161✔
37
        {
38
                // If Utf8 is instantiated twice,
39
                // error "Constant UTF8_ENABLED already defined" occurs
40
                $UTF8 = $_classes['Utf8'];
41
                $_classes = array(
160✔
42
                        'Utf8' => $UTF8
160✔
43
                );
160✔
44
                $obj = new stdClass();
45
                return $obj;
46
        }
47

48
        // Register object directly
49
        if ($obj)
160✔
50
        {
51
                is_loaded($class);
52

53
                $_classes[$class] = $obj;
160✔
54
                return $_classes[$class];
55
        }
56

57
        // Does the class exist? If so, we're done...
58
        if (isset($_classes[$class]))
59
        {
60
                return $_classes[$class];
160✔
61
        }
62

63
        $name = FALSE;
160✔
64

65
        // Look for the class first in the local application/libraries folder
66
        // then in the native system/libraries folder
67
        foreach (array(APPPATH, BASEPATH) as $path)
1✔
68
        {
69
                if (file_exists($path.$directory.'/'.$class.'.php'))
×
70
                {
71
                        $name = 'CI_'.$class;
72

73
                        if (class_exists($name, FALSE) === FALSE)
74
                        {
75
                                require_once($path.$directory.'/'.$class.'.php');
1✔
76
                        }
77

78
                        break;
79
                }
80
        }
81

82
        // Is the request a class extension? If so we load it too
83
        if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
84
        {
85
                $name = config_item('subclass_prefix').$class;
86

87
                if (class_exists($name, FALSE) === FALSE)
88
                {
89
                        require_once(APPPATH.$directory.'/'.$name.'.php');
162✔
90
                }
91
        }
92

93
        // Did we find the class?
94
        if ($name === FALSE)
162✔
95
        {
96
                // Note: We use exit() rather then show_error() in order to avoid a
97
                // self-referencing loop with the Exceptions class
98
                set_status_header(503);
×
99

100
                // changed by ci-phpunit-test
101
                $msg = 'Unable to locate the specified class: '.$class.'.php';
141✔
102
//                exit(5); // EXIT_UNK_CLASS
103
                throw new CIPHPUnitTestExitException($msg);
141✔
104
        }
105

106
        // Keep track of what we just loaded
107
        is_loaded($class);
108

109
        $_classes[$class] = isset($param)
141✔
110
                ? new $name($param)
141✔
111
                : new $name();
141✔
112
        return $_classes[$class];
141✔
113
}
114

115
/**
116
 * Keeps track of which libraries have been loaded.
117
 *
118
 * @staticvar array $_is_loaded
119
 *
120
 * @param string $class
121
 * @param bool   $reset
122
 *
123
 * @return array
124
 */
125
function &is_loaded($class = '', $reset = FALSE)
160✔
126
{
127
        static $_is_loaded = array();
160✔
128

129
        if ($reset)
130
        {
131
                $_is_loaded = array();
160✔
132
                return $_is_loaded;
160✔
133
        }
134

135
        if ($class !== '')
160✔
136
        {
137
                $_is_loaded[strtolower($class)] = $class;
160✔
138
        }
139

140
        return $_is_loaded;
×
141
}
142

143
function is_cli($return = null)
144
{
145
        static $_return = TRUE;
×
146

147
        if ($return !== null)
×
148
        {
149
                $_return = $return;
150
        }
151

152
        return $_return;
153
}
154

155
function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
156
{
157
        $status_code = abs($status_code);
161✔
158
        if ($status_code < 100)
161✔
159
        {
160
                $exit_status = $status_code + 9; // 9 is EXIT__AUTO_MIN
160✔
161

162
                $status_code = 500;
161✔
163
        }
164
        else
165
        {
166
                $exit_status = 1; // EXIT_ERROR
167
        }
168

169
        while (ob_get_level() > 1)
170
        {
171
                ob_end_clean();
172
        }
173

174
        throw new CIPHPUnitTestShowErrorException($message, $status_code);
175
}
176

177
function show_404($page = '', $log_error = TRUE)
178
{
179
        while (ob_get_level() > 1)
180
        {
181
                ob_end_clean();
182
        }
183

184
        throw new CIPHPUnitTestShow404Exception($page, 404);
185
}
186

187
/**
188
 * Error Logging Interface
189
 *
190
 * We use this as a simple mechanism to access the logging
191
 * class and send messages to be logged.
192
 *
193
 * @param        string        the error level: 'error', 'debug' or 'info'
194
 * @param        string        the error message
195
 * @return        void
196
 */
197
function log_message($level, $message)
198
{
199
        static $_log;
200

201
        if ($_log === NULL)
202
        {
203
                // references cannot be directly assigned to static variables, so we use an array
204
                $_log[0] =& load_class('Log', 'core');
205
        }
206

207
        $_log[0]->write_log($level, $message);
208

209
        // added by ci-phpunit-test
210
        CIPHPUnitTestLogger::log($level, $message);
211
}
212

213
function set_status_header($code = 200, $text = '')
214
{
215
//        if (is_cli())
216
//        {
217
//                return;
218
//        }
219

220
        if (empty($code) OR ! is_numeric($code))
221
        {
222
                show_error('Status codes must be numeric', 500);
223
        }
224

225
        if (empty($text))
226
        {
227
                is_int($code) OR $code = (int) $code;
228
                $stati = array(
229
                        100        => 'Continue',
230
                        101        => 'Switching Protocols',
231

232
                        200        => 'OK',
233
                        201        => 'Created',
234
                        202        => 'Accepted',
235
                        203        => 'Non-Authoritative Information',
236
                        204        => 'No Content',
237
                        205        => 'Reset Content',
238
                        206        => 'Partial Content',
239

240
                        300        => 'Multiple Choices',
241
                        301        => 'Moved Permanently',
242
                        302        => 'Found',
243
                        303        => 'See Other',
244
                        304        => 'Not Modified',
245
                        305        => 'Use Proxy',
246
                        307        => 'Temporary Redirect',
247

248
                        400        => 'Bad Request',
249
                        401        => 'Unauthorized',
250
                        402        => 'Payment Required',
251
                        403        => 'Forbidden',
252
                        404        => 'Not Found',
253
                        405        => 'Method Not Allowed',
254
                        406        => 'Not Acceptable',
255
                        407        => 'Proxy Authentication Required',
256
                        408        => 'Request Timeout',
257
                        409        => 'Conflict',
258
                        410        => 'Gone',
259
                        411        => 'Length Required',
260
                        412        => 'Precondition Failed',
261
                        413        => 'Request Entity Too Large',
262
                        414        => 'Request-URI Too Long',
263
                        415        => 'Unsupported Media Type',
264
                        416        => 'Requested Range Not Satisfiable',
265
                        417        => 'Expectation Failed',
266
                        422        => 'Unprocessable Entity',
267
                        426        => 'Upgrade Required',
268
                        428        => 'Precondition Required',
269
                        429        => 'Too Many Requests',
270
                        431        => 'Request Header Fields Too Large',
271

272
                        500        => 'Internal Server Error',
273
                        501        => 'Not Implemented',
274
                        502        => 'Bad Gateway',
275
                        503        => 'Service Unavailable',
276
                        504        => 'Gateway Timeout',
277
                        505        => 'HTTP Version Not Supported',
278
                        511        => 'Network Authentication Required',
279

280
                );
281

282
                if (isset($stati[$code]))
283
                {
284
                        $text = $stati[$code];
285
                }
286
                else
287
                {
288
                        show_error('No status text available. Please check your status code number or supply your own message text.', 500);
289
                }
290
        }
291

292
        // Save status code in Output object
293
        // added by ci-phpunit-test
294
        $CI =& get_instance();
295
        $output = $CI->output;
296
        $output->_status = [
297
                'code'     => $code,
298
                'text'     => $text,
299
                'redirect' => null,
300
        ];
301

302
        // Everything is done, so return
303
        // added by ci-phpunit-test
304
        if (TestCase::isTestingEnv())
305
        {
306
                return;
307
        }
308

309
        if (strpos(PHP_SAPI, 'cgi') === 0)
310
        {
311
                header('Status: '.$code.' '.$text, TRUE);
312
                return;
313
        }
314

315
        $server_protocol = (isset($_SERVER['SERVER_PROTOCOL']) && in_array($_SERVER['SERVER_PROTOCOL'], array('HTTP/1.0', 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0'), TRUE))
316
                ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
317
        header($server_protocol.' '.$code.' '.$text, TRUE, $code);
318
}
319

320
/**
321
 * Loads the main config.php file
322
 *
323
 * @staticvar array $config
324
 *
325
 * @param array $replace
326
 * @param bool  $reset
327
 *
328
 * @return array
329
 */
330
function &get_config(Array $replace = array(), $reset = FALSE)
331
{
332
        static $config;
333

334
        // Reset static variable
335
        // added by ci-phpunit-test
336
        if ($reset)
337
        {
338
                $config = null;
339
                get_config();
340
                return $config;
341
        }
342

343
        if (empty($config))
344
        {
345
                $file_path = APPPATH.'config/config.php';
346
                $found = FALSE;
347
                if (file_exists($file_path))
348
                {
349
                        $found = TRUE;
350
                        require($file_path);
351
                }
352

353
                // Is the config file in the environment folder?
354
                if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
355
                {
356
                        require($file_path);
357
                }
358
                elseif ( ! $found)
359
                {
360
                        set_status_header(503);
361
                        echo 'The configuration file does not exist.';
362
                        exit(3); // EXIT_CONFIG
363
                }
364

365
                // Does the $config array exist in the file?
366
                if ( ! isset($config) OR ! is_array($config))
367
                {
368
                        set_status_header(503);
369
                        echo 'Your config file does not appear to be formatted correctly.';
370
                        exit(3); // EXIT_CONFIG
371
                }
372
        }
373

374
        // Are any values being dynamically added or replaced?
375
        foreach ($replace as $key => $val)
376
        {
377
                $config[$key] = $val;
378
        }
379

380
        return $config;
381
}
382

383
/**
384
 * Returns the specified config item
385
 *
386
 * @staticvar array $_config
387
 *
388
 * @param string $item
389
 * @param bool   $reset
390
 *
391
 * @return type
392
 */
393
function config_item($item, $reset = FALSE)
394
{
395
        static $_config;
396

397
        // Reset static variable
398
        // added by ci-phpunit-test
399
        if ($reset)
400
        {
401
                $config = null;
402
                return;
403
        }
404

405
        if (empty($_config))
406
        {
407
                // references cannot be directly assigned to static variables, so we use an array
408
                $_config[0] =& get_config();
409
        }
410

411
        return isset($_config[0][$item]) ? $_config[0][$item] : NULL;
412
}
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

© 2026 Coveralls, Inc