Skip to content

Commit

Permalink
Check for exception output correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray committed Feb 17, 2024
1 parent 874bff4 commit 4d934b3
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions test/Integration/HttpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Cookie\ResponseCookie;
use Amp\Http\HttpStatus;
use Amp\PHPUnit\AsyncTestCase;
use Cspray\AnnotatedContainer\AnnotatedContainer;
use Cspray\StreamBufferIntercept\BufferIdentifier;
use Cspray\StreamBufferIntercept\StreamBuffer;
use Labrador\DummyApp\Controller\SessionDtoController;
use Labrador\DummyApp\CountingService;
use Labrador\DummyApp\DummyMonologInitializer;
use Labrador\DummyApp\Middleware\BarMiddleware;
use Labrador\DummyApp\Middleware\BazMiddleware;
Expand Down Expand Up @@ -149,20 +146,18 @@ public function testExceptionThrowHasCorrectLogOutputSentToStdout() : void {
$request = new Request('http://localhost:4200/exception');
$client->request($request);

$expectedContext = '{"client_address":"127.0.0.1:%d","method":"GET","path":"/exception","exception_class":"RuntimeException","file":"%a/RouterListener.php","line_number":28,"exception_message":"A message detailing what went wrong that should show up in logs.","stack_trace":%a}';
$expected = <<<TEXT
%a
%a labrador.app.ERROR: RuntimeException thrown in %a/RouterListener.php#L28 handling client 127.0.0.1:%d with request "GET /exception". Message: A message detailing what went wrong that should show up in logs. $expectedContext []
TEXT;
$handler = self::$container->get(DummyMonologInitializer::class)->testHandler;
self::assertInstanceOf(TestHandler::class, $handler);

self::assertStringMatchesFormat(
$expected,
join(
PHP_EOL,
array_map(static fn(LogRecord $logRecord) => $logRecord->formatted, $handler->getRecords())
)
);
self::assertTrue($handler->hasErrorThatPasses(static function (LogRecord $record) {
$expectedContext = '{"client_address":"127.0.0.1:%d","method":"GET","path":"/exception","exception_class":"RuntimeException","file":"%a/RouterListener.php","line_number":28,"exception_message":"A message detailing what went wrong that should show up in logs.","stack_trace":%a}';
$expected = <<<TEXT
%a labrador.app.ERROR: RuntimeException thrown in %a/RouterListener.php#L28 handling client 127.0.0.1:%d with request "GET /exception". Message: A message detailing what went wrong that should show up in logs. $expectedContext []
TEXT;
return (new StringMatchesFormatDescription($expected))->evaluate(
other: $record->formatted,
returnResult: true
);
}));
}
}

0 comments on commit 4d934b3

Please sign in to comment.