add order manage

This commit is contained in:
2026-03-13 10:23:14 +08:00
parent cef2525f83
commit b5e6096200
4 changed files with 824 additions and 3 deletions
@@ -0,0 +1,464 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Order;
use App\Model\Order;
use App\Model\Role;
use App\Model\User;
use HyperfTest\TestCase;
use Qbhy\HyperfAuth\AuthManager;
use function Hyperf\Support\make;
/**
* OrderController 集成测试
*
* 覆盖 Normal/Raw 列表详情、字段校验、order_items 关联、时间范围筛选、401/404
*
* @internal
* @coversNothing
*/
class OrderControllerTest extends TestCase
{
protected function getAdminToken(): string
{
$admin_role = $this->fetchAdminRole();
$user = $this->fetchUser(static function ($query) use ($admin_role): void {
$query->where('status', 1)->where('role_id', $admin_role->id);
});
if (!$user) {
$this->markTestSkipped('没有可用的 administrator 用户');
}
$auth = make(AuthManager::class);
return $auth->guard('jwt')->login($user);
}
protected function fetchAdminRole(): Role
{
if (\Swoole\Coroutine::getCid() > 0) {
return Role::query()->where('name', 'administrator')->firstOrFail();
}
$role = null;
\Swoole\Coroutine\run(static function () use (&$role): void {
$role = Role::query()->where('name', 'administrator')->firstOrFail();
});
return $role;
}
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;
}
protected function authHeaders(): array
{
return ['Authorization' => 'Bearer ' . $this->getAdminToken()];
}
protected function hasOrderData(): bool
{
if (\Swoole\Coroutine::getCid() > 0) {
return Order::query()->exists();
}
$exists = false;
\Swoole\Coroutine\run(static function () use (&$exists): void {
$exists = Order::query()->exists();
});
return $exists;
}
protected function getFirstOrderId(): ?int
{
if (\Swoole\Coroutine::getCid() > 0) {
return Order::query()->value('id');
}
$id = null;
\Swoole\Coroutine\run(static function () use (&$id): void {
$id = Order::query()->value('id');
});
return $id;
}
// ========== Normal 列表接口 ==========
public function test_normal_list_returns_paginated_data(): void
{
$response = $this->get('/api/v1/orders', [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonStructure([
'code',
'message',
'data' => [
'items',
'total',
'page',
'per_page',
],
]);
}
public function test_normal_list_respects_per_page(): void
{
$response = $this->get('/api/v1/orders', ['per_page' => 5], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('data.per_page', 5);
}
public function test_normal_list_per_page_max_100(): void
{
$response = $this->get('/api/v1/orders', ['per_page' => 999], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('data.per_page', 100);
}
public function test_normal_list_excludes_raw_and_hash(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders', [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$first_item = $body['data']['items'][0] ?? [];
$this->assertArrayNotHasKey('raw', $first_item);
$this->assertArrayNotHasKey('hash', $first_item);
}
public function test_normal_list_contains_expected_fields(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders', [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$first_item = $body['data']['items'][0];
$expected_keys = [
'id', 'company_id', 'platform_id', 'store_id', 'order_status_id',
'platform_order_id', 'total_amount', 'total_paid', 'total_discount',
'created_date', 'paid_date', 'shipping_date',
];
foreach ($expected_keys as $key) {
$this->assertArrayHasKey($key, $first_item, "列表缺少字段: {$key}");
}
}
// ========== Normal 列表筛选 ==========
public function test_normal_list_filter_by_company_id(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$company_id = null;
if (\Swoole\Coroutine::getCid() > 0) {
$company_id = Order::query()->value('company_id');
} else {
\Swoole\Coroutine\run(static function () use (&$company_id): void {
$company_id = Order::query()->value('company_id');
});
}
$response = $this->get('/api/v1/orders', ['company_id' => $company_id], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertSame($company_id, $item['company_id']);
}
}
public function test_normal_list_filter_by_order_status_id(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$status_id = null;
if (\Swoole\Coroutine::getCid() > 0) {
$status_id = Order::query()->value('order_status_id');
} else {
\Swoole\Coroutine\run(static function () use (&$status_id): void {
$status_id = Order::query()->value('order_status_id');
});
}
$response = $this->get('/api/v1/orders', ['order_status_id' => $status_id], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
foreach ($body['data']['items'] as $item) {
$this->assertSame($status_id, $item['order_status_id']);
}
}
public function test_normal_list_filter_by_created_date_range(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders', [
'created_date_from' => '2020-01-01',
'created_date_to' => '2099-12-31',
], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
// 使用足够宽的范围,应该能返回数据
$this->assertGreaterThanOrEqual(0, $body['data']['total']);
}
public function test_normal_list_filter_by_paid_date_range(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders', [
'paid_date_from' => '2020-01-01',
'paid_date_to' => '2099-12-31',
], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
}
// ========== Normal 详情接口 ==========
public function test_normal_detail_returns_order(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonPath('data.id', $id);
}
public function test_normal_detail_contains_order_items(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$this->assertArrayHasKey('order_items', $body['data'], '订单详情应包含 order_items');
$this->assertIsArray($body['data']['order_items']);
}
public function test_normal_detail_contains_ext(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$this->assertArrayHasKey('ext', $body['data']);
}
public function test_normal_detail_excludes_raw_and_hash(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$this->assertArrayNotHasKey('raw', $body['data']);
$this->assertArrayNotHasKey('hash', $body['data']);
}
public function test_normal_detail_not_found_returns_404(): void
{
$response = $this->get('/api/v1/orders/999999999', [], $this->authHeaders());
$response->assertStatus(404);
$response->assertJsonPath('code', 404);
}
// ========== Raw 列表接口 ==========
public function test_raw_list_returns_paginated_data(): void
{
$response = $this->get('/api/v1/raw/orders', [], $this->authHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonStructure([
'code',
'message',
'data' => [
'items',
'total',
'page',
'per_page',
],
]);
}
public function test_raw_list_contains_hash_but_not_raw(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/raw/orders', [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$first_item = $body['data']['items'][0] ?? [];
$this->assertArrayHasKey('hash', $first_item, 'Raw 列表应包含 hash 字段');
$this->assertArrayNotHasKey('raw', $first_item, 'Raw 列表不应包含 raw 字段(太大)');
}
public function test_raw_list_excludes_business_fields(): void
{
if (!$this->hasOrderData()) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/raw/orders', [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$first_item = $body['data']['items'][0] ?? [];
// Raw 列表不应包含业务字段
$this->assertArrayNotHasKey('total_amount', $first_item);
$this->assertArrayNotHasKey('total_paid', $first_item);
$this->assertArrayNotHasKey('total_discount', $first_item);
$this->assertArrayNotHasKey('buyer_user_id', $first_item);
}
// ========== Raw 详情接口 ==========
public function test_raw_detail_contains_raw_and_hash(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/raw/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
$this->assertArrayHasKey('raw', $body['data'], 'Raw 详情应包含 raw 字段');
$this->assertArrayHasKey('hash', $body['data'], 'Raw 详情应包含 hash 字段');
$this->assertArrayHasKey('ext', $body['data'], 'Raw 详情应包含 ext 字段');
}
public function test_raw_detail_excludes_business_fields(): void
{
$id = $this->getFirstOrderId();
if (!$id) {
$this->markTestSkipped('没有订单数据');
}
$response = $this->get('/api/v1/raw/orders/' . $id, [], $this->authHeaders());
$response->assertStatus(200);
$body = json_decode($response->getContent(), true);
// Raw 详情不应包含业务字段
$this->assertArrayNotHasKey('total_amount', $body['data']);
$this->assertArrayNotHasKey('total_paid', $body['data']);
$this->assertArrayNotHasKey('buyer_user_id', $body['data']);
}
public function test_raw_detail_not_found_returns_404(): void
{
$response = $this->get('/api/v1/raw/orders/999999999', [], $this->authHeaders());
$response->assertStatus(404);
$response->assertJsonPath('code', 404);
}
// ========== 认证拦截 ==========
public function test_normal_list_without_token_returns_401(): void
{
$response = $this->get('/api/v1/orders');
$response->assertStatus(401);
}
public function test_normal_detail_without_token_returns_401(): void
{
$response = $this->get('/api/v1/orders/1');
$response->assertStatus(401);
}
public function test_raw_list_without_token_returns_401(): void
{
$response = $this->get('/api/v1/raw/orders');
$response->assertStatus(401);
}
public function test_raw_detail_without_token_returns_401(): void
{
$response = $this->get('/api/v1/raw/orders/1');
$response->assertStatus(401);
}
}