| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Response;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Exception\ApiException;
- use SixShop\Wangdian\Response\ApiResponse;
- use SixShop\Wangdian\Response\ResponseHandler;
- class ResponseHandlerTest extends TestCase
- {
- private ResponseHandler $handler;
- protected function setUp(): void
- {
- $this->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);
- }
- }
|