Browse Source

PlainTextHandler 100% Coverage

2.1.0
Dustin Wilson 1 year ago
parent
commit
b8243619d6
  1. 3
      composer.json
  2. 44
      composer.lock
  3. 4
      lib/Catcher/PlainTextHandler.php
  4. 18
      tests/cases/TestCatcher.php
  5. 6
      tests/cases/TestHandler.php
  6. 95
      tests/cases/TestPlainTextHandler.php
  7. 42
      tests/cases/TestThrowableController.php
  8. 1
      tests/phpunit.dist.xml

3
composer.json

@ -25,7 +25,6 @@
"mensbeam/html-dom": "^1.0",
"phpunit/phpunit": "^9.5",
"nikic/php-parser": "^4.15",
"eloquent/phony-phpunit": "^7.1",
"giberti/phpunit-local-server": "^3.0"
"eloquent/phony-phpunit": "^7.1"
}
}

44
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "426fded11213b92f432b1083735aeae4",
"content-hash": "276b1ae35ddd23e0ebc736f079ecf98f",
"packages": [
{
"name": "psr/log",
@ -275,48 +275,6 @@
},
"time": "2020-12-21T09:36:47+00:00"
},
{
"name": "giberti/phpunit-local-server",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/giberti/phpunit-local-server.git",
"reference": "61d07cb083fe5a354d95d84f357c63ba30d74862"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/giberti/phpunit-local-server/zipball/61d07cb083fe5a354d95d84f357c63ba30d74862",
"reference": "61d07cb083fe5a354d95d84f357c63ba30d74862",
"shasum": ""
},
"require": {
"ext-posix": "*",
"php": "^7.3 || ^8.0",
"phpunit/phpunit": "^8.0 || ^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Giberti\\PHPUnitLocalServer\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Erik Giberti",
"homepage": "https://github.com/giberti"
}
],
"description": "A local HTTP server for PHPUnit tests",
"support": {
"issues": "https://github.com/giberti/phpunit-local-server/issues",
"source": "https://github.com/giberti/phpunit-local-server/tree/v3.0.0"
},
"time": "2022-05-01T01:31:15+00:00"
},
{
"name": "mensbeam/framework",
"version": "1.0.5",

4
lib/Catcher/PlainTextHandler.php

@ -55,7 +55,7 @@ class PlainTextHandler extends Handler {
$args = '';
if (!empty($frame['args']) && $this->_backtraceArgFrameLimit >= $num) {
$args = "\n" . preg_replace('/^/m', str_repeat(' ', $maxDigits) . '| ', var_export($frame['args'], true));
$args = "\n" . preg_replace('/^/m', str_repeat(' ', $maxDigits) . '| ', print_r($frame['args'], true));
}
$template = "\n%{$maxDigits}d. %s";
@ -79,7 +79,7 @@ class PlainTextHandler extends Handler {
// The logger will handle timestamps itself.
if ($this->_logger !== null) {
$this->log($controller->getThrowable(), $message);
$this->log($controller->getThrowable(), $output);
}
if (!$this->_silent && $this->_outputTime && $this->_timeFormat !== '') {

18
tests/cases/TestCatcher.php

@ -31,7 +31,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
public function testMethod___construct(): void {
$c = new Catcher();
$this->assertSame('MensBeam\Foundation\Catcher', $c::class);
$this->assertEquals(1, count($c->getHandlers()));
$this->assertSame(1, count($c->getHandlers()));
$this->assertSame(PlainTextHandler::class, $c->getHandlers()[0]::class);
$c->unregister();
@ -41,7 +41,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
new JSONHandler()
);
$this->assertSame('MensBeam\Foundation\Catcher', $c::class);
$this->assertEquals(3, count($c->getHandlers()));
$this->assertSame(3, count($c->getHandlers()));
$h = $c->getHandlers();
$this->assertSame(PlainTextHandler::class, $h[0]::class);
$this->assertSame(HTMLHandler::class, $h[1]::class);
@ -75,7 +75,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
public function testMethod_getLastThrowable(): void {
$c = new Catcher(new PlainTextHandler([ 'silent' => true ]));
trigger_error('Ook!', \E_USER_WARNING);
$this->assertEquals(\E_USER_WARNING, $c->getLastThrowable()->getCode());
$this->assertSame(\E_USER_WARNING, $c->getLastThrowable()->getCode());
$c->unregister();
}
@ -96,13 +96,13 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$h = new PlainTextHandler();
$c = new Catcher($h, $h);
$c->unregister();
$this->assertEquals(\E_USER_WARNING, $e);
$this->assertSame(\E_USER_WARNING, $e);
$e = null;
$c = new Catcher();
$c->unregister();
$c->pushHandler($h, $h);
$this->assertEquals(\E_USER_WARNING, $e);
$this->assertSame(\E_USER_WARNING, $e);
restore_error_handler();
@ -183,7 +183,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$c = new Catcher();
$c->setHandlers(new PlainTextHandler());
$h = $c->getHandlers();
$this->assertEquals(1, count($h));
$this->assertSame(1, count($h));
$this->assertSame(PlainTextHandler::class, $h[0]::class);
$c->unregister();
}
@ -250,7 +250,7 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
$c = new Catcher(new PlainTextHandler());
$c->unshiftHandler(new JSONHandler(), new HTMLHandler(), new PlainTextHandler());
$h = $c->getHandlers();
$this->assertEquals(4, count($h));
$this->assertSame(4, count($h));
$this->assertSame(JSONHandler::class, $h[0]::class);
$this->assertSame(HTMLHandler::class, $h[1]::class);
$this->assertSame(PlainTextHandler::class, $h[2]::class);
@ -262,11 +262,11 @@ class TestCatcher extends \PHPUnit\Framework\TestCase {
});
$c->unshiftHandler($h[0]);
$this->assertEquals(\E_USER_WARNING, $e);
$this->assertSame(\E_USER_WARNING, $e);
$e = null;
$h = new PlainTextHandler();
$c->unshiftHandler($h, $h);
$this->assertEquals(\E_USER_WARNING, $e);
$this->assertSame(\E_USER_WARNING, $e);
restore_error_handler();
$c->unregister();

6
tests/cases/TestHandler.php

@ -52,7 +52,7 @@ class TestHandler extends \PHPUnit\Framework\TestCase {
// Just need to test forceExit for coverage purposes
$c = new Catcher(new PlainTextHandler([ 'forceExit' => true, 'silent' => true ]));
trigger_error('Ook!', \E_USER_ERROR);
$this->assertEquals(\E_USER_ERROR, $c->getLastThrowable()->getCode());
$this->assertSame(\E_USER_ERROR, $c->getLastThrowable()->getCode());
$c->unregister();
}
@ -83,7 +83,7 @@ class TestHandler extends \PHPUnit\Framework\TestCase {
// Just need to test forceOutputNow for coverage purposes
$c = new Catcher(new PlainTextHandler([ 'forceOutputNow' => true, 'silent' => true ]));
trigger_error('Ook!', \E_USER_ERROR);
$this->assertEquals(\E_USER_ERROR, $c->getLastThrowable()->getCode());
$this->assertSame(\E_USER_ERROR, $c->getLastThrowable()->getCode());
$c->unregister();
}
@ -117,7 +117,7 @@ class TestHandler extends \PHPUnit\Framework\TestCase {
ob_start();
trigger_error('Ook!', \E_USER_NOTICE);
ob_end_clean();
$this->assertEquals(\E_USER_NOTICE, $c->getLastThrowable()->getCode());
$this->assertSame(\E_USER_NOTICE, $c->getLastThrowable()->getCode());
$c->unregister();
}
}

95
tests/cases/TestPlainTextHandler.php

@ -10,50 +10,87 @@ namespace MensBeam\Foundation\Catcher\Test;
use MensBeam\Foundation\Catcher;
use MensBeam\Foundation\Catcher\{
Error,
HTMLHandler,
JSONHandler,
PlainTextHandler
Handler,
PlainTextHandler,
ThrowableController
};
use Eloquent\Phony\Phpunit\Phony,
Psr\Log\LoggerInterface;
class TestPlainTextHandler extends \Giberti\PHPUnitLocalServer\LocalServerTestCase {
public static function setupBeforeClass(): void {
static::createServerWithDocroot('./tests/docroot');
}
class TestPlainTextHandler extends \PHPUnit\Framework\TestCase {
/**
* @covers \MensBeam\Foundation\Catcher\Handler::__construct
*/
public function testMethod___construct__exception(): void {
$this->expectException(\InvalidArgumentException::class);
$c = new Catcher(new PlainTextHandler([ 'httpCode' => 42 ]));
}
/**
* @covers \MensBeam\Foundation\Catcher\Handler::dispatch
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::handleCallback
*
* @covers \MensBeam\Foundation\Catcher::__construct
* @covers \MensBeam\Foundation\Catcher::handleError
* @covers \MensBeam\Foundation\Catcher::handleThrowable
* @covers \MensBeam\Foundation\Catcher::pushHandler
* @covers \MensBeam\Foundation\Catcher::register
* @covers \MensBeam\Foundation\Catcher\Error::__construct
* @covers \MensBeam\Foundation\Catcher\Handler::__construct
* @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::log
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::serializeThrowable
* @covers \MensBeam\Foundation\Catcher\ThrowableController::__construct
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getErrorType
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getFrames
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getPrevious
* @covers \MensBeam\Foundation\Catcher\ThrowableController::getThrowable
*/
public function testMethod__dispatch(): void {
$url = $this->getLocalServerUrl() . '/testDispatch.php';
$this->assertEquals(1, preg_match('/^\[[0-9:]+\]\s+Warning/', file_get_contents($url)));
public function testMethod_handleCallback(): void {
$c = new ThrowableController(new \Exception(message: 'Ook!', previous: new \Error('Eek!')));
$l = Phony::mock(LoggerInterface::class);
$h = new PlainTextHandler([
'logger' => $l->get(),
'outputBacktrace' => true
]);
$o = $h->handle($c);
$this->assertSame(Handler::CONTINUE, $o->controlCode);
$this->assertSame(Handler::OUTPUT | Handler::NOW, $o->outputCode);
$this->assertStringContainsString('Caused by ↴', $o->output);
$l->critical->called();
}
/**
* @covers \MensBeam\Foundation\Catcher\PlainTextHandler::log
*/
public function testMethod_log(): void {
$l = Phony::mock(LoggerInterface::class);
$h = new PlainTextHandler([ 'logger' => $l->get() ]);
$e = [
'notice' => [
\E_NOTICE,
\E_USER_NOTICE,
\E_STRICT
],
'warning' => [
\E_WARNING,
\E_COMPILE_WARNING,
\E_USER_WARNING,
\E_DEPRECATED,
\E_USER_DEPRECATED
],
'error' => [
\E_RECOVERABLE_ERROR
],
'alert' => [
\E_PARSE,
\E_CORE_ERROR,
\E_COMPILE_ERROR
]
];
foreach ($e as $k => $v) {
foreach ($v as $vv) {
$h->handle(new ThrowableController(new Error('Ook!', $vv)));
$l->$k->called();
}
}
$h->handle(new ThrowableController(new \PharException('Ook!')));
$l->alert->called();
$h->handle(new ThrowableController(new \RuntimeException('Ook!')));
$l->alert->called();
}
}

42
tests/cases/TestThrowableController.php

@ -46,39 +46,39 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
ob_start();
trigger_error('Ook!', \E_USER_DEPRECATED);
ob_end_clean();
$this->assertEquals(\E_USER_DEPRECATED, $c->getLastThrowable()->getCode());
$this->assertSame(\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());
$this->assertSame(\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());
$this->assertSame('PHP Fatal Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_WARNING));
$this->assertEquals('PHP Warning', $c->getErrorType());
$this->assertSame('PHP Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_PARSE));
$this->assertEquals('PHP Parsing Error', $c->getErrorType());
$this->assertSame('PHP Parsing Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_NOTICE));
$this->assertEquals('PHP Notice', $c->getErrorType());
$this->assertSame('PHP Notice', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_DEPRECATED));
$this->assertEquals('Deprecated', $c->getErrorType());
$this->assertSame('Deprecated', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_CORE_ERROR));
$this->assertEquals('PHP Core Error', $c->getErrorType());
$this->assertSame('PHP Core Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_CORE_WARNING));
$this->assertEquals('PHP Core Warning', $c->getErrorType());
$this->assertSame('PHP Core Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_COMPILE_ERROR));
$this->assertEquals('Compile Error', $c->getErrorType());
$this->assertSame('Compile Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_COMPILE_WARNING));
$this->assertEquals('Compile Warning', $c->getErrorType());
$this->assertSame('Compile Warning', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_STRICT));
$this->assertEquals('Runtime Notice', $c->getErrorType());
$this->assertSame('Runtime Notice', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!', \E_RECOVERABLE_ERROR));
$this->assertEquals('Recoverable Error', $c->getErrorType());
$this->assertSame('Recoverable Error', $c->getErrorType());
$c = new ThrowableController(new Error('Ook!'));
$this->assertNull($c->getErrorType());
$c = new ThrowableController(new \Exception('Ook!'));
@ -100,7 +100,7 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(\Exception::class, $f[0]['class']);
$this->assertSame(\Exception::class, $f[0]['class']);
}
$f = false;
@ -110,7 +110,7 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(Error::class, $f[0]['class']);
$this->assertSame(Error::class, $f[0]['class']);
}
$f = false;
@ -120,8 +120,8 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(\Exception::class, $f[0]['class']);
$this->assertEquals(Error::class, $f[count($f) - 2]['class']);
$this->assertSame(\Exception::class, $f[0]['class']);
$this->assertSame(Error::class, $f[count($f) - 2]['class']);
}
$f = false;
@ -133,12 +133,12 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(\Exception::class, $f[0]['class']);
$this->assertSame(\Exception::class, $f[0]['class']);
$this->assertArrayHasKey('file', $f[2]);
$this->assertMatchesRegularExpression('/TestThrowableController\.php$/', $f[2]['file']);
$this->assertEquals('call_user_func_array', $f[2]['function']);
$this->assertSame('call_user_func_array', $f[2]['function']);
$this->assertArrayHasKey('line', $f[2]);
$this->assertNotEquals(0, $f[2]['line']);
$this->assertNotSame(0, $f[2]['line']);
}
// This is mostly here for code coverage: to delete userland error handling from
@ -151,7 +151,7 @@ class TestThrowableController extends \PHPUnit\Framework\TestCase {
$c = new ThrowableController($t);
$f = $c->getFrames();
} finally {
$this->assertEquals(\TypeError::class, $f[0]['class']);
$this->assertSame(\TypeError::class, $f[0]['class']);
}
// For code coverage purposes; should use the cached value instead of calculating

1
tests/phpunit.dist.xml

@ -19,6 +19,7 @@
<testsuite name="Main">
<file>cases/TestCatcher.php</file>
<file>cases/TestHandler.php</file>
<file>cases/TestPlainTextHandler.php</file>
<file>cases/TestThrowableController.php</file>
</testsuite>
</testsuites>

Loading…
Cancel
Save