stanley-king 3 سال پیش
والد
کامیت
eef141b79d
2فایلهای تغییر یافته به همراه118 افزوده شده و 38 حذف شده
  1. 0 38
      test/TestAccount.php
  2. 118 0
      test/TestRefillUtil.php

+ 0 - 38
test/TestAccount.php

@@ -65,43 +65,5 @@ class TestAccount extends TestCase
 
         return $total_amount;
     }
-
-    public function testImportRefill()
-    {
-        $filename = BASE_ROOT_PATH . "/data/upload/recharge.xls";
-        if(!file_exists($filename)) {
-            Log::record("{$filename} not exists.",Log::ERR);
-            return false;
-        }
-        $fileType = PHPExcel_IOFactory::identify($filename);
-        $objReader = PHPExcel_IOFactory::createReader($fileType);
-        $objPHPExcel = $objReader->load($filename);
-
-        foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
-        {
-            foreach ($sheet->getRowIterator() as $row)
-            {
-                $index = $row->getRowIndex();
-                if ($index == 1) continue;
-                $items = [];
-                foreach ($row->getCellIterator() as $cell) {
-                    $items[] = $cell->getValue();
-                }
-                $card_no = intval($items[0]);
-                $amount = $items[1];
-            }
-        }
-    }
-
-    public function testFopenRefill()
-    {
-        $fn = fopen(BASE_ROOT_PATH . "/data/upload/recharge.xls", "r");
-
-        while (!feof($fn)) {
-            $line = trim(fgets($fn));
-        }
-
-        fclose($fn);
-    }
 }
 //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAccount::testLog)( .*)?$/" --test-suffix TestAccount.php /var/www/html/test

+ 118 - 0
test/TestRefillUtil.php

@@ -0,0 +1,118 @@
+<?php
+
+
+use PHPUnit\Framework\TestCase;
+
+define('APP_ID', 'test');
+define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/global.php');
+require_once(BASE_CORE_PATH . '/lrlz.php');
+require_once(BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
+require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
+
+class TestRefillUtil extends TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        Base::run_util();
+    }
+
+    //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefillThird::testRefillFromExecl)( .*)?$/" --test-suffix TestRefillUtil.php /var/www/html/test
+    public function testRefillFromExecl()
+    {
+        $filename = BASE_ROOT_PATH . "/data/upload/refill/cards.xls";
+        if(!file_exists($filename)) {
+            Log::record("{$filename} not exists.",Log::ERR);
+            return false;
+        }
+
+        $field_reader = function ($filename)
+        {
+            $fileType = PHPExcel_IOFactory::identify($filename);
+            $objReader = PHPExcel_IOFactory::createReader($fileType);
+            $objPHPExcel = $objReader->load($filename);
+
+            foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
+            {
+                foreach ($sheet->getRowIterator() as $row)
+                {
+                    $items = [];
+                    foreach ($row->getCellIterator() as $cell) {
+                        $items[] = $cell->getValue();
+                    }
+
+                    yield $items;
+                }
+            }
+        };
+
+        $cardno_checker = function ($card_no)
+        {
+            $card_no = trim($card_no);
+            $ret = preg_match("/^\d{11}$/i",$card_no) > 0;
+            if($ret) {
+                return $card_no;
+            } else {
+                return false;
+            }
+        };
+
+        $amount_checker = function ($amount)
+        {
+            $amount = floatval($amount) + 0.0005;
+            $amount = intval($amount);
+            if($amount > 0) {
+                return $amount;
+            } else {
+                return false;
+            }
+        };
+
+        $mchid = 10239;
+        $buyer_id = 66221;
+        $sender = function ($card_no,$amount,$order_sn) use ($mchid,$buyer_id)
+        {
+            $params = [ 'mchid' => $mchid,
+                'buyer_id' => $buyer_id,
+                'amount' => $amount,
+                'mch_order' => $order_sn,
+                'card_no' => $card_no
+            ];
+
+            $ret = refill\util::push_add($params);
+            return $ret;
+        };
+
+        $record_csv = fopen(BASE_ROOT_PATH . "/data/upload/refill/record.csv", 'a+');
+        $items = $field_reader($filename);
+        foreach ($items as $item)
+        {
+            $card_no = $cardno_checker($item[0]);
+            $amount = $amount_checker($item[1]);
+
+            if ($card_no != false && $amount != false) {
+                Log::record("{$card_no}:{$amount}", Log::DEBUG);
+                $order_sn = $this->make_sn();
+                $ret = $sender($card_no, $amount, $order_sn);
+
+                $row = [$order_sn,$card_no, $amount,time()];
+                if($ret) {
+                    $row[] = 'SUCC';
+                } else {
+                    $row[] = 'FAIL';
+                }
+                fputcsv($record_csv,$row);
+            }
+        }
+        fclose($record_csv);
+    }
+
+    private function make_sn()
+    {
+        return mt_rand(1000, 9999)
+            . sprintf('%010d', time())
+            . sprintf('%06d', (float)microtime() * 1000000);
+    }
+}