fetchUser(static function ($query): void { $query->where('status', 1); }); if (!$user) { $this->markTestSkipped('没有可用的活跃用户,无法测试'); } $auth = make(AuthManager::class); return $auth->guard('jwt')->login($user); } /** * 获取认证请求头 */ protected function authHeaders(): array { return ['Authorization' => 'Bearer ' . $this->getAuthToken()]; } protected function fetchUser(?callable $callback = null): ?User { if (\Swoole\Coroutine::getCid() > 0) { $query = User::query(); if ($callback !== null) { $callback($query); } return $query->first(); } $user = null; \Swoole\Coroutine\run(static function () use ($callback, &$user): void { $query = User::query(); if ($callback !== null) { $callback($query); } $user = $query->first(); }); return $user; } // ========== 列表接口测试 ========== public function test_list_users_returns_paginated_data(): void { $response = $this->get('/api/v1/users', [], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $response->assertJsonStructure([ 'code', 'message', 'data' => [ 'items', 'total', 'page', 'per_page', ], ]); } public function test_list_users_respects_page_and_per_page(): void { $response = $this->get('/api/v1/users', ['page' => 1, 'per_page' => 5], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $response->assertJsonPath('data.page', 1); $response->assertJsonPath('data.per_page', 5); } public function test_list_users_per_page_max_100(): void { $response = $this->get('/api/v1/users', ['per_page' => 999], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('data.per_page', 100); } public function test_list_users_filter_by_username(): void { // 先获取一个已知用户名 $user = $this->fetchUser(); if (!$user) { $this->markTestSkipped('没有用户数据'); } $response = $this->get('/api/v1/users', ['username' => $user->username], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $body = json_decode($response->getContent(), true); $this->assertGreaterThanOrEqual(1, $body['data']['total']); } public function test_list_users_filter_by_email(): void { $user = $this->fetchUser(static function ($query): void { $query->whereNotNull('email') ->where('email', '!=', ''); }); if (!$user) { $this->markTestSkipped('没有用户邮箱数据'); } $response = $this->get('/api/v1/users', ['email' => $user->email], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $body = json_decode($response->getContent(), true); $this->assertGreaterThanOrEqual(1, $body['data']['total']); } public function test_list_users_filter_by_status(): void { $response = $this->get('/api/v1/users', ['status' => 1], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $body = json_decode($response->getContent(), true); foreach ($body['data']['items'] as $item) { $this->assertSame(1, $item['status']); } } public function test_list_users_does_not_expose_password(): void { $response = $this->get('/api/v1/users', [], $this->authHeaders()); $response->assertStatus(200); $body = json_decode($response->getContent(), true); if (!empty($body['data']['items'])) { $first_item = $body['data']['items'][0]; $this->assertArrayNotHasKey('password', $first_item); $this->assertArrayNotHasKey('refresh_token', $first_item); } } // ========== 详情接口测试 ========== public function test_show_user_returns_user_data(): void { $user = $this->fetchUser(); if (!$user) { $this->markTestSkipped('没有用户数据'); } $response = $this->get('/api/v1/users/' . $user->id, [], $this->authHeaders()); $response->assertStatus(200); $response->assertJsonPath('code', 0); $response->assertJsonPath('data.id', $user->id); $response->assertJsonPath('data.username', $user->username); } public function test_show_user_does_not_expose_password(): void { $user = $this->fetchUser(); if (!$user) { $this->markTestSkipped('没有用户数据'); } $response = $this->get('/api/v1/users/' . $user->id, [], $this->authHeaders()); $response->assertStatus(200); $body = json_decode($response->getContent(), true); $this->assertArrayNotHasKey('password', $body['data']); $this->assertArrayNotHasKey('refresh_token', $body['data']); } public function test_show_user_not_found_returns_404(): void { $response = $this->get('/api/v1/users/999999', [], $this->authHeaders()); $response->assertStatus(404); $response->assertJsonPath('code', 404); } // ========== 认证拦截测试 ========== public function test_list_users_without_token_returns_401(): void { $response = $this->get('/api/v1/users'); $response->assertStatus(401); } public function test_show_user_without_token_returns_401(): void { $response = $this->get('/api/v1/users/1'); $response->assertStatus(401); } }