ApiResponseTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit\Response;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Response\ApiResponse;
  6. class ApiResponseTest extends TestCase
  7. {
  8. public function testSuccessResponse(): void
  9. {
  10. $data = [
  11. 'code' => 0,
  12. 'message' => 'Success',
  13. 'data' => ['result' => 'test']
  14. ];
  15. $response = new ApiResponse($data);
  16. $this->assertTrue($response->isSuccess());
  17. $this->assertEquals(0, $response->getCode());
  18. $this->assertEquals('Success', $response->getMessage());
  19. $this->assertEquals(['result' => 'test'], $response->getData());
  20. $this->assertEquals($data, $response->getRawData());
  21. }
  22. public function testErrorResponse(): void
  23. {
  24. $data = [
  25. 'code' => 1001,
  26. 'message' => 'Error occurred',
  27. 'data' => null
  28. ];
  29. $response = new ApiResponse($data);
  30. $this->assertFalse($response->isSuccess());
  31. $this->assertEquals(1001, $response->getCode());
  32. $this->assertEquals('Error occurred', $response->getMessage());
  33. $this->assertNull($response->getData());
  34. }
  35. public function testResponseWithMsgField(): void
  36. {
  37. $data = [
  38. 'code' => 0,
  39. 'msg' => 'Success with msg field',
  40. 'data' => 'test_data'
  41. ];
  42. $response = new ApiResponse($data);
  43. $this->assertEquals('Success with msg field', $response->getMessage());
  44. }
  45. public function testResponseWithMissingFields(): void
  46. {
  47. $data = [];
  48. $response = new ApiResponse($data);
  49. $this->assertFalse($response->isSuccess());
  50. $this->assertEquals(-1, $response->getCode());
  51. $this->assertEquals('', $response->getMessage());
  52. $this->assertNull($response->getData());
  53. }
  54. public function testResponseWithStringCode(): void
  55. {
  56. $data = [
  57. 'code' => '0',
  58. 'message' => 'Success with string code'
  59. ];
  60. $response = new ApiResponse($data);
  61. $this->assertTrue($response->isSuccess());
  62. $this->assertEquals(0, $response->getCode());
  63. }
  64. public function testResponseWithNonZeroCode(): void
  65. {
  66. $data = [
  67. 'code' => 404,
  68. 'message' => 'Not found'
  69. ];
  70. $response = new ApiResponse($data);
  71. $this->assertFalse($response->isSuccess());
  72. $this->assertEquals(404, $response->getCode());
  73. }
  74. public function testToArray(): void
  75. {
  76. $data = [
  77. 'code' => 0,
  78. 'message' => 'Success',
  79. 'data' => ['key' => 'value']
  80. ];
  81. $response = new ApiResponse($data);
  82. $this->assertEquals($data, $response->toArray());
  83. }
  84. public function testToJson(): void
  85. {
  86. $data = [
  87. 'code' => 0,
  88. 'message' => 'Success',
  89. 'data' => ['key' => 'value']
  90. ];
  91. $response = new ApiResponse($data);
  92. $json = $response->toJson();
  93. $this->assertIsString($json);
  94. $this->assertEquals($data, json_decode($json, true));
  95. }
  96. public function testToJsonWithUnicodeCharacters(): void
  97. {
  98. $data = [
  99. 'code' => 0,
  100. 'message' => '成功',
  101. 'data' => ['名称' => '测试']
  102. ];
  103. $response = new ApiResponse($data);
  104. $json = $response->toJson();
  105. $this->assertIsString($json);
  106. $this->assertStringContainsString('成功', $json);
  107. $this->assertStringContainsString('名称', $json);
  108. $this->assertStringContainsString('测试', $json);
  109. }
  110. public function testHasMethod(): void
  111. {
  112. $data = [
  113. 'code' => 0,
  114. 'message' => 'Success',
  115. 'extra' => 'additional_info'
  116. ];
  117. $response = new ApiResponse($data);
  118. $this->assertTrue($response->has('code'));
  119. $this->assertTrue($response->has('message'));
  120. $this->assertTrue($response->has('extra'));
  121. $this->assertFalse($response->has('data'));
  122. $this->assertFalse($response->has('nonexistent'));
  123. }
  124. public function testGetMethod(): void
  125. {
  126. $data = [
  127. 'code' => 0,
  128. 'message' => 'Success',
  129. 'data' => ['result' => 'test'],
  130. 'null_value' => null
  131. ];
  132. $response = new ApiResponse($data);
  133. $this->assertEquals(0, $response->get('code'));
  134. $this->assertEquals('Success', $response->get('message'));
  135. $this->assertEquals(['result' => 'test'], $response->get('data'));
  136. $this->assertNull($response->get('null_value'));
  137. $this->assertNull($response->get('nonexistent'));
  138. $this->assertEquals('default', $response->get('nonexistent', 'default'));
  139. }
  140. public function testEmptyResponse(): void
  141. {
  142. $response = new ApiResponse([]);
  143. $this->assertFalse($response->isSuccess());
  144. $this->assertEquals(-1, $response->getCode());
  145. $this->assertEquals('', $response->getMessage());
  146. $this->assertNull($response->getData());
  147. $this->assertEquals([], $response->getRawData());
  148. $this->assertEquals([], $response->toArray());
  149. }
  150. public function testComplexDataStructure(): void
  151. {
  152. $complexData = [
  153. 'users' => [
  154. ['id' => 1, 'name' => 'John'],
  155. ['id' => 2, 'name' => 'Jane']
  156. ],
  157. 'meta' => [
  158. 'total' => 2,
  159. 'page' => 1
  160. ]
  161. ];
  162. $data = [
  163. 'code' => 0,
  164. 'message' => 'Success',
  165. 'data' => $complexData
  166. ];
  167. $response = new ApiResponse($data);
  168. $this->assertTrue($response->isSuccess());
  169. $this->assertEquals($complexData, $response->getData());
  170. $this->assertEquals(2, $response->getData()['meta']['total']);
  171. }
  172. }