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 FrameworkException)) {
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($exceptionInstance->__toString()))),
80                         trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
81                         $exceptionInstance->getHexCode(),
82                         $exceptionInstance->getFile(),
83                         $exceptionInstance->getLine(),
84                         trim($backTrace)
85                 );
86
87                 // Output the message
88                 print($message);
89         } else {
90                 // Invalid exception instance detected! Do *only* throw exceptions that
91                 // extends our own exception 'FrameworkException' to get such nice
92                 // outputs like above.
93                 print('exceptionInstance[]=' . gettype($exceptionInstance) . ' is invalid! Please inform the core developer team.');
94         }
95 }
96
97 // Error handler
98 function __errorHandler ($errno, $errstr, $errfile, $errline, array $errcontext) {
99         // Construct the message
100         $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
101                 basename($errfile),
102                 $errline,
103                 $errno,
104                 $errstr
105         );
106
107         // Throw an exception here
108         throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
109 } // END - function
110
111 // Assertion handler
112 function __assertHandler ($file, $line, $code) {
113         // Empty code?
114         if ($code === '') {
115                 $code = '<em>Unknown</em>';
116         } // END - if
117
118         // Create message
119         $message = sprintf('File: %s, Line: %s, Code: %s',
120                 basename($file),
121                 $line,
122                 $code
123         );
124
125         // Log assert
126         syslog(LOG_WARNING, $message);
127
128         // Throw an exception here
129         throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
130 } // END - function
131
132 // Set error handler
133 //set_error_handler('__errorHandler');
134
135 // Set the new handler
136 set_exception_handler('tests_exception_handler');
137
138 // Init assert handling
139 assert_options(ASSERT_ACTIVE    , TRUE);
140 assert_options(ASSERT_WARNING   , FALSE);
141 assert_options(ASSERT_BAIL      , TRUE);
142 assert_options(ASSERT_QUIET_EVAL, FALSE);
143 assert_options(ASSERT_CALLBACK  , '__assertHandler');