111 lines
3.9 KiB
PHP
111 lines
3.9 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Services\Admin\AdminSlowRequestReporter;
|
|
use Database\Seeders\RolesAndPermissionsSeeder;
|
|
use Illuminate\Support\Facades\File;
|
|
use Livewire\Volt\Volt as LivewireVolt;
|
|
use Tests\TestCase;
|
|
|
|
test('slow admin reporter parses and aggregates slow request logs', function () {
|
|
$logPath = createAdminSlowRequestLog();
|
|
|
|
$report = app(AdminSlowRequestReporter::class)->report(
|
|
filters: ['route' => 'admin.users'],
|
|
top: 5,
|
|
limit: 5,
|
|
paths: [$logPath],
|
|
);
|
|
|
|
expect($report['summary']['total_requests'])->toBe(2)
|
|
->and($report['summary']['unique_routes'])->toBe(1)
|
|
->and($report['summary']['max_duration_ms'])->toBe(1400)
|
|
->and($report['top_routes'][0]['value'])->toBe('admin.users.index')
|
|
->and($report['top_routes'][0]['requests'])->toBe(2)
|
|
->and($report['slowest_requests'][0]['duration_ms'])->toBe(1400)
|
|
->and($report['slow_queries'][0]['occurrences'])->toBe(2)
|
|
->and($report['explain_plans'][0]['explainable'])->toBeTrue()
|
|
->and($report['explain_plans'][0]['plan'])->not->toBeEmpty();
|
|
});
|
|
|
|
test('slow admin request command renders report sections', function () {
|
|
/** @var TestCase $this */
|
|
$logPath = createAdminSlowRequestLog();
|
|
|
|
$this->artisan('admin:slow-requests', [
|
|
'--file' => [$logPath],
|
|
'--top' => 5,
|
|
'--limit' => 5,
|
|
])
|
|
->assertSuccessful()
|
|
->expectsOutput('Slow-Admin-Request-Report')
|
|
->expectsOutput('Requests: 3')
|
|
->expectsOutputToContain('Top Routen')
|
|
->expectsOutputToContain('Langsamste Requests')
|
|
->expectsOutputToContain('EXPLAIN Top Slow Queries');
|
|
});
|
|
|
|
test('admin slow request report page renders filters and report data', function () {
|
|
/** @var TestCase $this */
|
|
$logPath = createAdminSlowRequestLog();
|
|
config(['logging.channels.admin_slow.path' => $logPath]);
|
|
config(['admin_performance.slow_requests.enabled' => false]);
|
|
|
|
$this->seed(RolesAndPermissionsSeeder::class);
|
|
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole('admin');
|
|
$this->actingAs($admin);
|
|
|
|
LivewireVolt::test('admin.reports.slow-requests')
|
|
->assertSee('Performance Reports')
|
|
->assertSee('Top Routen')
|
|
->assertSee('EXPLAIN Top Slow Queries')
|
|
->assertSee('admin.users.index')
|
|
->set('routeFilter', 'admin.contacts')
|
|
->assertSee('admin.contacts.index')
|
|
->assertDontSee('admin.users.index');
|
|
});
|
|
|
|
function createAdminSlowRequestLog(): string
|
|
{
|
|
$path = storage_path('logs/testing-admin-slow.log');
|
|
File::ensureDirectoryExists(dirname($path));
|
|
File::put($path, collect([
|
|
adminSlowRequestLogLine('2026-04-29 10:00:00', 'admin.users.index', '/admin/users', 920, 180.5, 42),
|
|
adminSlowRequestLogLine('2026-04-29 10:05:00', 'admin.users.index', '/admin/users?page=2', 1400, 220.75, 58),
|
|
adminSlowRequestLogLine('2026-04-29 10:10:00', 'admin.contacts.index', '/admin/contacts', 760, 90.25, 25, 302),
|
|
])->implode(PHP_EOL).PHP_EOL);
|
|
|
|
return $path;
|
|
}
|
|
|
|
function adminSlowRequestLogLine(
|
|
string $timestamp,
|
|
string $route,
|
|
string $path,
|
|
int $durationMs,
|
|
float $databaseTimeMs,
|
|
int $queryCount,
|
|
int $statusCode = 200,
|
|
): string {
|
|
$context = [
|
|
'method' => 'GET',
|
|
'path' => $path,
|
|
'route_name' => $route,
|
|
'status_code' => $statusCode,
|
|
'user_id' => 1,
|
|
'duration_ms' => $durationMs,
|
|
'database_time_ms' => $databaseTimeMs,
|
|
'query_count' => $queryCount,
|
|
'slow_queries' => [
|
|
[
|
|
'sql' => 'select * from "users" where "email" = ?',
|
|
'time_ms' => 75.5,
|
|
'connection' => 'sqlite',
|
|
],
|
|
],
|
|
];
|
|
|
|
return '['.$timestamp.'] local.WARNING: Slow admin request detected. '.json_encode($context, JSON_UNESCAPED_SLASHES);
|
|
}
|