Skip to content

Commit

Permalink
Added More Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 1, 2024
1 parent 83fd462 commit 6a11847
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 23 deletions.
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<file>./src/webfiori/error/TraceEntry.php</file>
<file>./src/webfiori/error/Handler.php</file>
<file>./src/webfiori/error/AbstractHandler.php</file>
</whitelist>
</filter>
<logging>
Expand Down
44 changes: 41 additions & 3 deletions tests/webfiori/tests/error/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace webfiori\tests\error;

namespace webfiori\tests\error;
require_once 'SampleHandler1.php';
require_once 'SampleHandler2.php';

use PHPUnit\Framework\TestCase;
use webfiori\error\TraceEntry;
use webfiori\error\ErrorHandlerException;
use webfiori\error\Handler;
/**
* Description of HandlerTest
Expand All @@ -17,7 +18,44 @@ class HandlerTest extends TestCase {
* @test
*/
public function test00() {
$this->expectException(ErrorHandlerException::class);
$this->expectExceptionMessage('An exception caused by an error. Run-time warning: Undefined variable $y at HandlerTest Line 24');
$h = Handler::get();
$x = $y;
}
/**
* @test
*/
public function test01() {
$h = Handler::get();
$this->assertFalse($h->hasHandler('New Handler'));
$h->registerHandler(new SampleHandler1());
$this->assertTrue($h->hasHandler('New Handler'));
$h->unregisterHandler($h->getHandler('New Handler'));
$this->assertFalse($h->hasHandler('New Handler'));
}
/**
* @test
*/
public function test02() {
$h = Handler::get();
$h->reset();
$h->registerHandler(new SampleHandler1());
$this->assertFalse(defined('SampleHandler1'));
$h->invokExceptionHandler();
$this->assertTrue(defined('SampleHandler1'));
}
/**
* @test
*/
public function test03() {
$h = Handler::get();
$this->assertTrue(true);
$h->reset();
$h->registerHandler(new SampleHandler2());
$this->assertFalse(defined('SampleHandler2'));
$h->invokExceptionHandler();
$this->assertFalse(defined('SampleHandler2'));
$h->invokShutdownHandler();
$this->assertTrue(defined('SampleHandler2'));
}
}
23 changes: 23 additions & 0 deletions tests/webfiori/tests/error/SampleHandler1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace webfiori\tests\error;

use webfiori\error\AbstractHandler;

class SampleHandler1 extends AbstractHandler {
public function __construct() {
parent::__construct();
}
public function handle() {
define('SampleHandler1', 'Yes');
}

public function isActive(): bool {
return true;
}

public function isShutdownHandler(): bool {
return false;
}
}
23 changes: 23 additions & 0 deletions tests/webfiori/tests/error/SampleHandler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace webfiori\tests\error;

use webfiori\error\AbstractHandler;

class SampleHandler2 extends AbstractHandler {
public function __construct() {
parent::__construct();
}
public function handle() {
define('SampleHandler2', 'Yes');
}

public function isActive(): bool {
return true;
}

public function isShutdownHandler(): bool {
return true;
}
}
13 changes: 8 additions & 5 deletions webfiori/error/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct() {
* was thrown at.
*/
public function getClass() : string {
return TraceEntry::extractClassName($this->getException()->getFile());
return TraceEntry::extractClassName($this->getException() !== null ? $this->getException()->getFile() : 'X');
}
/**
* Returns exception error code.
Expand All @@ -43,9 +43,9 @@ public function getCode() : string {
/**
* Returns an object that represents the exception which was thrown.
*
* @return Throwable An object that represents the exception which was thrown.
* @return Throwable|null An object that represents the exception which was thrown.
*/
public function getException() : Throwable {
public function getException() {
return $this->exception;
}
/**
Expand All @@ -54,15 +54,15 @@ public function getException() : Throwable {
* @return string The number of line at which the exception was thrown at.
*/
public function getLine() : string {
return $this->getException()->getLine().'';
return $this->getException() !== null ? $this->getException()->getLine().'' : 'X';
}
/**
* Returns a string that represents exception message.
*
* @return string A string that represents exception message.
*/
public function getMessage() : string {
return $this->getException()->getMessage();
return $this->getException() !== null ? $this->getException()->getMessage() : 'No Message';
}
/**
* Returns the name of the handler.
Expand Down Expand Up @@ -116,6 +116,9 @@ public function isExecuting() : bool {
}
/**
* Checks if the handler will be called in case of error after shutdown.
*
* Note that if the handler is set as shutdown handler, it will not
* get executed during normal events.
*/
public abstract function isShutdownHandler() : bool;
/**
Expand Down
51 changes: 36 additions & 15 deletions webfiori/error/Handler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace webfiori\error;

use Exception;
use Throwable;
/**
* The core class which is used to define errors and exceptions handling.
Expand Down Expand Up @@ -92,34 +93,36 @@ class Handler {
* @var Throwable|null
*/
private $lastException;
private $errToExceptionHandler;
private $exceptionsHandler;
private $shutdownFunction;
private function __construct() {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//ini_set('display_errors', 1);
error_reporting(-1);
$this->isErrOccured = false;
set_error_handler(function (int $errno, string $errString, string $errFile, int $errLine)
{
$this->errToExceptionHandler = function (int $errno, string $errString, string $errFile, int $errLine) {
//Convert errors to exceptions
$errClass = TraceEntry::extractClassName($errFile);
$errType = Handler::ERR_TYPES[$errno];
$message = 'An exception caused by an error. '.$errType['description'].': '.$errString.' at '.$errClass.' Line '.$errLine;
throw new ErrorHandlerException($message, $errno, $errFile);
});
set_exception_handler(function (Throwable $ex)
{
$this->lastException = $ex;
};
$this->exceptionsHandler = function (Throwable $ex = null) {
Handler::get()->lastException = $ex;

foreach (Handler::get()->handlersPool as $h) {
if ($h->isActive()) {
$h->setException($ex);
if ($h->isActive() && !$h->isShutdownHandler()) {
if ($ex !== null) {
$h->setException($ex);
}
$h->setIsExecuting(true);
$h->handle();
$h->setIsExecuting(false);
$h->setIsExecuted(true);
}
}
});
register_shutdown_function(function ()
{
};
$this->shutdownFunction = function () {
if ($this->lastException !== null) {
if (ob_get_length()) {
ob_clean();
Expand All @@ -133,10 +136,28 @@ private function __construct() {
}
}
}
});
};
$this->isErrOccured = false;
set_exception_handler($this->exceptionsHandler);
set_error_handler($this->errToExceptionHandler);

register_shutdown_function($this->shutdownFunction);
$this->handlersPool = [];
$this->handlersPool[] = new DefaultHandler();
}
public function invokShutdownHandler() {
self::get()->lastException = new Exception();
call_user_func(self::get()->shutdownFunction);
}
public function invokExceptionHandler() {
call_user_func(self::get()->exceptionsHandler);
}
public static function reset() {
$h = self::get();
$h->handlersPool = [];
$h->handlersPool[] = new DefaultHandler();
set_error_handler($h->errToExceptionHandler);
}
/**
* Returns a handler given its name.
*
Expand Down

0 comments on commit 6a11847

Please sign in to comment.