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 - 2020 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 // Convert arguments type into human-readable
42 foreach ($traceArray['args'] as $arg) {
43 $argsString .= ', ' . gettype($arg);
45 $argsString = substr($argsString, 2);
47 // Set missing file/line
48 if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown';
49 if (!isset($traceArray['line'])) $traceArray['line'] = '0';
50 if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
51 if (!isset($traceArray['type'])) $traceArray['type'] = '->';
53 $backTrace .= sprintf("---------- Pos %d: ----------
61 $traceArray['function'],
63 basename($traceArray['file']),
68 // Construct the message
69 $message = sprintf("--------------------------------------------------------------------------------
70 Uncaught Exception : %s
71 --------------------------------------------------------------------------------
76 --------------------------------------------------------------------------------
78 --------------------------------------------------------------------------------
80 --------------------------------------------------------------------------------\n",
81 trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
82 trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
83 ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
84 $exceptionInstance->getFile(),
85 $exceptionInstance->getLine(),
91 } elseif (is_object($exceptionInstance)) {
92 // Output more details
93 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
96 * Invalid exception instance detected! Do *only* throw exceptions that
97 * extends our own exception 'FrameworkException' to get such nice
100 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
105 function tests_error_handler ($errno, $errstr, $errfile, $errline, array $errcontext) {
106 // Construct the message
107 $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
114 // Throw an exception here
115 throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
119 function tests_assert_handler ($file, $line, $code) {
122 $code = '<em>Unknown</em>';
126 $message = sprintf('File: %s, Line: %s, Code: %s',
133 syslog(LOG_WARNING, $message);
135 // Throw an exception here
136 throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
140 //set_error_handler('tests_error_handler');
142 // Set the new handler
143 set_exception_handler('tests_exception_handler');
145 // Init assert handling
146 assert_options(ASSERT_ACTIVE , true);
147 assert_options(ASSERT_WARNING , false);
148 assert_options(ASSERT_BAIL , true);
149 assert_options(ASSERT_QUIET_EVAL, false);
150 assert_options(ASSERT_CALLBACK , 'tests_assert_handler');