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