Skip to content

Commit 55ab7c5

Browse files
committed
Add tests for stack trace filtering in context
Tests verify that context string values are filtered when shorter stack traces feature is enabled, ensuring vendor paths are replaced with ellipsis while app paths remain.
1 parent a576fdc commit 55ab7c5

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/Unit/LaravelLogs/LaravelLogsTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,64 @@
255255
'size_formatted' => Utils::bytesForHumans(strlen($messageString) - strlen('[2023-08-24 15:51:14] local.DEBUG: ')),
256256
]);
257257
});
258+
259+
it('filters stack traces in context when shorter stack traces is enabled', function () {
260+
session(['log-viewer:shorter-stack-traces' => true]);
261+
config([
262+
'log-viewer.shorter_stack_trace_excludes' => [
263+
'/vendor/symfony/',
264+
'/vendor/laravel/framework/',
265+
],
266+
]);
267+
268+
$stackTrace = <<<'EOF'
269+
#0 /app/Controllers/UserController.php(25): someFunction()
270+
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
271+
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
272+
#3 /app/Middleware/CustomMiddleware.php(42): handle()
273+
#4 /vendor/symfony/routing/Router.php(89): route()
274+
#5 /app/bootstrap/app.php(15): bootstrap()
275+
EOF;
276+
277+
$logText = <<<EOF
278+
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
279+
EOF;
280+
281+
$log = new LaravelLog($logText);
282+
283+
expect($log->context)->toHaveKey('exception')
284+
->and($log->context['exception'])->toContain('#0 /app/Controllers/UserController.php(25): someFunction()')
285+
->and($log->context['exception'])->toContain('#3 /app/Middleware/CustomMiddleware.php(42): handle()')
286+
->and($log->context['exception'])->toContain('#5 /app/bootstrap/app.php(15): bootstrap()')
287+
->and($log->context['exception'])->toContain(' ...')
288+
->and($log->context['exception'])->not->toContain('/vendor/symfony/http-kernel/')
289+
->and($log->context['exception'])->not->toContain('/vendor/laravel/framework/')
290+
->and($log->context['exception'])->not->toContain('/vendor/symfony/routing/');
291+
});
292+
293+
it('does not filter context when shorter stack traces is disabled', function () {
294+
session(['log-viewer:shorter-stack-traces' => false]);
295+
config([
296+
'log-viewer.shorter_stack_trace_excludes' => [
297+
'/vendor/symfony/',
298+
'/vendor/laravel/framework/',
299+
],
300+
]);
301+
302+
$stackTrace = <<<'EOF'
303+
#0 /app/Controllers/UserController.php(25): someFunction()
304+
#1 /vendor/symfony/http-kernel/HttpKernel.php(158): handle()
305+
#2 /vendor/laravel/framework/Illuminate/Pipeline/Pipeline.php(128): process()
306+
EOF;
307+
308+
$logText = <<<EOF
309+
[2024-10-18 12:00:00] production.ERROR: Exception occurred {"exception":"$stackTrace"}
310+
EOF;
311+
312+
$log = new LaravelLog($logText);
313+
314+
expect($log->context)->toHaveKey('exception')
315+
->and($log->context['exception'])->toBe($stackTrace)
316+
->and($log->context['exception'])->toContain('/vendor/symfony/http-kernel/')
317+
->and($log->context['exception'])->toContain('/vendor/laravel/framework/');
318+
});

0 commit comments

Comments
 (0)