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