stanley-king 2 gadi atpakaļ
vecāks
revīzija
7a43daaffb
4 mainītis faili ar 302 papildinājumiem un 264 dzēšanām
  1. 61 54
      core/framework/cache/cache.redis.php
  2. 231 0
      test/TestExeclIO.php
  3. 5 1
      test/TestRedis.php
  4. 5 209
      test/TestRefillUtil.php

+ 61 - 54
core/framework/cache/cache.redis.php

@@ -8,7 +8,6 @@ defined('InShopNC') or exit('Access Invalid!');
 class Cacheredis extends Cache
 {
 	private $config;
-	private $type;
 	private $prefix;
 
 	private $master;
@@ -18,7 +17,8 @@ class Cacheredis extends Cache
     {
     	$this->config = C('redis');
     	if (empty($this->config['slave'])) $this->config['slave'] = $this->config['master'];
-    	$this->prefix = $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
+    	$this->prefix = $this->config['prefix'] ?? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
+
         if ( !extension_loaded('redis') ) {
             throw_exception('redis failed to load');
         }
@@ -85,6 +85,7 @@ class Cacheredis extends Cache
                     Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
                 }
             }
+
             if (is_mobile()) {
                 $func = 'connect';
             } else {
@@ -113,6 +114,7 @@ class Cacheredis extends Cache
                     Log::record("CacheRedis init_slave disconnect.",Log::DEBUG);
                 }
             }
+
             if (is_mobile()) {
                 $func = 'connect';
             } else {
@@ -150,12 +152,12 @@ class Cacheredis extends Cache
         $this->handler = $this->slave;
     }
 
+    ///redis function
 	public function get($key, $prefix = '')
     {
 		$this->init_slave();
 		if (!$this->enable) return false;
-		$this->type = $prefix;
-		$value = $this->handler->get($this->_key($key));
+        $value = $this->handler->get($this->_key($key, $prefix));
 
         return unserialize($value);
 	}
@@ -164,14 +166,14 @@ class Cacheredis extends Cache
     {
     	$this->init_master();
     	if (!$this->enable) return false;
-    	$this->type = $prefix;
 
         $value = serialize($value);
-        if(is_int($expire)) {
-            $result = $this->handler->setex($this->_key($key), $expire, $value);
-        }else{
-            $result = $this->handler->set($this->_key($key), $value);
+        if (is_int($expire)) {
+            $result = $this->handler->setex($this->_key($key, $prefix), $expire, $value);
+        } else {
+            $result = $this->handler->set($this->_key($key, $prefix), $value);
         }
+
         return $result;
     }
 
@@ -179,8 +181,7 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $result = $this->handler->set($this->_key($key), $val);
+        $result = $this->handler->set($this->_key($key,$prefix), $val);
 
         return $result;
     }
@@ -189,9 +190,8 @@ class Cacheredis extends Cache
     {
         $this->init_slave();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $result = $this->handler->get($this->_key($key));
 
+        $result = $this->handler->get($this->_key($key,$prefix));
         return $result;
     }
 
@@ -199,8 +199,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key, $prefix);
         $value = $this->handler->incr($key);
         return $value;
     }
@@ -209,8 +209,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key, $prefix);
         $value = $this->handler->incrby($key,$val);
         return $value;
     }
@@ -219,8 +219,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key,$prefix);
         $value = $this->handler->decr($key);
         return $value;
     }
@@ -229,8 +229,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key,$prefix);
         $value = $this->handler->decrby($key,$val);
         return $value;
     }
@@ -239,8 +239,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key,$prefix);
         $result = $this->handler->zIncrBy($key,$value,$member);
         return $result;
     }
@@ -249,8 +249,8 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
-        $key = $this->_key($key);
+
+        $key = $this->_key($key,$prefix);
         $result = $this->handler->zDelete($key,$member);
         return $result;
     }
