TestJsonGenerationCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\File;
  5. /**
  6. * 测试JSON生成和文件输出功能
  7. */
  8. class TestJsonGenerationCommand extends Command
  9. {
  10. /**
  11. * 命令名称和签名
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'gameitems:test-json-generation';
  16. /**
  17. * 命令描述
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Test JSON generation and file output functionality';
  22. /**
  23. * 执行命令
  24. */
  25. public function handle()
  26. {
  27. $this->info('开始测试物品配置表JSON生成和文件输出功能...');
  28. // 测试物品配置表生成
  29. $this->info('1. 生成物品配置表...');
  30. $itemsData = GenerateItemsJsonCommand::generateJson(true);
  31. if ($itemsData) {
  32. $this->info(' 物品配置表生成成功!');
  33. $this->info(' 物品数量: ' . count($itemsData['items']));
  34. $this->info(' 生成时间: ' . $itemsData['generated_at']);
  35. } else {
  36. $this->error(' 物品配置表生成失败!');
  37. return 1;
  38. }
  39. // 测试宝箱配置表生成
  40. $this->info('2. 生成宝箱配置表...');
  41. $chestData = GenerateChestJsonCommand::generateJson(true);
  42. if ($chestData) {
  43. $this->info(' 宝箱配置表生成成功!');
  44. $this->info(' 宝箱数量: ' . count($chestData['chest']));
  45. $this->info(' 生成时间: ' . $chestData['generated_at']);
  46. } else {
  47. $this->error(' 宝箱配置表生成失败!');
  48. return 1;
  49. }
  50. // 验证文件是否存在
  51. $this->info('3. 验证配置文件是否存在...');
  52. $itemsJsonPath = 'public/json/items.json';
  53. $chestJsonPath = 'public/json/chest.json';
  54. if (File::exists($itemsJsonPath)) {
  55. $this->info(' 物品配置文件存在: ' . $itemsJsonPath);
  56. $fileSize = File::size($itemsJsonPath);
  57. $this->info(' 文件大小: ' . $this->formatBytes($fileSize));
  58. } else {
  59. $this->error(' 物品配置文件不存在: ' . $itemsJsonPath);
  60. return 1;
  61. }
  62. if (File::exists($chestJsonPath)) {
  63. $this->info(' 宝箱配置文件存在: ' . $chestJsonPath);
  64. $fileSize = File::size($chestJsonPath);
  65. $this->info(' 文件大小: ' . $this->formatBytes($fileSize));
  66. } else {
  67. $this->error(' 宝箱配置文件不存在: ' . $chestJsonPath);
  68. return 1;
  69. }
  70. $this->info('测试完成,所有功能正常!');
  71. return 0;
  72. }
  73. /**
  74. * 格式化字节数为可读格式
  75. *
  76. * @param int $bytes 字节数
  77. * @param int $precision 精度
  78. * @return string 格式化后的字符串
  79. */
  80. protected function formatBytes($bytes, $precision = 2)
  81. {
  82. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  83. $bytes = max($bytes, 0);
  84. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  85. $pow = min($pow, count($units) - 1);
  86. $bytes /= pow(1024, $pow);
  87. return round($bytes, $precision) . ' ' . $units[$pow];
  88. }
  89. }