TestSSH2.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  9. require_once(BASE_HELPER_PATH . '/vendor/yifutongtax.php');
  10. function my_ssh_disconnect($reason, $message, $language) {
  11. printf("Server disconnected with reason code [%d] and message: %s\n",
  12. $reason, $message);
  13. }
  14. class TestSSH2 extends TestCase
  15. {
  16. public static function setUpBeforeClass(): void
  17. {
  18. Base::run_util();
  19. }
  20. public function testConnect()
  21. {
  22. $connection = ssh2_connect('121.89.223.81', 22);
  23. if (ssh2_auth_pubkey_file($connection, 'root',
  24. './ssh/id_rsa.pub',
  25. './ssh/id_rsa', '')) {
  26. echo "Public Key Authentication Successful\n";
  27. } else {
  28. die('Public Key Authentication Failed');
  29. }
  30. $sftp = ssh2_sftp($connection);
  31. if (!$sftp) {
  32. die('无法初始化 SFTP');
  33. }
  34. $remote_file = "ssh2.sftp://".intval($sftp)."/nfs/upload/yl.tar";
  35. $local_file = "./ssh/yl.tar";
  36. if (!copy($remote_file,$local_file)) {
  37. die('文件上传失败');
  38. }
  39. ssh2_disconnect($connection);
  40. }
  41. //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestSSH2::testConnectYFT)( .*)?$/" --test-suffix TestSSH2.php /var/www/html/test
  42. public function testConnectYFT()
  43. {
  44. $yft = new vendor\yifutongtax();
  45. $yft->handle();
  46. }
  47. private function down_file()
  48. {
  49. }
  50. public function tstParaseFile()
  51. {
  52. }
  53. public function testTime()
  54. {
  55. $cur_time = time();
  56. $a = strftime("%Y%m%d_000110089970029",$cur_time);
  57. Log::record("$a", Log::DEBUG);
  58. $start_date = strtotime('20241211164754');
  59. Log::record("$start_date", Log::DEBUG);
  60. $a = strftime("%Y-%m-%d %H:%M:%S",$start_date);
  61. }
  62. public function testParaseFile()
  63. {
  64. $yft = new vendor\yifutongtax();
  65. $file = BASE_ROOT_PATH . '/test/ssh/20241209_000110089970029.txt';
  66. $file = BASE_ROOT_PATH . '/test/ssh/20241211_000110089970029.txt';
  67. $yft->parese_file($file);
  68. }
  69. public function testModifyTime()
  70. {
  71. $yft = new vendor\yifutongtax();
  72. $file = BASE_ROOT_PATH . '/test/ssh/20241211_000110089970029.txt';
  73. $yft->modify_time($file);
  74. }
  75. //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestSSH2::testParase)( .*)?$/" --test-suffix TestSSH2.php /var/www/html/test
  76. public function testParase()
  77. {
  78. #卡号|订单号(唯一的)| 状态(只给成功)| 面值 |运营商流水凭证| 手机号| 创建时间|交易成功时间
  79. #5|3456.00
  80. $text = '9629980601009991|62241211164700945483|成功|938|6200945495|13391703053|20241211164725|20241211164754';
  81. $matcher = function ($input)
  82. {
  83. $data = explode('|', $input);
  84. $count = count($data);
  85. if ($count === 8) {
  86. return [
  87. // 'bank_card' => trim($data[0]),
  88. 'mch_order' => trim($data[1]),
  89. 'status' => 'SUCCESS',
  90. 'amount' => 1,
  91. // 'org_quality' => 2,
  92. // 'quality' => 2,
  93. 'quantity' => intval(trim($data[3])),
  94. 'official_sn' => trim($data[4]),
  95. 'card_no' => trim($data[5]),
  96. 'order_time' => time()
  97. // 'order_time' => strtotime(trim($data[6])),
  98. // 'finish_time' => strtotime(trim($data[7]))
  99. ];
  100. }
  101. elseif($count === 2) {
  102. return [
  103. 'count' => intval(trim($data[0])),
  104. 'total' => intval(trim($data[1]))
  105. ];
  106. }
  107. else {
  108. return false;
  109. }
  110. };
  111. $result = $matcher($text);
  112. $minfo = $this->mchinfo(1092);
  113. $params = array_merge($result,$minfo,['notify_url' => '']);
  114. $mch_order = $params['mch_order'];
  115. refill\util::write_yifutong_order($mch_order,$params);
  116. $val = refill\util::read_yifutong_order($mch_order);
  117. refill\util::push_add($params);
  118. }
  119. public function testGetMchinfo()
  120. {
  121. $minfo = $this->mchinfo(1092);
  122. }
  123. private function mchinfo($mchid)
  124. {
  125. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  126. if(empty($mchinfo)) {
  127. return false;
  128. }
  129. return ['mchid' =>$mchid, 'buyer_id' => intval($mchinfo['admin_id'])];
  130. }
  131. }