update copyright as changes will happen this year
[core.git] / application / tests / exceptions.php
1 <?php
2 /**
3  * An include file for setting up the exception handler of test suite
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 // The node's own exception handler
25 function tests_exception_handler ($exceptionInstance) {
26         // Is it an object and a valid instance?
27         if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof FrameworkException)) {
28                 // Init variable
29                 $backTrace = '';
30
31                 // Get all call levels from backtrace
32                 foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
33                         // Init argument string
34                         $argsString = '';
35
36                         // Convert arguments type into human-readable
37                         foreach ($traceArray['args'] as $arg) {
38                                 $argsString .= ', ' . gettype($arg);
39                         } // END - foreach
40                         $argsString = substr($argsString, 2);
41
42                         // Set missing file/line
43                         if (!isset($traceArray['file']))  $traceArray['file']  = 'unknown';
44                         if (!isset($traceArray['line']))  $traceArray['line']  = '0';
45                         if (!isset($traceArray['class'])) $traceArray['class'] = 'UnknownObject';
46                         if (!isset($traceArray['type']))  $traceArray['type']  = '->';
47
48                         $backTrace .= sprintf("---------- Pos %d: ----------
49 Method : %s%s%s(%s)
50 ----- Caller: -----
51 File   : %s
52 Line   : %d\n",
53                                 ($idx + 1),
54                                 $traceArray['class'],
55                                 $traceArray['type'],
56                                 $traceArray['function'],
57                                 $argsString,
58                                 basename($traceArray['file']),
59                                 $traceArray['line']
60                         );
61                 } // END - foreach
62
63                 // Construct the message
64                 $message = sprintf("--------------------------------------------------------------------------------
65 Uncaught Exception : %s
66 --------------------------------------------------------------------------------
67 Message            : %s
68 Code               : %s
69 File               : %s
70 Line               : %d
71 --------------------------------------------------------------------------------
72 Backtrace:
73 --------------------------------------------------------------------------------
74 %s
75 --------------------------------------------------------------------------------\n",
76                         trim(html_entity_decode(strip_tags($exceptionInstance->__toString()))),
77                         trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
78                         $exceptionInstance->getHexCode(),
79                         $exceptionInstance->getFile(),
80                         $exceptionInstance->getLine(),
81                         trim($backTrace)
82                 );
83
84                 // Output the message
85                 print($message);
86         } else {
87                 // Invalid exception instance detected! Do *only* throw exceptions that
88                 // extends our own exception 'FrameworkException' to get such nice
89                 // outputs like above.
90                 print('exceptionInstance[]=' . gettype($exceptionInstance) . ' is invalid! Please inform the core developer team.');
91         }
92 }
93
94 // Error handler
95 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
96         // Construct the message
97         $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
98                 basename($errfile),
99                 $errline,
100                 $errno,
101                 $errstr
102         );
103
104         // Throw an exception here
105         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
106 } // END - function
107
108 // Assertion handler
109 function __assertHandler ($file, $line, $code) {
110         // Empty code?
111         if ($code === '') {
112                 $code = '<em>Unknown</em>';
113         } // END - if
114
115         // Create message
116         $message = sprintf('File: %s, Line: %s, Code: %s',
117                 basename($file),
118                 $line,
119                 $code
120         );
121
122         // Log assert
123         syslog(LOG_WARNING, $message);
124
125         // Throw an exception here
126         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
127 } // END - function
128
129 // Set error handler
130 //set_error_handler('__errorHandler');
131
132 // Set the new handler
133 set_exception_handler('tests_exception_handler');
134
135 // Init assert handling
136 assert_options(ASSERT_ACTIVE    , TRUE);
137 assert_options(ASSERT_WARNING   , FALSE);
138 assert_options(ASSERT_BAIL      , TRUE);
139 assert_options(ASSERT_QUIET_EVAL, FALSE);
140 assert_options(ASSERT_CALLBACK  , '__assertHandler');
141
142 // [EOF]
143 ?>