0, 'message' => 'Success', 'data' => ['result' => 'test'] ]; $response = new ApiResponse($data); $this->assertTrue($response->isSuccess()); $this->assertEquals(0, $response->getCode()); $this->assertEquals('Success', $response->getMessage()); $this->assertEquals(['result' => 'test'], $response->getData()); $this->assertEquals($data, $response->getRawData()); } public function testErrorResponse(): void { $data = [ 'code' => 1001, 'message' => 'Error occurred', 'data' => null ]; $response = new ApiResponse($data); $this->assertFalse($response->isSuccess()); $this->assertEquals(1001, $response->getCode()); $this->assertEquals('Error occurred', $response->getMessage()); $this->assertNull($response->getData()); } public function testResponseWithMsgField(): void { $data = [ 'code' => 0, 'msg' => 'Success with msg field', 'data' => 'test_data' ]; $response = new ApiResponse($data); $this->assertEquals('Success with msg field', $response->getMessage()); } public function testResponseWithMissingFields(): void { $data = []; $response = new ApiResponse($data); $this->assertFalse($response->isSuccess()); $this->assertEquals(-1, $response->getCode()); $this->assertEquals('', $response->getMessage()); $this->assertNull($response->getData()); } public function testResponseWithStringCode(): void { $data = [ 'code' => '0', 'message' => 'Success with string code' ]; $response = new ApiResponse($data); $this->assertTrue($response->isSuccess()); $this->assertEquals(0, $response->getCode()); } public function testResponseWithNonZeroCode(): void { $data = [ 'code' => 404, 'message' => 'Not found' ]; $response = new ApiResponse($data); $this->assertFalse($response->isSuccess()); $this->assertEquals(404, $response->getCode()); } public function testToArray(): void { $data = [ 'code' => 0, 'message' => 'Success', 'data' => ['key' => 'value'] ]; $response = new ApiResponse($data); $this->assertEquals($data, $response->toArray()); } public function testToJson(): void { $data = [ 'code' => 0, 'message' => 'Success', 'data' => ['key' => 'value'] ]; $response = new ApiResponse($data); $json = $response->toJson(); $this->assertIsString($json); $this->assertEquals($data, json_decode($json, true)); } public function testToJsonWithUnicodeCharacters(): void { $data = [ 'code' => 0, 'message' => '成功', 'data' => ['名称' => '测试'] ]; $response = new ApiResponse($data); $json = $response->toJson(); $this->assertIsString($json); $this->assertStringContainsString('成功', $json); $this->assertStringContainsString('名称', $json); $this->assertStringContainsString('测试', $json); } public function testHasMethod(): void { $data = [ 'code' => 0, 'message' => 'Success', 'extra' => 'additional_info' ]; $response = new ApiResponse($data); $this->assertTrue($response->has('code')); $this->assertTrue($response->has('message')); $this->assertTrue($response->has('extra')); $this->assertFalse($response->has('data')); $this->assertFalse($response->has('nonexistent')); } public function testGetMethod(): void { $data = [ 'code' => 0, 'message' => 'Success', 'data' => ['result' => 'test'], 'null_value' => null ]; $response = new ApiResponse($data); $this->assertEquals(0, $response->get('code')); $this->assertEquals('Success', $response->get('message')); $this->assertEquals(['result' => 'test'], $response->get('data')); $this->assertNull($response->get('null_value')); $this->assertNull($response->get('nonexistent')); $this->assertEquals('default', $response->get('nonexistent', 'default')); } public function testEmptyResponse(): void { $response = new ApiResponse([]); $this->assertFalse($response->isSuccess()); $this->assertEquals(-1, $response->getCode()); $this->assertEquals('', $response->getMessage()); $this->assertNull($response->getData()); $this->assertEquals([], $response->getRawData()); $this->assertEquals([], $response->toArray()); } public function testComplexDataStructure(): void { $complexData = [ 'users' => [ ['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane'] ], 'meta' => [ 'total' => 2, 'page' => 1 ] ]; $data = [ 'code' => 0, 'message' => 'Success', 'data' => $complexData ]; $response = new ApiResponse($data); $this->assertTrue($response->isSuccess()); $this->assertEquals($complexData, $response->getData()); $this->assertEquals(2, $response->getData()['meta']['total']); } }