token_create_test.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require __DIR__ . '/../vendor/autoload.php';
  5. use Services\TokenCreateService;
  6. if (class_exists(Dotenv\Dotenv::class) && file_exists(__DIR__ . '/../.env')) {
  7. Dotenv\Dotenv::createImmutable(dirname(__DIR__), null, true)->safeLoad();
  8. }
  9. $dsn = $_ENV['DB_DSN'] ?? (function () {
  10. $host = $_ENV['DB_HOST'] ?? 'localhost';
  11. $port = $_ENV['DB_PORT'] ?? '5432';
  12. $name = $_ENV['DB_NAME'] ?? 'postgres';
  13. return "pgsql:host={$host};port={$port};dbname={$name}";
  14. })();
  15. $dbUser = $_ENV['DB_USER'] ?? 'postgres';
  16. $dbPass = $_ENV['DB_PASSWORD'] ?? '';
  17. $GLOBALS['pdo'] = new PDO($dsn, $dbUser, $dbPass);
  18. $GLOBALS['pdo']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. $payloadFile = $argv[1] ?? null;
  20. $cprIdArg = $argv[1] ?? null;
  21. if ($cprIdArg === null || !ctype_digit((string)$cprIdArg)) {
  22. fwrite(STDERR, "Usage: php ./test/token_create_test.php <cpr_id> [payload.json]" . PHP_EOL);
  23. exit(1);
  24. }
  25. $cprId = (int)$cprIdArg;
  26. $payloadFile = $argv[2] ?? null;
  27. if ($payloadFile) {
  28. if (!is_file($payloadFile)) {
  29. fwrite(STDERR, "[error] Payload file '{$payloadFile}' not found" . PHP_EOL);
  30. exit(1);
  31. }
  32. $data = json_decode(file_get_contents($payloadFile), true);
  33. if (!is_array($data)) {
  34. fwrite(STDERR, "[error] Invalid JSON payload" . PHP_EOL);
  35. exit(1);
  36. }
  37. } else {
  38. $data = [
  39. 'token_commodities_amount' => 1,
  40. 'token_commodities_value' => 100,
  41. 'token_uf' => 'SP',
  42. 'token_city' => 'SAO PAULO',
  43. 'token_content' => 'testetesteteste',
  44. 'token_flag' => 'a',
  45. 'wallet_id' => 1,
  46. 'chain_id' => 1,
  47. 'commodities_id' => 1,
  48. 'cpr_id' => $cprId,
  49. 'user_id' => 1,
  50. ];
  51. }
  52. $data['cpr_id'] = $cprId;
  53. $required = [
  54. 'token_commodities_amount',
  55. 'token_commodities_value',
  56. 'token_uf',
  57. 'token_city',
  58. 'token_content',
  59. 'token_flag',
  60. 'wallet_id',
  61. 'chain_id',
  62. 'commodities_id',
  63. 'cpr_id',
  64. 'user_id',
  65. ];
  66. foreach ($required as $field) {
  67. if (!array_key_exists($field, $data)) {
  68. fwrite(STDERR, "[error] Missing required field: {$field}" . PHP_EOL);
  69. exit(1);
  70. }
  71. }
  72. $service = new TokenCreateService();
  73. try {
  74. $result = $service->createToken(
  75. (int)$data['token_commodities_amount'],
  76. (int)$data['token_commodities_value'],
  77. (string)$data['token_uf'],
  78. (string)$data['token_city'],
  79. (string)$data['token_content'],
  80. (string)$data['token_flag'],
  81. (int)$data['wallet_id'],
  82. (int)$data['chain_id'],
  83. (int)$data['commodities_id'],
  84. (int)$data['cpr_id'],
  85. (int)$data['user_id']
  86. );
  87. echo json_encode([
  88. 'status' => 'ok',
  89. 'data' => $result,
  90. ], JSON_PRETTY_PRINT) . PHP_EOL;
  91. } catch (Throwable $e) {
  92. fwrite(STDERR, '[error] ' . $e->getMessage() . PHP_EOL);
  93. exit(1);
  94. }