OpenHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Tests\Unit\AppGame;
  3. use Tests\TestCase;
  4. use Uraus\Kku\Request\RequestMatchexchangeOpen;
  5. use Uraus\Kku\Response\ResponseMatchexchangeOpen;
  6. /**
  7. * 获取开放交易物品列表Handler测试
  8. */
  9. class OpenHandlerTest extends TestCase
  10. {
  11. /**
  12. * 测试RequestMatchexchangeOpen请求对象
  13. */
  14. public function testRequestMatchexchangeOpenStructure()
  15. {
  16. $request = new RequestMatchexchangeOpen();
  17. // 测试设置时间戳
  18. $timestamp = time();
  19. $request->setTimes($timestamp);
  20. $this->assertEquals($timestamp, $request->getTimes());
  21. }
  22. /**
  23. * 测试RequestMatchexchangeOpen默认值
  24. */
  25. public function testRequestMatchexchangeOpenDefaultValues()
  26. {
  27. $request = new RequestMatchexchangeOpen();
  28. // 测试默认值
  29. $this->assertEquals(0, $request->getTimes());
  30. }
  31. /**
  32. * 测试ResponseMatchexchangeOpen响应对象
  33. */
  34. public function testResponseMatchexchangeOpenStructure()
  35. {
  36. $response = new ResponseMatchexchangeOpen();
  37. // 测试设置物品ID列表
  38. $itemIds = [1001, 1002, 1003];
  39. $response->setItemId($itemIds);
  40. $responseItemIds = $response->getItemId();
  41. $this->assertCount(3, $responseItemIds);
  42. // 验证物品ID列表内容
  43. $responseArray = iterator_to_array($responseItemIds);
  44. $this->assertEquals($itemIds, $responseArray);
  45. }
  46. /**
  47. * 测试ResponseMatchexchangeOpen空列表
  48. */
  49. public function testResponseMatchexchangeOpenEmptyList()
  50. {
  51. $response = new ResponseMatchexchangeOpen();
  52. // 测试设置空列表
  53. $response->setItemId([]);
  54. $responseItemIds = $response->getItemId();
  55. $this->assertCount(0, $responseItemIds);
  56. }
  57. /**
  58. * 测试ResponseMatchexchangeOpen大量物品ID
  59. */
  60. public function testResponseMatchexchangeOpenLargeList()
  61. {
  62. $response = new ResponseMatchexchangeOpen();
  63. // 生成大量物品ID
  64. $itemIds = range(1001, 1100); // 100个物品ID
  65. $response->setItemId($itemIds);
  66. $responseItemIds = $response->getItemId();
  67. $this->assertCount(100, $responseItemIds);
  68. // 验证第一个和最后一个物品ID
  69. $responseArray = iterator_to_array($responseItemIds);
  70. $this->assertEquals(1001, $responseArray[0]);
  71. $this->assertEquals(1100, $responseArray[99]);
  72. }
  73. /**
  74. * 测试protobuf序列化和反序列化
  75. */
  76. public function testProtobufSerialization()
  77. {
  78. // 创建请求对象
  79. $request = new RequestMatchexchangeOpen();
  80. $request->setTimes(time());
  81. // 序列化
  82. $serialized = $request->serializeToString();
  83. $this->assertNotEmpty($serialized);
  84. // 反序列化
  85. $newRequest = new RequestMatchexchangeOpen();
  86. $newRequest->mergeFromString($serialized);
  87. $this->assertEquals($request->getTimes(), $newRequest->getTimes());
  88. // 创建响应对象
  89. $response = new ResponseMatchexchangeOpen();
  90. $itemIds = [1001, 1002, 1003];
  91. $response->setItemId($itemIds);
  92. // 序列化响应
  93. $serializedResponse = $response->serializeToString();
  94. $this->assertNotEmpty($serializedResponse);
  95. // 反序列化响应
  96. $newResponse = new ResponseMatchexchangeOpen();
  97. $newResponse->mergeFromString($serializedResponse);
  98. $responseArray = iterator_to_array($newResponse->getItemId());
  99. $this->assertEquals($itemIds, $responseArray);
  100. }
  101. }