@@ -259,10 +259,11 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable || !is_array($data) || empty($data)) return false;
-        $this->type = $prefix;
+
         foreach ($data as $key => $value)
         {
-            if ($value[0] == 'exp') {
+            if ($value[0] == 'exp')
+            {
                 $value[1] = str_replace(' ', '', $value[1]);
                 preg_match('/^[A-Za-z_]+([+-]\d+(\.\d+)?)$/',$value[1],$matches);
                 if (is_numeric($matches[1])) {
@@ -271,23 +272,25 @@ class Cacheredis extends Cache
                 unset($data[$key]);
             }
         }
+
         if (count($data) == 1) {
-            $this->handler->hset($this->_key($name), key($data),current($data));
+            $this->handler->hset($this->_key($name,$prefix), key($data),current($data));
         } elseif (count($data) > 1) {
-            $this->handler->hMset($this->_key($name), $data);
+            $this->handler->hMset($this->_key($name,$prefix), $data);
         }
     }
 
-    public function hget($name, $prefix, $key = null) {
+    public function hget($name, $prefix, $key = null)
+    {
         $this->init_slave();
         if (!$this->enable) return false;
-        $this->type = $prefix;
+
         if ($key == '*' || is_null($key)) {
-            return $this->handler->hGetAll($this->_key($name));
+            return $this->handler->hGetAll($this->_key($name,$prefix));
         } elseif (strpos($key,',') != false) {
-            return $this->handler->hmGet($this->_key($name), explode(',',$key));
+            return $this->handler->hmGet($this->_key($name,$prefix), explode(',',$key));
         } else {
-            return $this->handler->hGet($this->_key($name), $key);
+            return $this->handler->hGet($this->_key($name,$prefix), $key);
         }
     }
 
@@ -295,44 +298,45 @@ class Cacheredis extends Cache
     {
         $this->init_master();
         if (!$this->enable) return false;
-        $this->type = $prefix;
+
         if (is_null($key)) {
             if (is_array($name)) {
+                $array = [];
                 return $this->handler->del(array_walk($array, [__CLASS__, '_key']));
             } else {
-                return $this->handler->del($this->_key($name));
+                return $this->handler->del($this->_key($name,$prefix));
             }
         } elseif (is_array($name)) {
             foreach ($name as $key => $value) {
-                $this->handler->hdel($this->_key($name), $key);
+                $this->handler->hdel($this->_key($name,$prefix), $key);
             }
             return true;
         } else {
-            return $this->handler->hdel($this->_key($name), $key);
+            return $this->handler->hdel($this->_key($name,$prefix), $key);
         }
     }
 
-    public function hIncrBy($name, $key, $value)
+    public function hIncrBy($name, $prefix, $key, $value)
     {
         $this->init_master();
         if (!$this->enable) return false;
 
-        $this->type = '';
-        return $this->handler->hIncrBy($this->_key($name), $key, $value);
+        return $this->handler->hIncrBy($this->_key($name, $prefix), $key, $value);
     }
 
-    public function hIncrByFloat($name, $prefix, $key, $num = 1) {
+    public function hIncrByFloat($name, $prefix, $key, $value)
+    {
         $this->init_master();
-        if ($this->hget($name, $prefix,$key) !== false) {
-            $this->handler->hIncrByFloat($this->_key($name), $key, floatval($num));
-        }
+        if (!$this->enable) return false;
+
+        return $this->handler->hIncrByFloat($this->_key($name, $prefix), $key, $value);
     }
 
-    public function rm($key, $type = '') {
+    public function rm($key, $prefix = '') {
     	$this->init_master();
     	if (!$this->enable) return false;
-    	$this->type = $type;
-        return $this->handler->del($this->_key($key));
+
+        return $this->handler->del($this->_key($key, $prefix));
     }
 
     public function clear() {
@@ -341,28 +345,31 @@ class Cacheredis extends Cache
         return $this->handler->flushDB();
     }
 
-	private function _key($str) {
-		return $this->prefix.$this->type.$str;
+	private function _key($str,$type) {
+		return $this->prefix.$type.$str;
 	}
 
-    public function keys($key = '')
+    public function keys($key = '',$prefix = '')
     {
         $this->init_slave();
         if(!isset($key) || empty($key)) {
             $key = '*';
         }
-        return $this->handler->keys($this->_key($key));
+        return $this->handler->keys($this->_key($key, $prefix));
     }
 
-    public function del($key){
+    public function del($key)
+    {
         $this->init_master();
         if (!$this->enable) return false;
         return $this->handler->del($key);
     }
 
-    public function lLen($key) {
+    public function lLen($key)
+    {
         $this->init_slave();
         if (!$this->enable) return false;
+
         return $this->handler->lLen($key);
     }
 

+ 231 - 0
test/TestExeclIO.php

@@ -0,0 +1,231 @@
+<?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 TestExeclIO extends TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        Base::run_util();
+    }
+
+    //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefillUtil::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)
+        {
+            Log::record("{$item[0]}:{$item[1]}", Log::DEBUG);
+
+            $card_no = $cardno_checker($item[0]);
+            $amount = $amount_checker($item[1]);
+
+            if ($card_no != false && $amount != false) {
+                $order_sn = $this->make_sn();
+                $ret = $sender($card_no, $amount, $order_sn);
+                Log::record("{$order_sn}:{$card_no}:{$amount}:{$ret}", Log::DEBUG);
+
+                $order_time = date('Y-m-d H:i:S',time());
+                $row = [$order_sn,$card_no, $amount,$order_time];
+                if($ret) {
+                    $row[] = 'SUCC';
+                } else {
+                    $row[] = 'FAIL';
+                }
+                fputcsv($record_csv,$row);
+            }
+            sleep(1);
+        }
+        fclose($record_csv);
+    }
+
+    private function make_sn()
+    {
+        return mt_rand(1000, 9999)
+            . sprintf('%010d', time())
+            . sprintf('%06d', (float)microtime() * 1000000);
+    }
+
+    public function testDiffFromExecl()
+    {
+        $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;
+            }
+        };
+
+        $differ = function ($left,$right)
+        {
+            $result = [];
+
+            $ll = count($left);
+            $lr = count($right);
+            $i = $j = 0;
+
+            while ($i < $ll && $j < $lr)
+            {
+                if ($left[$i] > $right[$j]) {
+                    $j += 1;
+                } elseif ($left[$i] === $right[$j]) {
+                    $i += 1;
+                    $j += 1;
+                } else {
+                    $result[] = $left[$i];
+                    $i += 1;
+                }
+            }
+
+            for (; $i < $ll; $i++) {
+                $result[] = $left[$i];
+            }
+
+            return $result;
+        };
+
+        $refill_reader = function ($file) use ($field_reader,$cardno_checker,$amount_checker)
+        {
+            if(!file_exists($file)) {
+                Log::record("{$file} not exists.",Log::ERR);
+                return [];
+            }
+
+            $result = [];
+            $items = $field_reader($file);
+            foreach ($items as $item) {
+                $no = $cardno_checker($item[0]);
+                $amount = $amount_checker($item[1]);
+
+                if ($no != false && $amount != false) {
+                    $result[] = "{$no}-{$amount}";
+                }
+            }
+
+            return $result;
+        };
+
+        $all  = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/all.xls");
+        $succ = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/succ.xls");
+
+        sort($all);
+        sort($succ);
+        $result = $differ($all,$succ);
+
+        $csv = fopen(BASE_ROOT_PATH . "/data/upload/refill/diff.csv", 'a+');
+        foreach ($result as $item) {
+            $row = explode('-',$item);
+            fputcsv($csv,$row);
+        }
+        fclose($csv);
+    }
+}

+ 5 - 1
test/TestRedis.php

@@ -68,7 +68,6 @@ class TestRedis extends TestCase
     {
         $mod = Model('member');
         $mobile = $mod->getMemberInfoByID(36490,'member_no');
-
     }
 
     public function testGetgid()
@@ -365,4 +364,9 @@ class TestRedis extends TestCase
     {
 
     }
+
+    public function testSome()
+    {
+
+    }
 }

+ 5 - 209
test/TestRefillUtil.php

@@ -9,8 +9,8 @@ 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');
+require_once(BASE_HELPER_PATH . '/refill/util.php');
+
 
 class TestRefillUtil extends TestCase
 {
@@ -19,213 +19,9 @@ class TestRefillUtil extends TestCase
         Base::run_util();
     }
 
-    //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefillUtil::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)
-        {
-            Log::record("{$item[0]}:{$item[1]}", Log::DEBUG);
-
-            $card_no = $cardno_checker($item[0]);
-            $amount = $amount_checker($item[1]);
-
-            if ($card_no != false && $amount != false) {
-                $order_sn = $this->make_sn();
-                $ret = $sender($card_no, $amount, $order_sn);
-                Log::record("{$order_sn}:{$card_no}:{$amount}:{$ret}", Log::DEBUG);
-
-                $order_time = date('Y-m-d H:i:S',time());
-                $row = [$order_sn,$card_no, $amount,$order_time];
-                if($ret) {
-                    $row[] = 'SUCC';
-                } else {
-                    $row[] = 'FAIL';
-                }
-                fputcsv($record_csv,$row);
-            }
-            sleep(1);
-        }
-        fclose($record_csv);
-    }
-
-    private function make_sn()
+    public function testQueueOrder()
     {
-        return mt_rand(1000, 9999)
-            . sprintf('%010d', time())
-            . sprintf('%06d', (float)microtime() * 1000000);
-    }
-
-    public function testDiffFromExecl()
-    {
-        $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;
-            }
-        };
-
-        $differ = function ($left,$right)
-        {
-            $result = [];
-
-            $ll = count($left);
-            $lr = count($right);
-            $i = $j = 0;
-
-            while ($i < $ll && $j < $lr)
-            {
-                if ($left[$i] > $right[$j]) {
-                    $j += 1;
-                } elseif ($left[$i] === $right[$j]) {
-                    $i += 1;
-                    $j += 1;
-                } else {
-                    $result[] = $left[$i];
-                    $i += 1;
-                }
-            }
-
-            for (; $i < $ll; $i++) {
-                $result[] = $left[$i];
-            }
-
-            return $result;
-        };
-
-        $refill_reader = function ($file) use ($field_reader,$cardno_checker,$amount_checker)
-        {
-            if(!file_exists($file)) {
-                Log::record("{$file} not exists.",Log::ERR);
-                return [];
-            }
-
-            $result = [];
-            $items = $field_reader($file);
-            foreach ($items as $item) {
-                $no = $cardno_checker($item[0]);
-                $amount = $amount_checker($item[1]);
-
-                if ($no != false && $amount != false) {
-                    $result[] = "{$no}-{$amount}";
-                }
-            }
-
-            return $result;
-        };
-
-        $all  = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/all.xls");
-        $succ = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/succ.xls");
-
-        sort($all);
-        sort($succ);
-        $result = $differ($all,$succ);
-
-        $csv = fopen(BASE_ROOT_PATH . "/data/upload/refill/diff.csv", 'a+');
-        foreach ($result as $item) {
-            $row = explode('-',$item);
-            fputcsv($csv,$row);
-        }
-        fclose($csv);
+        refill\util::push_queue_order(1092,'abcdefg',30);
+        $val = refill\util::query_queue_order(1092,'abcdefg');
     }
 }