2 // Import framework stuff
3 use Org\Mxchange\CoreFramework\Assertion\AssertionException;
4 use Org\Mxchange\CoreFramework\Generic\FrameworkException;
5 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 * An include file for setting up the exception handler of test suite
10 * @author Roland Haeder <webmaster@shipsimu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
13 * @license GNU GPL 3.0 or any newer version
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 // The node's own exception handler
30 function tests_exception_handler ($exceptionInstance) {
31 // Is it an object and a valid instance?
32 if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
36 // Get all call levels from backtrace
37 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
38 // Init argument string
41 // Some argument found?
42 if (isset($traceArray['args'])) {
43 // Convert arguments type into human-readable
44 foreach ($traceArray['args'] as $arg) {
45 $argsString .= ', ' . gettype($arg);
47 $argsString = substr($argsString, 2);
50 // Set missing file/line
51 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
52 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
53 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
54 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
56 $backTrace .= sprintf("---------- Pos %d: ----------
64 $traceArray['function'],
66 basename($traceArray['file']),
71 // Construct the message
72 $message = sprintf("--------------------------------------------------------------------------------
73 Uncaught Exception : %s
74 --------------------------------------------------------------------------------
79 --------------------------------------------------------------------------------
81 --------------------------------------------------------------------------------
83 --------------------------------------------------------------------------------\n",
84 trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
85 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
86 ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
87 $exceptionInstance->getFile(),
88 $exceptionInstance->getLine(),
94 } elseif (is_object($exceptionInstance)) {
95 // Output more details
96 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
99 * Invalid exception instance detected! Do *only* throw exceptions that
100 * extends our own exception 'FrameworkException' to get such nice
101 * outputs like above.
103 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
108 function tests_error_handler ($errno, $errstr, $errfile, $errline, array $errcontext) {
109 // Construct the message
110 $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
117 // Throw an exception here
118 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
122 function tests_assert_handler ($file, $line, $code) {
125 $code = '<em>Unknown</em>';
129 $message = sprintf('File: %s, Line: %s, Code: %s',
136 syslog(LOG_WARNING, $message);
138 // Throw an exception here
139 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
143 //set_error_handler('tests_error_handler');
145 // Set the new handler
146 set_exception_handler('tests_exception_handler');
148 // Init assert handling
149 assert_options(ASSERT_ACTIVE , true);
150 assert_options(ASSERT_WARNING , false);
151 assert_options(ASSERT_BAIL , true);
152 assert_options(ASSERT_QUIET_EVAL, false);
153 assert_options(ASSERT_CALLBACK , 'tests_assert_handler');