fix order item

This commit is contained in:
2026-03-13 11:09:15 +08:00
parent 93046ceb1f
commit 8a0166508f
5 changed files with 128 additions and 270 deletions
@@ -5,12 +5,8 @@ declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Product;
use App\Model\Product;
use App\Model\Role;
use App\Model\User;
use HyperfTest\TestCase;
use Qbhy\HyperfAuth\AuthManager;
use function Hyperf\Support\make;
use HyperfTest\Traits\AuthenticatedTestTrait;
/**
* ProductController 集成测试
@@ -22,83 +18,20 @@ use function Hyperf\Support\make;
*/
class ProductControllerTest 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()];
}
use AuthenticatedTestTrait;
protected function hasProductData(): bool
{
if (\Swoole\Coroutine::getCid() > 0) {
return $this->runInCoroutine(static function (): bool {
return Product::query()->exists();
}
$exists = false;
\Swoole\Coroutine\run(static function () use (&$exists): void {
$exists = Product::query()->exists();
});
return $exists;
}
protected function getFirstProductId(): ?int
{
if (\Swoole\Coroutine::getCid() > 0) {
return $this->runInCoroutine(static function (): ?int {
return Product::query()->value('id');
}
$id = null;
\Swoole\Coroutine\run(static function () use (&$id): void {
$id = Product::query()->value('id');
});
return $id;
}
// ========== Normal 列表接口 ==========
@@ -180,15 +113,9 @@ class ProductControllerTest extends TestCase
$this->markTestSkipped('没有产品数据');
}
// 获取一个已知的 company_id
$company_id = null;
if (\Swoole\Coroutine::getCid() > 0) {
$company_id = Product::query()->value('company_id');
} else {
\Swoole\Coroutine\run(static function () use (&$company_id): void {
$company_id = Product::query()->value('company_id');
});
}
$company_id = $this->runInCoroutine(static function (): mixed {
return Product::query()->value('company_id');
});
$response = $this->get('/api/v1/products', ['company_id' => $company_id], $this->authHeaders());