update user api
This commit is contained in:
+2
-1
@@ -2,8 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
namespace App\Controller\Api\V1;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Model\User;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api\V1;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\Model\User;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
#[Controller(prefix: "/api/v1/users")]
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* 用户列表
|
||||
*
|
||||
* 支持分页、按 username/email 模糊搜索、按 status 精确筛选
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "GET")]
|
||||
#[Middleware(AuthMiddleware::class)]
|
||||
public function index(): array
|
||||
{
|
||||
$page = max(1, (int) $this->request->input('page', 1));
|
||||
$per_page = min(100, max(1, (int) $this->request->input('per_page', 15)));
|
||||
|
||||
$query = User::query();
|
||||
|
||||
// 按 username 模糊搜索
|
||||
$username = $this->request->input('username');
|
||||
if ($username !== null && $username !== '') {
|
||||
$query->where('username', 'like', '%' . $username . '%');
|
||||
}
|
||||
|
||||
// 按 email 模糊搜索
|
||||
$email = $this->request->input('email');
|
||||
if ($email !== null && $email !== '') {
|
||||
$query->where('email', 'like', '%' . $email . '%');
|
||||
}
|
||||
|
||||
// 按 status 精确筛选
|
||||
$status = $this->request->input('status');
|
||||
if ($status !== null && $status !== '') {
|
||||
$query->where('status', (int) $status);
|
||||
}
|
||||
|
||||
// 按 created_at 降序排序
|
||||
$query->orderBy('created_at', 'desc');
|
||||
|
||||
$total = $query->count();
|
||||
$items = $query->offset(($page - 1) * $per_page)
|
||||
->limit($per_page)
|
||||
->get();
|
||||
|
||||
return [
|
||||
'code' => 0,
|
||||
'message' => '获取成功',
|
||||
'data' => [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'per_page' => $per_page,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户详情
|
||||
*
|
||||
* @param int $id 用户 ID
|
||||
*/
|
||||
#[RequestMapping(path: "{id}", methods: "GET")]
|
||||
#[Middleware(AuthMiddleware::class)]
|
||||
public function show(int $id): \Psr\Http\Message\ResponseInterface|array
|
||||
{
|
||||
$user = User::query()->find($id);
|
||||
|
||||
if (!$user) {
|
||||
return $this->response->json([
|
||||
'code' => 404,
|
||||
'message' => '用户不存在',
|
||||
])->withStatus(404);
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => 0,
|
||||
'message' => '获取成功',
|
||||
'data' => $user,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -97,14 +97,7 @@ class User extends Model implements Authenticatable
|
||||
return $this->hasMany(Platform::class, 'developer_id');
|
||||
}
|
||||
|
||||
protected function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleting(function (User $user) {
|
||||
$user->developedPlatforms()->update(['developer_id' => 1]);
|
||||
});
|
||||
}
|
||||
// @TODO 重新实现删除用户时平台归属转移逻辑(Hyperf 不支持 static::deleting 事件绑定)
|
||||
|
||||
/**
|
||||
* Check if refresh token is valid.
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\Controller\\Api\\V1\\": "app/Controller/api/v1/",
|
||||
"App\\": "app/"
|
||||
},
|
||||
"files": []
|
||||
|
||||
@@ -33,6 +33,10 @@ return [
|
||||
'platforms' => (static function (): array {
|
||||
$platforms = [];
|
||||
|
||||
if (env('APP_ENV', 'dev') === 'testing') {
|
||||
return $platforms;
|
||||
}
|
||||
|
||||
try {
|
||||
// 从数据库获取所有启用的平台
|
||||
$rows = Db::table('platforms')
|
||||
|
||||
@@ -20,8 +20,10 @@ use Hyperf\Testing\TestCase;
|
||||
*/
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
public function testExample()
|
||||
public function testExample(): void
|
||||
{
|
||||
$this->get('/')->assertOk()->assertSee('Hyperf');
|
||||
$this->get('/health')
|
||||
->assertOk()
|
||||
->assertJsonPath('message', 'System status ok');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Cases\Integration\User;
|
||||
|
||||
use App\Model\User;
|
||||
use HyperfTest\TestCase;
|
||||
use Qbhy\HyperfAuth\AuthManager;
|
||||
|
||||
use function Hyperf\Support\make;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class UserControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* 获取认证用的 JWT Token
|
||||
*/
|
||||
protected function getAuthToken(): string
|
||||
{
|
||||
$user = $this->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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest;
|
||||
|
||||
use Hyperf\HttpServer\MiddlewareManager;
|
||||
use Hyperf\Testing\Http\TestResponse;
|
||||
|
||||
use function Hyperf\Support\make;
|
||||
|
||||
/**
|
||||
* 自定义 TestCase 基类
|
||||
*
|
||||
* 修复 Hyperf Testing Client 不处理 PriorityMiddleware 的问题:
|
||||
* Server.php 在分发请求前会调用 MiddlewareManager::sortMiddlewares()
|
||||
* 将 PriorityMiddleware 对象转为字符串类名,但 Testing Client 缺少此步骤。
|
||||
*/
|
||||
abstract class TestCase extends \Hyperf\Testing\TestCase
|
||||
{
|
||||
private bool $middlewaresSorted = false;
|
||||
|
||||
protected function doRequest(string $method, ...$args): TestResponse
|
||||
{
|
||||
$client = make(\Hyperf\Testing\Http\Client::class);
|
||||
|
||||
// Client 构造时触发路由注册,此时排序中间件
|
||||
if (!$this->middlewaresSorted) {
|
||||
$this->sortAllRegisteredMiddlewares();
|
||||
$this->middlewaresSorted = true;
|
||||
}
|
||||
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return $this->createTestResponse(
|
||||
$client->{$method}(...$args)
|
||||
);
|
||||
}
|
||||
|
||||
$response = null;
|
||||
$exception = null;
|
||||
|
||||
\Swoole\Coroutine\run(function () use ($client, $method, $args, &$response, &$exception): void {
|
||||
try {
|
||||
$response = $this->createTestResponse(
|
||||
$client->{$method}(...$args)
|
||||
);
|
||||
} catch (\Throwable $throwable) {
|
||||
$exception = $throwable;
|
||||
}
|
||||
});
|
||||
|
||||
if ($exception !== null) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($response === null) {
|
||||
throw new \RuntimeException('Test response not initialized.');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将所有已注册的 PriorityMiddleware 对象转为字符串类名
|
||||
*/
|
||||
private function sortAllRegisteredMiddlewares(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(MiddlewareManager::class);
|
||||
$prop = $reflection->getProperty('container');
|
||||
$prop->setAccessible(true);
|
||||
$container = $prop->getValue();
|
||||
|
||||
foreach ($container as $server => $paths) {
|
||||
foreach ($paths as $path => $methods) {
|
||||
foreach ($methods as $method => $middlewares) {
|
||||
$container[$server][$path][$method] = MiddlewareManager::sortMiddlewares($middlewares);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$prop->setValue(null, $container);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user