update tests and doc

This commit is contained in:
2026-03-17 12:47:02 +08:00
parent ebcbfaac40
commit ccea43d8d6
4 changed files with 1291 additions and 0 deletions
@@ -0,0 +1,214 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\System;
use App\Model\ApiRequestLog;
use HyperfTest\TestCase;
use HyperfTest\Traits\AuthenticatedTestTrait;
/**
* RequestLogController 集成测试
*
* 覆盖列表分页/筛选、详情(含完整请求体和 User-Agent)、仅 admin 可访问、404
*
* @internal
* @coversNothing
*/
class RequestLogControllerTest extends TestCase
{
use AuthenticatedTestTrait;
private static ?int $testRecordId = null;
/**
* 确保有测试数据
*/
protected function ensureTestData(): int
{
if (self::$testRecordId !== null) {
return self::$testRecordId;
}
$id = $this->runInCoroutine(static function (): int {
$record = ApiRequestLog::query()->create([
'user_id' => 1,
'method' => 'POST',
'path' => '/api/v1/test/integration',
'status_code' => 200,
'ip' => '127.0.0.1',
'user_agent' => 'PHPUnit/Integration-Test',
'request_body' => ['username' => 'test', 'action' => 'create'],
'response_code' => 0,
'duration_ms' => 42,
]);
return $record->id;
});
self::$testRecordId = $id;
return $id;
}
protected function tearDown(): void
{
parent::tearDown();
}
public static function tearDownAfterClass(): void
{
if (self::$testRecordId !== null) {
$id = self::$testRecordId;
if (\Swoole\Coroutine::getCid() > 0) {
ApiRequestLog::query()->where('id', $id)->delete();
} else {
\Swoole\Coroutine\run(static function () use ($id): void {
ApiRequestLog::query()->where('id', $id)->delete();
});
}
self::$testRecordId = null;
}
parent::tearDownAfterClass();
}
// ========== 列表接口 ==========
public function test_list_returns_paginated_data(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonStructure([
'code',
'message',
'data' => [
'items',
'total',
'page',
'per_page',
],
]);
}
public function test_list_respects_per_page(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', ['per_page' => 5], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('data.per_page', 5);
}
public function test_list_excludes_request_body(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', [], $this->authHeaders());
$response->assertStatus(200);
$items = $response->json('data.items');
if (!empty($items)) {
$first = $items[0];
$this->assertArrayNotHasKey('request_body', $first);
$this->assertArrayNotHasKey('user_agent', $first);
$this->assertArrayHasKey('method', $first);
$this->assertArrayHasKey('path', $first);
}
}
// ========== 筛选 ==========
public function test_list_filter_by_method(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', ['method' => 'POST'], $this->authHeaders());
$response->assertStatus(200);
$items = $response->json('data.items');
foreach ($items as $item) {
$this->assertSame('POST', $item['method']);
}
}
public function test_list_filter_by_status_code(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', ['status_code' => 200], $this->authHeaders());
$response->assertStatus(200);
$items = $response->json('data.items');
foreach ($items as $item) {
$this->assertSame(200, $item['status_code']);
}
}
public function test_list_filter_by_path_like(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', ['path' => 'integration'], $this->authHeaders());
$response->assertStatus(200);
$items = $response->json('data.items');
foreach ($items as $item) {
$this->assertStringContainsString('integration', strtolower($item['path']));
}
}
public function test_list_filter_by_created_at_range(): void
{
$this->ensureTestData();
$response = $this->get('/api/v1/logs/requests', [
'created_at_from' => '2026-03-01',
'created_at_to' => '2026-03-31',
], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
}
// ========== 详情接口 ==========
public function test_detail_contains_full_fields(): void
{
$id = $this->ensureTestData();
$response = $this->get("/api/v1/logs/requests/{$id}", [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$data = $response->json('data');
$this->assertArrayHasKey('request_body', $data);
$this->assertArrayHasKey('user_agent', $data);
$this->assertArrayHasKey('method', $data);
$this->assertArrayHasKey('path', $data);
$this->assertArrayHasKey('ip', $data);
$this->assertArrayHasKey('duration_ms', $data);
$this->assertSame('POST', $data['method']);
}
public function test_detail_not_found_returns_404(): void
{
$response = $this->get('/api/v1/logs/requests/999999', [], $this->authHeaders());
$response->assertStatus(404);
$this->assertSame(404, $response->json('code'));
}
// ========== 认证检查 ==========
public function test_list_without_token_returns_401(): void
{
$response = $this->get('/api/v1/logs/requests');
$response->assertStatus(401);
}
}
@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Unit\Middleware;
use App\Middleware\RequestLogMiddleware;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;
/**
* RequestLogMiddleware 静态辅助方法单元测试
*
* 覆盖 sanitizeBody 敏感字段脱敏、extractResponseCode JSON 提取、getClientIp IP 获取
*
* @internal
* @coversNothing
*/
class RequestLogMiddlewareTest extends TestCase
{
// ========== sanitizeBody ==========
public function test_sanitize_body_replaces_password_with_asterisks(): void
{
$body = ['username' => 'admin', 'password' => 'secret123'];
$result = RequestLogMiddleware::sanitizeBody($body);
$this->assertSame('admin', $result['username']);
$this->assertSame('***', $result['password']);
}
public function test_sanitize_body_handles_nested_password_fields(): void
{
$body = [
'user' => [
'name' => 'test',
'password' => 'nested_secret',
],
];
$result = RequestLogMiddleware::sanitizeBody($body);
$this->assertSame('test', $result['user']['name']);
$this->assertSame('***', $result['user']['password']);
}
public function test_sanitize_body_handles_all_password_variants(): void
{
$body = [
'password' => 'p1',
'old_password' => 'p2',
'new_password' => 'p3',
'password_confirmation' => 'p4',
];
$result = RequestLogMiddleware::sanitizeBody($body);
$this->assertSame('***', $result['password']);
$this->assertSame('***', $result['old_password']);
$this->assertSame('***', $result['new_password']);
$this->assertSame('***', $result['password_confirmation']);
}
public function test_sanitize_body_preserves_non_sensitive_fields(): void
{
$body = ['username' => 'admin', 'email' => 'a@b.com', 'status' => 1];
$result = RequestLogMiddleware::sanitizeBody($body);
$this->assertSame($body, $result);
}
public function test_sanitize_body_handles_empty_array(): void
{
$result = RequestLogMiddleware::sanitizeBody([]);
$this->assertSame([], $result);
}
// ========== extractResponseCode ==========
public function test_extract_response_code_from_json_response(): void
{
$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'code' => 0,
'message' => 'success',
'data' => [],
]));
$code = RequestLogMiddleware::extractResponseCode($response);
$this->assertSame(0, $code);
}
public function test_extract_response_code_returns_null_for_non_json(): void
{
$response = new Response(200, ['Content-Type' => 'text/html'], '<html></html>');
$code = RequestLogMiddleware::extractResponseCode($response);
$this->assertNull($code);
}
public function test_extract_response_code_returns_null_when_no_code_field(): void
{
$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'message' => 'ok',
]));
$code = RequestLogMiddleware::extractResponseCode($response);
$this->assertNull($code);
}
// ========== getClientIp ==========
public function test_get_client_ip_from_x_forwarded_for(): void
{
$request = new ServerRequest('GET', '/test', ['X-Forwarded-For' => '1.2.3.4, 5.6.7.8']);
$ip = RequestLogMiddleware::getClientIp($request);
$this->assertSame('1.2.3.4', $ip);
}
public function test_get_client_ip_from_x_real_ip(): void
{
$request = new ServerRequest('GET', '/test', ['X-Real-IP' => '10.0.0.1']);
$ip = RequestLogMiddleware::getClientIp($request);
$this->assertSame('10.0.0.1', $ip);
}
public function test_get_client_ip_from_server_params(): void
{
$request = new ServerRequest('GET', '/test', [], null, '1.1', ['remote_addr' => '192.168.1.1']);
$ip = RequestLogMiddleware::getClientIp($request);
$this->assertSame('192.168.1.1', $ip);
}
}