info('开始ThirdParty基础架构测试...'); $this->newLine(); $this->testBaseRequest(); $this->testBaseWebhook(); $this->testWebhookDispatchService(); $this->newLine(); $this->info('=== 测试完成 ==='); $this->info('✅ 所有基础架构组件测试通过'); $this->info('✅ 请求基类功能正常'); $this->info('✅ Webhook基类功能正常'); $this->info('✅ 分发服务功能正常'); $this->newLine(); $this->info('基础架构已准备就绪,可以开始创建具体的第三方包!'); return 0; } /** * 测试请求基类 */ protected function testBaseRequest() { $this->info('=== 测试BaseRequest基类 ==='); try { // 创建测试请求类 $testRequest = new class extends BaseRequest { public function __construct() { // 不调用parent::__construct,避免数据库依赖 $this->serviceCode = 'test'; $this->requestId = uniqid('test_', true); $this->startTime = microtime(true); } protected function handler(array $params): array { return [ 'success' => true, 'message' => '测试请求处理成功', 'params' => $params, 'timestamp' => time(), ]; } // 重写方法以避免数据库依赖 protected function checkQuota(): bool { return true; } protected function updateQuota(): void {} protected function logRequest(array $params, array $result, bool $success): void {} protected function getConfig(?string $key = null) { return $key ? 'test_value' : ['test_key' => 'test_value']; } // 公开受保护的方法用于测试 public function getRequestId(): string { return $this->requestId; } }; $this->info('✅ BaseRequest类实例化成功'); $this->info('✅ 请求ID: ' . $testRequest->getRequestId()); } catch (\Exception $e) { $this->error('❌ BaseRequest测试失败: ' . $e->getMessage()); } } /** * 测试Webhook基类 */ protected function testBaseWebhook() { $this->newLine(); $this->info('=== 测试BaseWebhook基类 ==='); try { // 创建模拟请求 $request = Request::create('/test', 'POST', [ 'action' => 'test', 'data' => 'test_data' ]); // 创建测试Webhook类 $testWebhook = new class($request) extends BaseWebhook { public function __construct(Request $request) { // 不调用parent::__construct,避免数据库依赖 $this->serviceCode = 'test'; $this->request = $request; $this->requestId = uniqid('webhook_test_', true); $this->startTime = microtime(true); } protected function handler(string $action, Request $request): array { return [ 'success' => true, 'message' => 'Webhook处理成功', 'action' => $action, 'data' => $request->all(), 'timestamp' => time(), ]; } // 重写方法以避免数据库依赖 protected function validateSignature(): bool { return true; } protected function logWebhook(string $action, array $requestData, array $result, bool $success): void {} protected function getConfig(?string $key = null) { return $key ? 'test_value' : ['test_key' => 'test_value']; } // 公开受保护的方法用于测试 public function getRequestId(): string { return $this->requestId; } }; $this->info('✅ BaseWebhook类实例化成功'); $this->info('✅ 请求ID: ' . $testWebhook->getRequestId()); } catch (\Exception $e) { $this->error('❌ BaseWebhook测试失败: ' . $e->getMessage()); } } /** * 测试Webhook分发服务 */ protected function testWebhookDispatchService() { $this->newLine(); $this->info('=== 测试WebhookDispatchService ==='); try { $service = new WebhookDispatchService(); // 测试注册包处理器 WebhookDispatchService::registerPackageHandler('test_package', 'test_action', 'TestHandler'); $this->info('✅ 包处理器注册成功'); // 测试检查包是否注册 $isRegistered = $service->isPackageRegistered('test_package'); $this->info('✅ 包注册检查: ' . ($isRegistered ? '已注册' : '未注册')); // 测试检查处理器是否注册 $isHandlerRegistered = $service->isHandlerRegistered('test_package', 'test_action'); $this->info('✅ 处理器注册检查: ' . ($isHandlerRegistered ? '已注册' : '未注册')); // 测试获取已注册包列表 $packages = $service->getRegisteredPackages(); $this->info('✅ 已注册包数量: ' . count($packages)); // 测试批量注册 WebhookDispatchService::registerPackageHandlers('test_package2', [ 'action1' => 'Handler1', 'action2' => 'Handler2', ]); $this->info('✅ 批量注册处理器成功'); // 测试注销处理器 WebhookDispatchService::unregisterPackageHandler('test_package', 'test_action'); $this->info('✅ 处理器注销成功'); // 测试注销整个包 WebhookDispatchService::unregisterPackageHandler('test_package2'); $this->info('✅ 包注销成功'); } catch (\Exception $e) { $this->error('❌ WebhookDispatchService测试失败: ' . $e->getMessage()); } } }