| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Response;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Response\ApiResponse;
- class ApiResponseTest extends TestCase
- {
- public function testSuccessResponse(): void
- {
- $data = [
- 'code' => 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']);
- }
- }
|