Browse Source

Started testing ThrowableController

2.1.0
Dustin Wilson 1 year ago
parent
commit
3b3cf812d4
  1. 18
      lib/Catcher/ThrowableController.php
  2. 2
      run
  3. 113
      tests/cases/TestThrowableController.php
  4. 1
      tests/phpunit.dist.xml

18
lib/Catcher/ThrowableController.php

@ -102,7 +102,7 @@ class ThrowableController {
) {
$frames = $this->throwable->getTrace();
} else {
$frames = array_diff_key(array_reverse(xdebug_get_function_stack()), debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$frames = array_values(array_diff_key(xdebug_get_function_stack(), debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS)));
}
// PHP for some stupid reason thinks it's okay not to provide line numbers and file
@ -133,11 +133,17 @@ class ThrowableController {
}
// Delete everything that has anything to do with userland error handling
for ($frameCount = count($frames), $i = $frameCount - 1; $i >= 0; $i--) {
$frame = $frames[$i];
if ($frame['file'] === $this->throwable->getFile() && $frame['line'] === $this->throwable->getLine()) {
array_splice($frames, 0, $i);
break;
$frameCount = count($frames);
if ($frameCount > 0) {
$tFile = $this->throwable->getFile();
$tLine = $this->throwable->getLine();
for ($i = $frameCount - 1; $i >= 0; $i--) {
$frame = $frames[$i];
if ($tFile === $frame['file'] && $tLine === $frame['line']) {
array_splice($frames, 0, $i);
break;
}
}
}

2
run

@ -92,7 +92,7 @@ if (!extension_loaded('xdebug')) {
error('Xdebug is not installed on your system; aborting');
}
}
$cmd[] = '-d xdebug.mode=coverage,trace';
$cmd[] = '-d xdebug.mode=coverage,develop,trace';
$cmd = implode(' ', $cmd);
$process = proc_open("$cmd $phpunitPath -c $confPath $opts", [

113
tests/cases/TestThrowableController.php

@ -0,0 +1,113 @@
<?php
/**
* @license MIT
* Copyright 2022 Dustin Wilson, et al.
* See LICENSE and AUTHORS files for details
*/
declare(strict_types=1);
namespace MensBeam\Foundation\Catcher\Test;
use MensBeam\Foundation\Catcher;
use MensBeam\Foundation\Catcher\{
Error,
PlainTextHandler,
ThrowableController
};
class TestThrowableController extends \PHPUnit\Framework\TestCase {
/**
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getErrorType
*
* @covers \MensBeam\Foundation\Catcher::__construct
* @covers \MensBeam\Foundation\Catcher::getLastThrowable
* @covers \MensBeam\Foundation\Catcher::handleError
* @covers \MensBeam\Foundation\Catcher::handleThrowable
* @covers \MensBeam\Foundation\Catcher::pushHandler
* @covers \MensBeam\Foundation\Catcher::register
* @covers \MensBeam\Foundation\Catcher::unregister
* @covers \MensBeam\Foundation\Catcher\Error::__construct
* @covers \MensBeam\Foundation\Catcher\Handler::__construct
* @covers \MensBeam\Foundation\Catcher\Handler::dispatch
* @covers \MensBeam\Foundation\Catcher\Handler::getControlCode
* @covers \MensBeam\Foundation\Catcher\Handler::getOutputCode
* @covers \MensBeam\Foundation\Catcher\Handler::handle
* @covers \MensBeam\Foundation\Catcher\Handler::print
* @covers \MensBeam\Foundation\Catcher\HandlerOutput::__construct
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::dispatchCallback
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::handleCallback
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::serializeThrowable
* @covers \MensBeam\Foundation\Catcher\ThrowableController::__construct
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getPrevious
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getThrowable
*/
public function testMethod_getErrorType(): void {
$c = new Catcher(new PlainTextHandler([ 'outputToStderr' => false ]));
ob_start();
trigger_error('Ook!', \E_USER_DEPRECATED);
ob_end_clean();
$this->assertEquals(\E_USER_DEPRECATED, $c->getLastThrowable()->getCode());
$c->unregister();
$c = new Catcher(new PlainTextHandler([ 'outputToStderr' => false ]));
ob_start();
trigger_error('Ook!', \E_USER_WARNING);
ob_end_clean();
$this->assertEquals(\E_USER_WARNING, $c->getLastThrowable()->getCode());
$c->unregister();
// These others will be tested by invoking the method directly
$c = new ThrowableController(new Error('Ook!', \E_ERROR));
$this->assertEquals('PHP Fatal Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_WARNING));
$this->assertEquals('PHP Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_PARSE));
$this->assertEquals('PHP Parsing Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_NOTICE));
$this->assertEquals('PHP Notice', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_DEPRECATED));
$this->assertEquals('Deprecated', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_CORE_ERROR));
$this->assertEquals('PHP Core Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_CORE_WARNING));
$this->assertEquals('PHP Core Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_COMPILE_ERROR));
$this->assertEquals('Compile Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_COMPILE_WARNING));
$this->assertEquals('Compile Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_STRICT));
$this->assertEquals('Runtime Notice', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_RECOVERABLE_ERROR));
$this->assertEquals('Recoverable Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!'));
$this->assertNull($c->getErrorType());
$c = new ThrowableController(new \Exception('Ook!'));
$this->assertNull($c->getErrorType());
}
/**
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getFrames
*
*/
public function testMethod_getFrames(): void {
$f = false;
try {
throw new \Exception('Ook!');
} catch (\Throwable $t) {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals('Exception', $f[0]['class']);
}
$f = false;
try {
throw new Error('Ook!', \E_ERROR);
} catch (\Throwable $t) {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(Error::class, $f[0]['class']);
}
}
}

1
tests/phpunit.dist.xml

@ -19,6 +19,7 @@
<testsuite name="Main">
<file>cases/TestCatcher.php</file>
<file>cases/TestHandler.php</file>
<file>cases/TestThrowableController.php</file>
</testsuite>
</testsuites>
</phpunit>
Loading…
Cancel
Save