|
255 | 255 | 'size_formatted' => Utils::bytesForHumans(strlen($messageString) - strlen('[2023-08-24 15:51:14] local.DEBUG: ')), |
256 | 256 | ]); |
257 | 257 | }); |
| 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 | + // Normalize expected value for cross-platform comparison (heredocs may have platform line endings) |
| 315 | + $expectedStackTrace = str_replace(["\r\n", "\r"], "\n", $stackTrace); |
| 316 | + |
| 317 | + expect($log->context)->toHaveKey('exception') |
| 318 | + ->and($log->context['exception'])->toBe($expectedStackTrace) |
| 319 | + ->and($log->context['exception'])->toContain('/vendor/symfony/http-kernel/') |
| 320 | + ->and($log->context['exception'])->toContain('/vendor/laravel/framework/'); |
| 321 | +}); |
0 commit comments