pp-planer/tests/Feature/ApiLogControllerTest.php

134 lines
3.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\ApiRequestLog;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class ApiLogControllerTest extends TestCase
{
use RefreshDatabase;
public function test_api_log_index_zeigt_die_api_logs_seite_mit_paginated_logs(): void
{
$this->withoutVite();
$user = User::factory()->create();
ApiRequestLog::create([
'method' => 'fetchEvents',
'endpoint' => 'events',
'status' => 'success',
'duration_ms' => 124,
]);
$response = $this->actingAs($user)->get(route('api-logs.index'));
$response->assertOk();
$response->assertInertia(
fn ($page) => $page
->component('ApiLogs/Index')
->has('logs.data', 1)
->where('logs.data.0.method', 'fetchEvents')
->where('filters.search', '')
->where('filters.status', '')
);
}
public function test_api_log_index_filtert_nach_suche(): void
{
$this->withoutVite();
$user = User::factory()->create();
ApiRequestLog::create([
'method' => 'fetchEvents',
'endpoint' => 'events',
'status' => 'success',
'duration_ms' => 101,
]);
ApiRequestLog::create([
'method' => 'syncAgenda',
'endpoint' => 'agenda',
'status' => 'error',
'error_message' => 'Agenda not found',
'duration_ms' => 222,
]);
$response = $this->actingAs($user)->get(route('api-logs.index', ['search' => 'fetchEvents']));
$response->assertOk();
$response->assertInertia(
fn ($page) => $page
->component('ApiLogs/Index')
->has('logs.data', 1)
->where('logs.data.0.method', 'fetchEvents')
->where('filters.search', 'fetchEvents')
);
}
public function test_api_log_index_filtert_nach_status(): void
{
$this->withoutVite();
$user = User::factory()->create();
ApiRequestLog::create([
'method' => 'fetchSongs',
'endpoint' => 'songs',
'status' => 'success',
'duration_ms' => 77,
]);
ApiRequestLog::create([
'method' => 'getEventServices',
'endpoint' => 'event_services',
'status' => 'error',
'error_message' => 'Service not found',
'duration_ms' => 309,
]);
$response = $this->actingAs($user)->get(route('api-logs.index', ['status' => 'error']));
$response->assertOk();
$response->assertInertia(
fn ($page) => $page
->component('ApiLogs/Index')
->has('logs.data', 1)
->where('logs.data.0.status', 'error')
->where('filters.status', 'error')
);
}
public function test_api_request_log_scopes_funktionieren(): void
{
ApiRequestLog::create([
'method' => 'fetchEvents',
'endpoint' => 'events',
'status' => 'success',
'duration_ms' => 51,
]);
ApiRequestLog::create([
'method' => 'syncAgenda',
'endpoint' => 'agenda',
'status' => 'error',
'error_message' => 'Agenda not found',
'duration_ms' => 120,
]);
$countErrorWithSearch = ApiRequestLog::query()
->byStatus('error')
->search('agenda')
->count();
$countWithoutFilter = ApiRequestLog::query()
->byStatus('')
->search('')
->count();
$this->assertSame(1, $countErrorWithSearch);
$this->assertSame(2, $countWithoutFilter);
}
}