add mq controller
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Cases\Integration\System;
|
||||
|
||||
use HyperfTest\TestCase;
|
||||
use HyperfTest\Traits\AuthenticatedTestTrait;
|
||||
|
||||
/**
|
||||
* MqStatusController 集成测试
|
||||
*
|
||||
* 覆盖接口结构、队列参数筛选、无效参数 400、未认证 401
|
||||
*
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class MqStatusControllerTest extends TestCase
|
||||
{
|
||||
use AuthenticatedTestTrait;
|
||||
|
||||
// ========== 正常返回结构 ==========
|
||||
|
||||
public function test_mq_status_returns_expected_structure(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/mq/status', [], $this->authHeaders());
|
||||
|
||||
// RabbitMQ 可能不可用,接受 200 或 500
|
||||
$status = $response->getStatusCode();
|
||||
if ($status === 500) {
|
||||
// 连接不可用时返回 500,仍验证响应格式
|
||||
$response->assertJsonStructure(['code', 'message', 'data']);
|
||||
$this->assertSame(500, $response->json('code'));
|
||||
return;
|
||||
}
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('code', 0);
|
||||
$response->assertJsonPath('message', '获取成功');
|
||||
$response->assertJsonStructure([
|
||||
'code',
|
||||
'message',
|
||||
'data' => [
|
||||
'business_queues',
|
||||
'retry_queues',
|
||||
'error_queue',
|
||||
'summary' => [
|
||||
'total_messages',
|
||||
'total_consumers',
|
||||
],
|
||||
'fetched_at',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
// ========== 队列类型筛选 ==========
|
||||
|
||||
public function test_mq_status_with_valid_queue_filter(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/mq/status', ['queue' => 'orders'], $this->authHeaders());
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
if ($status === 500) {
|
||||
// RabbitMQ 不可用,跳过具体数据验证
|
||||
return;
|
||||
}
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonPath('code', 0);
|
||||
|
||||
$data = $response->json('data');
|
||||
|
||||
// 筛选时 business_queues 仅包含 orders.queue
|
||||
foreach ($data['business_queues'] as $queue) {
|
||||
$this->assertStringContainsString('orders', $queue['queue']);
|
||||
}
|
||||
|
||||
// 筛选时 error_queue 为空数组(仅全量查询时返回)
|
||||
$this->assertEmpty($data['error_queue']);
|
||||
}
|
||||
|
||||
// ========== 无效参数 ==========
|
||||
|
||||
public function test_mq_status_with_invalid_queue_returns_400(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/mq/status', ['queue' => 'invalid_type'], $this->authHeaders());
|
||||
|
||||
$response->assertStatus(400);
|
||||
$this->assertSame(400, $response->json('code'));
|
||||
$this->assertStringContainsString('无效的队列类型', $response->json('message'));
|
||||
}
|
||||
|
||||
// ========== 认证检查 ==========
|
||||
|
||||
public function test_mq_status_without_token_returns_401(): void
|
||||
{
|
||||
$response = $this->get('/api/v1/mq/status');
|
||||
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Cases\Unit\Service;
|
||||
|
||||
use App\Service\MqStatusService;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* MqStatusService 单元测试
|
||||
*
|
||||
* 覆盖队列类型常量、状态判定逻辑
|
||||
*
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class MqStatusServiceTest extends TestCase
|
||||
{
|
||||
private MqStatusService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$config = $this->createMock(ConfigInterface::class);
|
||||
$this->service = new MqStatusService($config);
|
||||
}
|
||||
|
||||
// ========== getValidQueueTypes ==========
|
||||
|
||||
public function test_get_valid_queue_types_returns_correct_list(): void
|
||||
{
|
||||
$types = $this->service->getValidQueueTypes();
|
||||
|
||||
$this->assertIsArray($types);
|
||||
$this->assertContains('orders', $types);
|
||||
$this->assertContains('products', $types);
|
||||
$this->assertContains('refunds', $types);
|
||||
$this->assertContains('inventory', $types);
|
||||
$this->assertCount(4, $types);
|
||||
}
|
||||
|
||||
// ========== getQueueStatus 状态判定 ==========
|
||||
|
||||
public function test_status_high_load_when_messages_over_100(): void
|
||||
{
|
||||
$this->assertSame('high_load', $this->service->getQueueStatus(101));
|
||||
$this->assertSame('high_load', $this->service->getQueueStatus(500));
|
||||
$this->assertSame('high_load', $this->service->getQueueStatus(10000));
|
||||
}
|
||||
|
||||
public function test_status_processing_when_messages_over_10(): void
|
||||
{
|
||||
$this->assertSame('processing', $this->service->getQueueStatus(11));
|
||||
$this->assertSame('processing', $this->service->getQueueStatus(50));
|
||||
$this->assertSame('processing', $this->service->getQueueStatus(100));
|
||||
}
|
||||
|
||||
public function test_status_active_when_messages_over_0(): void
|
||||
{
|
||||
$this->assertSame('active', $this->service->getQueueStatus(1));
|
||||
$this->assertSame('active', $this->service->getQueueStatus(5));
|
||||
$this->assertSame('active', $this->service->getQueueStatus(10));
|
||||
}
|
||||
|
||||
public function test_status_empty_when_no_messages(): void
|
||||
{
|
||||
$this->assertSame('empty', $this->service->getQueueStatus(0));
|
||||
}
|
||||
|
||||
// ========== 边界值测试 ==========
|
||||
|
||||
public function test_status_boundary_at_exactly_100(): void
|
||||
{
|
||||
// 100 条消息 → processing(> 10 且 <= 100)
|
||||
$this->assertSame('processing', $this->service->getQueueStatus(100));
|
||||
}
|
||||
|
||||
public function test_status_boundary_at_exactly_10(): void
|
||||
{
|
||||
// 10 条消息 → active(> 0 且 <= 10)
|
||||
$this->assertSame('active', $this->service->getQueueStatus(10));
|
||||
}
|
||||
|
||||
public function test_status_boundary_at_exactly_1(): void
|
||||
{
|
||||
$this->assertSame('active', $this->service->getQueueStatus(1));
|
||||
}
|
||||
|
||||
// ========== QUEUE_TYPES 常量 ==========
|
||||
|
||||
public function test_queue_types_constant_matches_valid_types(): void
|
||||
{
|
||||
$this->assertSame(
|
||||
MqStatusService::QUEUE_TYPES,
|
||||
$this->service->getValidQueueTypes()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user