BaseRequestTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace Tests\Unit\ThirdParty;
  3. use PHPUnit\Framework\TestCase;
  4. use App\Module\ThirdParty\Services\BaseRequest;
  5. /**
  6. * BaseRequest基类测试
  7. *
  8. * 测试ThirdParty模块的BaseRequest基类功能
  9. * 验证Request机制的基础架构是否正确
  10. */
  11. class BaseRequestTest extends TestCase
  12. {
  13. /**
  14. * 测试BaseRequest类是否存在
  15. */
  16. public function testBaseRequestClassExists()
  17. {
  18. $this->assertTrue(
  19. class_exists(BaseRequest::class),
  20. 'BaseRequest类应该存在'
  21. );
  22. }
  23. /**
  24. * 测试BaseRequest是抽象类
  25. */
  26. public function testBaseRequestIsAbstract()
  27. {
  28. $reflection = new \ReflectionClass(BaseRequest::class);
  29. $this->assertTrue(
  30. $reflection->isAbstract(),
  31. 'BaseRequest应该是抽象类'
  32. );
  33. }
  34. /**
  35. * 测试BaseRequest有必需的抽象方法
  36. */
  37. public function testBaseRequestHasRequiredAbstractMethods()
  38. {
  39. $reflection = new \ReflectionClass(BaseRequest::class);
  40. // 检查是否有handler抽象方法
  41. $this->assertTrue(
  42. $reflection->hasMethod('handler'),
  43. 'BaseRequest应该有handler抽象方法'
  44. );
  45. $handlerMethod = $reflection->getMethod('handler');
  46. $this->assertTrue(
  47. $handlerMethod->isAbstract(),
  48. 'handler方法应该是抽象方法'
  49. );
  50. $this->assertTrue(
  51. $handlerMethod->isProtected(),
  52. 'handler方法应该是protected'
  53. );
  54. }
  55. /**
  56. * 测试BaseRequest有必需的公共方法
  57. */
  58. public function testBaseRequestHasRequiredPublicMethods()
  59. {
  60. $reflection = new \ReflectionClass(BaseRequest::class);
  61. // 检查request方法
  62. $this->assertTrue(
  63. $reflection->hasMethod('request'),
  64. 'BaseRequest应该有request公共方法'
  65. );
  66. $requestMethod = $reflection->getMethod('request');
  67. $this->assertTrue(
  68. $requestMethod->isPublic(),
  69. 'request方法应该是public'
  70. );
  71. }
  72. /**
  73. * 测试BaseRequest有必需的受保护方法
  74. */
  75. public function testBaseRequestHasRequiredProtectedMethods()
  76. {
  77. $reflection = new \ReflectionClass(BaseRequest::class);
  78. $expectedMethods = [
  79. 'checkQuota',
  80. 'updateQuota',
  81. 'logRequest',
  82. 'getConfig',
  83. 'getService',
  84. 'getCredential',
  85. 'getRequestId'
  86. ];
  87. foreach ($expectedMethods as $methodName) {
  88. $this->assertTrue(
  89. $reflection->hasMethod($methodName),
  90. "BaseRequest应该有{$methodName}方法"
  91. );
  92. $method = $reflection->getMethod($methodName);
  93. $this->assertTrue(
  94. $method->isProtected(),
  95. "{$methodName}方法应该是protected"
  96. );
  97. }
  98. }
  99. /**
  100. * 测试BaseRequest构造函数需要serviceCode参数
  101. */
  102. public function testBaseRequestConstructorRequiresServiceCode()
  103. {
  104. $reflection = new \ReflectionClass(BaseRequest::class);
  105. $constructor = $reflection->getConstructor();
  106. $this->assertNotNull($constructor, 'BaseRequest应该有构造函数');
  107. $parameters = $constructor->getParameters();
  108. $this->assertCount(1, $parameters, '构造函数应该有1个参数');
  109. $serviceCodeParam = $parameters[0];
  110. $this->assertEquals('serviceCode', $serviceCodeParam->getName(), '参数名应该是serviceCode');
  111. $this->assertEquals('string', $serviceCodeParam->getType()->getName(), '参数类型应该是string');
  112. }
  113. /**
  114. * 测试BaseRequest有必需的属性
  115. */
  116. public function testBaseRequestHasRequiredProperties()
  117. {
  118. $reflection = new \ReflectionClass(BaseRequest::class);
  119. $expectedProperties = [
  120. 'serviceCode',
  121. 'service',
  122. 'credential',
  123. 'requestId',
  124. 'startTime'
  125. ];
  126. foreach ($expectedProperties as $propertyName) {
  127. $this->assertTrue(
  128. $reflection->hasProperty($propertyName),
  129. "BaseRequest应该有{$propertyName}属性"
  130. );
  131. $property = $reflection->getProperty($propertyName);
  132. $this->assertTrue(
  133. $property->isProtected(),
  134. "{$propertyName}属性应该是protected"
  135. );
  136. }
  137. }
  138. /**
  139. * 创建一个测试用的BaseRequest实现类
  140. */
  141. private function createTestRequestClass()
  142. {
  143. return new class('test_service') extends BaseRequest {
  144. protected function handler(array $params): array
  145. {
  146. return [
  147. 'success' => true,
  148. 'message' => '测试请求处理成功',
  149. 'params' => $params,
  150. 'service_code' => $this->serviceCode,
  151. 'request_id' => $this->requestId,
  152. ];
  153. }
  154. // 重写方法以避免数据库依赖
  155. protected function initializeService(): void
  156. {
  157. // 跳过服务初始化
  158. }
  159. protected function checkQuota(): bool
  160. {
  161. return true;
  162. }
  163. protected function updateQuota(): void
  164. {
  165. // 跳过配额更新
  166. }
  167. protected function logRequest(array $params, array $result, bool $success): void
  168. {
  169. // 跳过日志记录
  170. }
  171. protected function getConfig(?string $key = null)
  172. {
  173. $config = ['test_key' => 'test_value'];
  174. return $key ? ($config[$key] ?? null) : $config;
  175. }
  176. };
  177. }
  178. /**
  179. * 测试BaseRequest的基本功能
  180. */
  181. public function testBaseRequestBasicFunctionality()
  182. {
  183. $testRequest = $this->createTestRequestClass();
  184. // 测试request方法可以调用
  185. $result = $testRequest->request(['test_param' => 'test_value']);
  186. $this->assertIsArray($result, 'request方法应该返回数组');
  187. $this->assertTrue($result['success'], '测试请求应该成功');
  188. $this->assertEquals('测试请求处理成功', $result['message']);
  189. $this->assertEquals(['test_param' => 'test_value'], $result['params']);
  190. $this->assertEquals('test_service', $result['service_code']);
  191. $this->assertNotEmpty($result['request_id'], '应该有请求ID');
  192. }
  193. /**
  194. * 测试BaseRequest的单一职责原则设计
  195. */
  196. public function testBaseRequestSingleResponsibilityDesign()
  197. {
  198. $reflection = new \ReflectionClass(BaseRequest::class);
  199. // BaseRequest应该只有一个抽象方法handler
  200. $abstractMethods = $reflection->getMethods(\ReflectionMethod::IS_ABSTRACT);
  201. $this->assertCount(1, $abstractMethods, 'BaseRequest应该只有一个抽象方法');
  202. $this->assertEquals('handler', $abstractMethods[0]->getName(), '抽象方法应该是handler');
  203. // 这确保了每个子类只需要实现一个handler方法,符合单一职责原则
  204. }
  205. /**
  206. * 测试BaseRequest的错误处理机制
  207. */
  208. public function testBaseRequestErrorHandling()
  209. {
  210. $testRequest = new class('test_service') extends BaseRequest {
  211. protected function handler(array $params): array
  212. {
  213. throw new \Exception('测试异常');
  214. }
  215. // 重写方法以避免数据库依赖
  216. protected function initializeService(): void {}
  217. protected function checkQuota(): bool { return true; }
  218. protected function updateQuota(): void {}
  219. protected function logRequest(array $params, array $result, bool $success): void {}
  220. protected function getConfig(?string $key = null) { return []; }
  221. };
  222. // 测试异常会被正确抛出
  223. $this->expectException(\Exception::class);
  224. $this->expectExceptionMessage('测试异常');
  225. $testRequest->request([]);
  226. }
  227. }