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