ResponseHandlerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit\Response;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Exception\ApiException;
  6. use SixShop\Wangdian\Response\ApiResponse;
  7. use SixShop\Wangdian\Response\ResponseHandler;
  8. class ResponseHandlerTest extends TestCase
  9. {
  10. private ResponseHandler $handler;
  11. protected function setUp(): void
  12. {
  13. $this->handler = new ResponseHandler();
  14. }
  15. public function testHandleSuccess(): void
  16. {
  17. $responseData = [
  18. 'code' => 0,
  19. 'message' => 'Success',
  20. 'data' => ['result' => 'test']
  21. ];
  22. $response = $this->handler->handle($responseData);
  23. $this->assertInstanceOf(ApiResponse::class, $response);
  24. $this->assertTrue($response->isSuccess());
  25. $this->assertEquals(0, $response->getCode());
  26. $this->assertEquals('Success', $response->getMessage());
  27. $this->assertEquals(['result' => 'test'], $response->getData());
  28. }
  29. public function testIsSuccessWithZeroCode(): void
  30. {
  31. $responseData = ['code' => 0];
  32. $this->assertTrue($this->handler->isSuccess($responseData));
  33. }
  34. public function testIsSuccessWithStringZeroCode(): void
  35. {
  36. $responseData = ['code' => '0'];
  37. $this->assertTrue($this->handler->isSuccess($responseData));
  38. }
  39. public function testIsSuccessWithNonZeroCode(): void
  40. {
  41. $responseData = ['code' => 1];
  42. $this->assertFalse($this->handler->isSuccess($responseData));
  43. }
  44. public function testIsSuccessWithMissingCode(): void
  45. {
  46. $responseData = ['message' => 'Some message'];
  47. $this->assertFalse($this->handler->isSuccess($responseData));
  48. }
  49. public function testExtractErrorFromSuccessResponse(): void
  50. {
  51. $responseData = [
  52. 'code' => 0,
  53. 'message' => 'Success'
  54. ];
  55. $error = $this->handler->extractError($responseData);
  56. $this->assertNull($error);
  57. }
  58. public function testExtractErrorFromErrorResponse(): void
  59. {
  60. $responseData = [
  61. 'code' => 1001,
  62. 'message' => 'Authentication failed',
  63. 'data' => ['error_detail' => 'Invalid token']
  64. ];
  65. $error = $this->handler->extractError($responseData);
  66. $this->assertIsArray($error);
  67. $this->assertEquals(1001, $error['code']);
  68. $this->assertEquals('Authentication failed', $error['message']);
  69. $this->assertEquals(['error_detail' => 'Invalid token'], $error['data']);
  70. }
  71. public function testExtractErrorWithMsgField(): void
  72. {
  73. $responseData = [
  74. 'code' => 404,
  75. 'msg' => 'Resource not found'
  76. ];
  77. $error = $this->handler->extractError($responseData);
  78. $this->assertIsArray($error);
  79. $this->assertEquals(404, $error['code']);
  80. $this->assertEquals('Resource not found', $error['message']);
  81. $this->assertNull($error['data']);
  82. }
  83. public function testExtractErrorWithMissingFields(): void
  84. {
  85. $responseData = [];
  86. $error = $this->handler->extractError($responseData);
  87. $this->assertIsArray($error);
  88. $this->assertEquals('unknown', $error['code']);
  89. $this->assertEquals('Unknown error', $error['message']);
  90. $this->assertNull($error['data']);
  91. }
  92. public function testValidateOrThrowWithSuccessResponse(): void
  93. {
  94. $responseData = [
  95. 'code' => 0,
  96. 'message' => 'Success'
  97. ];
  98. // Should not throw any exception
  99. $this->handler->validateOrThrow($responseData);
  100. $this->assertTrue(true); // Assert that we reach this point
  101. }
  102. public function testValidateOrThrowWithErrorResponse(): void
  103. {
  104. $responseData = [
  105. 'code' => 1001,
  106. 'message' => 'Authentication failed',
  107. 'data' => ['error_detail' => 'Invalid token']
  108. ];
  109. $this->expectException(ApiException::class);
  110. $this->expectExceptionMessage('Authentication failed');
  111. $this->handler->validateOrThrow($responseData);
  112. }
  113. public function testValidateOrThrowWithApiException(): void
  114. {
  115. $responseData = [
  116. 'code' => 500,
  117. 'message' => 'Internal server error'
  118. ];
  119. try {
  120. $this->handler->validateOrThrow($responseData);
  121. $this->fail('Expected ApiException was not thrown');
  122. } catch (ApiException $e) {
  123. $this->assertEquals('Internal server error', $e->getMessage());
  124. $this->assertEquals('500', $e->getApiCode());
  125. $this->assertEquals($responseData, $e->getResponseData());
  126. }
  127. }
  128. public function testValidateOrThrowWithMsgField(): void
  129. {
  130. $responseData = [
  131. 'code' => 400,
  132. 'msg' => 'Bad request'
  133. ];
  134. $this->expectException(ApiException::class);
  135. $this->expectExceptionMessage('Bad request');
  136. $this->handler->validateOrThrow($responseData);
  137. }
  138. public function testValidateOrThrowWithMissingMessage(): void
  139. {
  140. $responseData = [
  141. 'code' => 999
  142. ];
  143. $this->expectException(ApiException::class);
  144. $this->expectExceptionMessage('Unknown error');
  145. $this->handler->validateOrThrow($responseData);
  146. }
  147. public function testHandleAndValidateWorkflow(): void
  148. {
  149. $successData = [
  150. 'code' => 0,
  151. 'message' => 'Operation completed',
  152. 'data' => ['id' => 123]
  153. ];
  154. // Handle should work
  155. $response = $this->handler->handle($successData);
  156. $this->assertInstanceOf(ApiResponse::class, $response);
  157. // Validate should not throw
  158. $this->handler->validateOrThrow($successData);
  159. $this->assertTrue(true);
  160. $errorData = [
  161. 'code' => 404,
  162. 'message' => 'Not found'
  163. ];
  164. // Handle should still work
  165. $errorResponse = $this->handler->handle($errorData);
  166. $this->assertInstanceOf(ApiResponse::class, $errorResponse);
  167. $this->assertFalse($errorResponse->isSuccess());
  168. // Validate should throw
  169. $this->expectException(ApiException::class);
  170. $this->handler->validateOrThrow($errorData);
  171. }
  172. }