2 // Import framework stuff
3 use CoreFramework\Generic\FrameworkException;
4 use CoreFramework\Object\BaseFrameworkSystem;
7 * An include file for setting up the exception handler of test suite
9 * @author Roland Haeder <webmaster@shipsimu.org>
11 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
12 * @license GNU GPL 3.0 or any newer version
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 // The node's own exception handler
29 function tests_exception_handler ($exceptionInstance) {
30 // Is it an object and a valid instance?
31 if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
35 // Get all call levels from backtrace
36 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
37 // Init argument string
40 // Convert arguments type into human-readable
41 foreach ($traceArray['args'] as $arg) {
42 $argsString .= ', ' . gettype($arg);
44 $argsString = substr($argsString, 2);
46 // Set missing file/line
47 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
48 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
49 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
50 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
52 $backTrace .= sprintf("---------- Pos %d: ----------
60 $traceArray['function'],
62 basename($traceArray['file']),
67 // Construct the message
68 $message = sprintf("--------------------------------------------------------------------------------
69 Uncaught Exception : %s
70 --------------------------------------------------------------------------------
75 --------------------------------------------------------------------------------
77 --------------------------------------------------------------------------------
79 --------------------------------------------------------------------------------\n",
80 trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
81 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
82 ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
83 $exceptionInstance->getFile(),
84 $exceptionInstance->getLine(),
90 } elseif (is_object($exceptionInstance)) {
91 // Output more details
92 printf('exceptionInstance=%s', print_r($exceptionInstance, TRUE));
95 * Invalid exception instance detected! Do *only* throw exceptions that
96 * extends our own exception 'FrameworkException' to get such nice
99 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
104 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
105 // Construct the message
106 $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
113 // Throw an exception here
114 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
118 function __assertHandler ($file, $line, $code) {
121 $code = '<em>Unknown</em>';
125 $message = sprintf('File: %s, Line: %s, Code: %s',
132 syslog(LOG_WARNING, $message);
134 // Throw an exception here
135 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
139 //set_error_handler('__errorHandler');
141 // Set the new handler
142 set_exception_handler('tests_exception_handler');
144 // Init assert handling
145 assert_options(ASSERT_ACTIVE , TRUE);
146 assert_options(ASSERT_WARNING , FALSE);
147 assert_options(ASSERT_BAIL , TRUE);
148 assert_options(ASSERT_QUIET_EVAL, FALSE);
149 assert_options(ASSERT_CALLBACK , '__assertHandler');