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