update user api
This commit is contained in:
@@ -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