Continued:
[core.git] / application / tests / exceptions.php
index ed824148ec45a4bd92b721e3b9400718d7f113fe..06acedd47625f1bc63e4e6c2e713d68e6cb4660b 100644 (file)
@@ -1,14 +1,16 @@
 <?php
 // Import framework stuff
-use CoreFramework\Generic\FrameworkException;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Assertion\AssertionException;
+use Org\Mxchange\CoreFramework\Error\FatalErrorException;
+use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
 /**
  * An include file for setting up the exception handler of test suite
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  *
  * This program is free software: you can redistribute it and/or modify
@@ -26,22 +28,28 @@ use CoreFramework\Object\BaseFrameworkSystem;
  */
 
 // The node's own exception handler
-function tests_exception_handler ($exceptionInstance) {
+function core_exception_handler ($exceptionInstance) {
        // Is it an object and a valid instance?
        if ((is_object($exceptionInstance)) && ($exceptionInstance instanceof Exception)) {
                // Init variable
                $backTrace = '';
 
+               // Generate exception code
+               $exceptionCode = ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode()));
+
                // Get all call levels from backtrace
                foreach ($exceptionInstance->getTrace() as $idx => $traceArray) {
                        // Init argument string
                        $argsString = '';
 
-                       // Convert arguments type into human-readable
-                       foreach ($traceArray['args'] as $arg) {
-                               $argsString .= ', ' . gettype($arg);
-                       } // END - foreach
-                       $argsString = substr($argsString, 2);
+                       // Arguments given?
+                       if (isset($traceArray['args'])) {
+                               // Convert arguments type into human-readable
+                               foreach ($traceArray['args'] as $arg) {
+                                       $argsString .= ', ' . gettype($arg);
+                               }
+                               $argsString = substr($argsString, 2);
+                       }
 
                        // Set missing file/line
                        if (!isset($traceArray['file']))  $traceArray['file']  = 'unknown';
@@ -62,7 +70,7 @@ Line   : %d\n",
                                basename($traceArray['file']),
                                $traceArray['line']
                        );
-               } // END - foreach
+               }
 
                // Construct the message
                $message = sprintf("--------------------------------------------------------------------------------
@@ -79,7 +87,7 @@ Backtrace:
 --------------------------------------------------------------------------------\n",
                        trim(html_entity_decode(strip_tags(get_class($exceptionInstance)))),
                        trim(html_entity_decode(strip_tags($exceptionInstance->getMessage()))),
-                       ($exceptionInstance instanceof FrameworkException ? $exceptionInstance->getHexCode() : '0x' . bin2hex($exceptionInstance->getCode())),
+                       $exceptionCode,
                        $exceptionInstance->getFile(),
                        $exceptionInstance->getLine(),
                        trim($backTrace)
@@ -87,9 +95,13 @@ Backtrace:
 
                // Output the message
                print($message);
+
+               // Exit with code
+               exit(hexdec($exceptionCode));
        } elseif (is_object($exceptionInstance)) {
                // Output more details
                printf('exceptionInstance=%s', print_r($exceptionInstance, true));
+               exit(255);
        } else {
                /*
                 * Invalid exception instance detected! Do *only* throw exceptions that
@@ -97,13 +109,14 @@ Backtrace:
                 * outputs like above.
                 */
                printf('exceptionInstance[]=%s is invalid! Please inform the core developer team.' . PHP_EOL, gettype($exceptionInstance));
+               exit(255);
        }
 }
 
 // Error handler
-function test_error_handler ($errno, $errstr, $errfile, $errline, array $errcontext) {
+function core_error_handler (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
        // Construct the message
-       $message = sprintf('File: %s, Line: %s, Code: %s, Message: %s',
+       $message = sprintf('File: %s, Line: %d, Code: %d, Message: %s',
                basename($errfile),
                $errline,
                $errno,
@@ -112,14 +125,14 @@ function test_error_handler ($errno, $errstr, $errfile, $errline, array $errcont
 
        // Throw an exception here
        throw new FatalErrorException($message, BaseFrameworkSystem::EXCEPTION_FATAL_ERROR);
-} // END - function
+}
 
 // Assertion handler
-function test_assert_handler ($file, $line, $code) {
+function core_assert_handler (string $file, int $line, int $code) {
        // Empty code?
-       if ($code === '') {
+       if (empty($code)) {
                $code = '<em>Unknown</em>';
-       } // END - if
+       }
 
        // Create message
        $message = sprintf('File: %s, Line: %s, Code: %s',
@@ -133,17 +146,17 @@ function test_assert_handler ($file, $line, $code) {
 
        // Throw an exception here
        throw new AssertionException($message, BaseFrameworkSystem::EXCEPTION_ASSERTION_FAILED);
-} // END - function
+}
 
 // Set error handler
-//set_error_handler('test_error_handler');
+//set_error_handler('core_error_handler');
 
 // Set the new handler
-set_exception_handler('tests_exception_handler');
+set_exception_handler('core_exception_handler');
 
 // Init assert handling
 assert_options(ASSERT_ACTIVE    , true);
-assert_options(ASSERT_WARNING   , false);
+assert_options(ASSERT_WARNING   , true);
 assert_options(ASSERT_BAIL      , true);
 assert_options(ASSERT_QUIET_EVAL, false);
-assert_options(ASSERT_CALLBACK  , 'test_assert_handler');
+assert_options(ASSERT_CALLBACK  , 'core_assert_handler');