generateToken([ 'id' => 1, 'username' => 'admin', 'role' => 'admin', ])['access_token']; } public static function tearDownAfterClass(): void { if (self::$serverProcess) { proc_terminate(self::$serverProcess); proc_close(self::$serverProcess); } } private static function request(string $method, string $path, array $body = [], array $headers = []): array { $url = self::$baseUrl . $path; $ch = curl_init(); $opts = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_CUSTOMREQUEST => $method, ]; if (!empty($headers)) { $opts[CURLOPT_HTTPHEADER] = $headers; } if (!empty($body)) { $opts[CURLOPT_POSTFIELDS] = json_encode($body, JSON_UNESCAPED_UNICODE); } curl_setopt_array($ch, $opts); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true) ?? []; } public function testGetCurrentUserProfile(): void { $resp = self::request('GET', '/api/auth/me', [], [ 'Authorization: Bearer ' . self::$token, ]); $this->assertSame(200, $resp['code']); $this->assertSame('admin', $resp['data']['username']); $this->assertArrayHasKey('nickname', $resp['data']); } public function testUpdateProfile(): void { $uniqueNickname = 'Admin_' . time(); $resp = self::request('PUT', '/api/auth/profile', [ 'nickname' => $uniqueNickname, 'email' => 'admin_' . time() . '@example.com', 'phone' => '1380000' . mt_rand(1000, 9999), ], [ 'Authorization: Bearer ' . self::$token, 'Content-Type: application/json', ]); $this->assertSame(200, $resp['code']); $me = self::request('GET', '/api/auth/me', [], [ 'Authorization: Bearer ' . self::$token, ]); $this->assertSame($uniqueNickname, $me['data']['nickname']); // 恢复昵称 self::request('PUT', '/api/auth/profile', [ 'nickname' => '超级管理员', 'email' => '', 'phone' => '', ], [ 'Authorization: Bearer ' . self::$token, 'Content-Type: application/json', ]); } public function testUpdateProfileWithInvalidEmail(): void { $resp = self::request('PUT', '/api/auth/profile', [ 'email' => 'invalid-email', ], [ 'Authorization: Bearer ' . self::$token, 'Content-Type: application/json', ]); $this->assertSame(400, $resp['code']); } }