fix order item
This commit is contained in:
@@ -5,12 +5,8 @@ declare(strict_types=1);
|
||||
namespace HyperfTest\Cases\Integration\Order;
|
||||
|
||||
use App\Model\OrderItem;
|
||||
use App\Model\Role;
|
||||
use App\Model\User;
|
||||
use HyperfTest\TestCase;
|
||||
use Qbhy\HyperfAuth\AuthManager;
|
||||
|
||||
use function Hyperf\Support\make;
|
||||
use HyperfTest\Traits\AuthenticatedTestTrait;
|
||||
|
||||
/**
|
||||
* OrderItemController 集成测试
|
||||
@@ -22,83 +18,20 @@ use function Hyperf\Support\make;
|
||||
*/
|
||||
class OrderItemControllerTest 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 hasOrderItemData(): bool
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return $this->runInCoroutine(static function (): bool {
|
||||
return OrderItem::query()->exists();
|
||||
}
|
||||
|
||||
$exists = false;
|
||||
\Swoole\Coroutine\run(static function () use (&$exists): void {
|
||||
$exists = OrderItem::query()->exists();
|
||||
});
|
||||
return $exists;
|
||||
}
|
||||
|
||||
protected function getFirstOrderItemId(): ?int
|
||||
{
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
return $this->runInCoroutine(static function (): ?int {
|
||||
return OrderItem::query()->value('id');
|
||||
}
|
||||
|
||||
$id = null;
|
||||
\Swoole\Coroutine\run(static function () use (&$id): void {
|
||||
$id = OrderItem::query()->value('id');
|
||||
});
|
||||
return $id;
|
||||
}
|
||||
|
||||
// ========== 列表接口 ==========
|
||||
@@ -187,14 +120,9 @@ class OrderItemControllerTest extends TestCase
|
||||
$this->markTestSkipped('没有订单项数据');
|
||||
}
|
||||
|
||||
$company_id = null;
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$company_id = OrderItem::query()->value('company_id');
|
||||
} else {
|
||||
\Swoole\Coroutine\run(static function () use (&$company_id): void {
|
||||
$company_id = OrderItem::query()->value('company_id');
|
||||
});
|
||||
}
|
||||
$company_id = $this->runInCoroutine(static function (): mixed {
|
||||
return OrderItem::query()->value('company_id');
|
||||
});
|
||||
|
||||
$response = $this->get('/api/v1/order-items', ['company_id' => $company_id], $this->authHeaders());
|
||||
|
||||
@@ -206,20 +134,35 @@ class OrderItemControllerTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_list_filter_by_order_id(): void
|
||||
{
|
||||
if (!$this->hasOrderItemData()) {
|
||||
$this->markTestSkipped('没有订单项数据');
|
||||
}
|
||||
|
||||
$order_id = $this->runInCoroutine(static function (): mixed {
|
||||
return OrderItem::query()->value('order_id');
|
||||
});
|
||||
|
||||
$response = $this->get('/api/v1/order-items', ['order_id' => $order_id], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
$body = json_decode($response->getContent(), true);
|
||||
|
||||
foreach ($body['data']['items'] as $item) {
|
||||
$this->assertSame($order_id, $item['order_id']);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_list_filter_by_platform_order_id(): void
|
||||
{
|
||||
if (!$this->hasOrderItemData()) {
|
||||
$this->markTestSkipped('没有订单项数据');
|
||||
}
|
||||
|
||||
$platform_order_id = null;
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$platform_order_id = OrderItem::query()->value('platform_order_id');
|
||||
} else {
|
||||
\Swoole\Coroutine\run(static function () use (&$platform_order_id): void {
|
||||
$platform_order_id = OrderItem::query()->value('platform_order_id');
|
||||
});
|
||||
}
|
||||
$platform_order_id = $this->runInCoroutine(static function (): mixed {
|
||||
return OrderItem::query()->value('platform_order_id');
|
||||
});
|
||||
|
||||
$response = $this->get('/api/v1/order-items', ['platform_order_id' => $platform_order_id], $this->authHeaders());
|
||||
|
||||
@@ -237,14 +180,9 @@ class OrderItemControllerTest extends TestCase
|
||||
$this->markTestSkipped('没有订单项数据');
|
||||
}
|
||||
|
||||
$platform_product_id = null;
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$platform_product_id = OrderItem::query()->value('platform_product_id');
|
||||
} else {
|
||||
\Swoole\Coroutine\run(static function () use (&$platform_product_id): void {
|
||||
$platform_product_id = OrderItem::query()->value('platform_product_id');
|
||||
});
|
||||
}
|
||||
$platform_product_id = $this->runInCoroutine(static function (): mixed {
|
||||
return OrderItem::query()->value('platform_product_id');
|
||||
});
|
||||
|
||||
$response = $this->get('/api/v1/order-items', ['platform_product_id' => $platform_product_id], $this->authHeaders());
|
||||
|
||||
@@ -262,14 +200,9 @@ class OrderItemControllerTest extends TestCase
|
||||
$this->markTestSkipped('没有订单项数据');
|
||||
}
|
||||
|
||||
$product_sku = null;
|
||||
if (\Swoole\Coroutine::getCid() > 0) {
|
||||
$product_sku = OrderItem::query()->whereNotNull('product_sku')->where('product_sku', '!=', '')->value('product_sku');
|
||||
} else {
|
||||
\Swoole\Coroutine\run(static function () use (&$product_sku): void {
|
||||
$product_sku = OrderItem::query()->whereNotNull('product_sku')->where('product_sku', '!=', '')->value('product_sku');
|
||||
});
|
||||
}
|
||||
$product_sku = $this->runInCoroutine(static function (): mixed {
|
||||
return OrderItem::query()->whereNotNull('product_sku')->where('product_sku', '!=', '')->value('product_sku');
|
||||
});
|
||||
|
||||
if (!$product_sku) {
|
||||
$this->markTestSkipped('没有有 SKU 的订单项数据');
|
||||
|
||||
Reference in New Issue
Block a user