From b8243619d638ed3b125370faa4ea84dc8bd2b01b Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Fri, 9 Dec 2022 06:56:46 -0600 Subject: [PATCH] PlainTextHandler 100% Coverage --- composer.json | 3 +- composer.lock | 44 +----------- lib/Catcher/PlainTextHandler.php | 4 +- tests/cases/TestCatcher.php | 18 ++--- tests/cases/TestHandler.php | 6 +- tests/cases/TestPlainTextHandler.php | 95 +++++++++++++++++-------- tests/cases/TestThrowableController.php | 42 +++++------ tests/phpunit.dist.xml | 1 + 8 files changed, 104 insertions(+), 109 deletions(-) diff --git a/composer.json b/composer.json index 4cc0884..679490c 100644 --- a/composer.json +++ b/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" } } diff --git a/composer.lock b/composer.lock index 55a706b..576c6d8 100644 --- a/composer.lock +++ b/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", diff --git a/lib/Catcher/PlainTextHandler.php b/lib/Catcher/PlainTextHandler.php index 4878b31..fe4a373 100644 --- a/lib/Catcher/PlainTextHandler.php +++ b/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 !== '') { diff --git a/tests/cases/TestCatcher.php b/tests/cases/TestCatcher.php index d7a95d6..6a14e7b 100644 --- a/tests/cases/TestCatcher.php +++ b/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(); diff --git a/tests/cases/TestHandler.php b/tests/cases/TestHandler.php index 512852a..b88c9d0 100644 --- a/tests/cases/TestHandler.php +++ b/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(); } } \ No newline at end of file diff --git a/tests/cases/TestPlainTextHandler.php b/tests/cases/TestPlainTextHandler.php index d45597b..0eff2cd 100644 --- a/tests/cases/TestPlainTextHandler.php +++ b/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(); } } \ No newline at end of file diff --git a/tests/cases/TestThrowableController.php b/tests/cases/TestThrowableController.php index b910030..d0f506e 100644 --- a/tests/cases/TestThrowableController.php +++ b/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 diff --git a/tests/phpunit.dist.xml b/tests/phpunit.dist.xml index 3b4fc0a..1fc334b 100644 --- a/tests/phpunit.dist.xml +++ b/tests/phpunit.dist.xml @@ -19,6 +19,7 @@ cases/TestCatcher.php cases/TestHandler.php + cases/TestPlainTextHandler.php cases/TestThrowableController.php