a336bb98e68c8d0d22afeac9d5b8bc64fad01e81
[core.git] / application / tests / exceptions.php
1 <?php
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;
7
8 /**
9  * An include file for setting up the exception handler of test suite
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  *
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.
20  *
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.
25  *
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/>.
28  */
29
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)) {
34                 // Init variable
35                 $backTrace = '';
36
37                 // Get all call levels from backtrace
38                 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
39                         // Init argument string
40                         $argsString = '';
41
42                         // Convert arguments type into human-readable
43                         foreach ($traceArray['args'] as $arg) {
44                                 $argsString .= ', ' . gettype($arg);
45                         } // END - foreach
46                         $argsString = substr($argsString, 2);
47
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']  = '->';
53
54                         $backTrace .= sprintf("---------- Pos %d: ----------
55 Method : %s%s%s(%s)
56 ----- Caller: -----
57 File   : %s
58 Line   : %d\n",
59                                 ($idx + 1),
60                                 $traceArray['class'],
61                                 $traceArray['type'],
62                                 $traceArray['function'],
63                                 $argsString,
64                                 basename($traceArray['file']),
65                                 $traceArray['line']
66                         );
67                 } // END - foreach
68
69                 // Construct the message
70                 $message = sprintf("--------------------------------------------------------------------------------
71 Uncaught Exception : %s
72 --------------------------------------------------------------------------------
73 Message            : %s
74 Code               : %s
75 File               : %s
76 Line               : %d
77 --------------------------------------------------------------------------------
78 Backtrace:
79 --------------------------------------------------------------------------------
80 %s
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(),
87                         trim($backTrace)
88                 );
89
90                 // Output the message
91                 print($message);
92         } elseif (is_object($exceptionInstance)) {
93                 // Output more details
94                 printf('exceptionInstance=%s', print_r($exceptionInstance, true));
95         } else {
96                 /*
97                  * Invalid exception instance detected! Do *only* throw exceptions that
98                  * extends our own exception 'FrameworkException' to get such nice
99                  * outputs like above.
100                  */
101                 printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
102         }
103 }
104
105 // Error handler
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',
109                 basename($errfile),
110                 $errline,
111                 $errno,
112                 $errstr
113         );
114
115         // Throw an exception here
116         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
117 }
118
119 // Assertion handler
120 function tests_assert_handler (string $file, int $line, $code) {
121         // Empty code?
122         if ($code === '') {
123                 $code = '<em>Unknown</em>';
124         } // END - if
125
126         // Create message
127         $message = sprintf('File: %s, Line: %s, Code: %s',
128                 basename($file),
129                 $line,
130                 $code
131         );
132
133         // Log assert
134         syslog(LOG_WARNING, $message);
135
136         // Throw an exception here
137         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
138 }
139
140 // Set error handler
141 //set_error_handler('tests_error_handler');
142
143 // Set the new handler
144 set_exception_handler('tests_exception_handler');
145
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');