handler = new ResponseHandler(); } public function testHandleSuccess(): void { $responseData = [ 'code' => 0, 'message' => 'Success', 'data' => ['result' => 'test'] ]; $response = $this->handler->handle($responseData); $this->assertInstanceOf(ApiResponse::class, $response); $this->assertTrue($response->isSuccess()); $this->assertEquals(0, $response->getCode()); $this->assertEquals('Success', $response->getMessage()); $this->assertEquals(['result' => 'test'], $response->getData()); } public function testIsSuccessWithZeroCode(): void { $responseData = ['code' => 0]; $this->assertTrue($this->handler->isSuccess($responseData)); } public function testIsSuccessWithStringZeroCode(): void { $responseData = ['code' => '0']; $this->assertTrue($this->handler->isSuccess($responseData)); } public function testIsSuccessWithNonZeroCode(): void { $responseData = ['code' => 1]; $this->assertFalse($this->handler->isSuccess($responseData)); } public function testIsSuccessWithMissingCode(): void { $responseData = ['message' => 'Some message']; $this->assertFalse($this->handler->isSuccess($responseData)); } public function testExtractErrorFromSuccessResponse(): void { $responseData = [ 'code' => 0, 'message' => 'Success' ]; $error = $this->handler->extractError($responseData); $this->assertNull($error); } public function testExtractErrorFromErrorResponse(): void { $responseData = [ 'code' => 1001, 'message' => 'Authentication failed', 'data' => ['error_detail' => 'Invalid token'] ]; $error = $this->handler->extractError($responseData); $this->assertIsArray($error); $this->assertEquals(1001, $error['code']); $this->assertEquals('Authentication failed', $error['message']); $this->assertEquals(['error_detail' => 'Invalid token'], $error['data']); } public function testExtractErrorWithMsgField(): void { $responseData = [ 'code' => 404, 'msg' => 'Resource not found' ]; $error = $this->handler->extractError($responseData); $this->assertIsArray($error); $this->assertEquals(404, $error['code']); $this->assertEquals('Resource not found', $error['message']); $this->assertNull($error['data']); } public function testExtractErrorWithMissingFields(): void { $responseData = []; $error = $this->handler->extractError($responseData); $this->assertIsArray($error); $this->assertEquals('unknown', $error['code']); $this->assertEquals('Unknown error', $error['message']); $this->assertNull($error['data']); } public function testValidateOrThrowWithSuccessResponse(): void { $responseData = [ 'code' => 0, 'message' => 'Success' ]; // Should not throw any exception $this->handler->validateOrThrow($responseData); $this->assertTrue(true); // Assert that we reach this point } public function testValidateOrThrowWithErrorResponse(): void { $responseData = [ 'code' => 1001, 'message' => 'Authentication failed', 'data' => ['error_detail' => 'Invalid token'] ]; $this->expectException(ApiException::class); $this->expectExceptionMessage('Authentication failed'); $this->handler->validateOrThrow($responseData); } public function testValidateOrThrowWithApiException(): void { $responseData = [ 'code' => 500, 'message' => 'Internal server error' ]; try { $this->handler->validateOrThrow($responseData); $this->fail('Expected ApiException was not thrown'); } catch (ApiException $e) { $this->assertEquals('Internal server error', $e->getMessage()); $this->assertEquals('500', $e->getApiCode()); $this->assertEquals($responseData, $e->getResponseData()); } } public function testValidateOrThrowWithMsgField(): void { $responseData = [ 'code' => 400, 'msg' => 'Bad request' ]; $this->expectException(ApiException::class); $this->expectExceptionMessage('Bad request'); $this->handler->validateOrThrow($responseData); } public function testValidateOrThrowWithMissingMessage(): void { $responseData = [ 'code' => 999 ]; $this->expectException(ApiException::class); $this->expectExceptionMessage('Unknown error'); $this->handler->validateOrThrow($responseData); } public function testHandleAndValidateWorkflow(): void { $successData = [ 'code' => 0, 'message' => 'Operation completed', 'data' => ['id' => 123] ]; // Handle should work $response = $this->handler->handle($successData); $this->assertInstanceOf(ApiResponse::class, $response); // Validate should not throw $this->handler->validateOrThrow($successData); $this->assertTrue(true); $errorData = [ 'code' => 404, 'message' => 'Not found' ]; // Handle should still work $errorResponse = $this->handler->handle($errorData); $this->assertInstanceOf(ApiResponse::class, $errorResponse); $this->assertFalse($errorResponse->isSuccess()); // Validate should throw $this->expectException(ApiException::class); $this->handler->validateOrThrow($errorData); } }