<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/10/7
 * Time: 下午1:03
 */

define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
require_once(BASE_ROOT_PATH . '/fooder.php');
require_once(BASE_ROOT_PATH . '/helper/search/server.php');
require_once(BASE_ROOT_PATH . '/helper/search/processor.php');
require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
require_once(BASE_ROOT_PATH . '/helper/search/util.php');
require_once(BASE_ROOT_PATH . '/helper/category_helper.php');
require_once(BASE_ROOT_PATH . '/helper/search/searcher.php');
require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');

define('MOBILE_SERVER',true);
define('TIME_STAMP',time());


class TestSearch extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }

    public function testFilter()
    {
        $words = search\filter::filter('哈English 文化');
    }

    public function testInit()
    {
        $dict = new search\goods_dict();
        $dict->init();
    }
    public function testHot()
    {
        $dict = new search\category_dict();
    }

    public function testBrand()
    {
        $dict = new search\brand_dict();
    }

    public function testSearcher()
    {
        $searcher = new search\searcher();
        $searcher->init();
        for($i = 0; $i < 10; ++$i) {
            $result = $searcher->get_result(array('keyword' => '',   'brand_id' => 0,'hot_id' => 11,'order' => 'price', 'sort' => 'desc', 'page_no' => 1, 'page_size' => 10));
            $result = $searcher->get_word('茱莉');
        }
//        $result = $searcher->match_price(array('price' => 9.27,'page_no' => 1,'page_size' => 10));
//        $x = 100;
    }


    public function testRequest()
    {
        for($i = 0; $i < 1000; ++$i) {
            $result = search\search_client::instance()->get_result(array('keyword' => 'bb'));
        }
    }

    public function testBrandSpecia()
    {
        $brands = new brands_special();
        $ret = $brands->info(4);
    }

    public function testGoods()
    {
        $mod = Model('goods');
        $items = $mod->getGoodsOnlineList(array(), "*", '', '', false);

        $mod_common = Model("goods_common");
        $items = $mod->field("*")->limit(false)->select();

    }

    public function testUasort()
    {
        $words = [];
        $words[] = array("add_time" =>  104,"count" => 1);
        $words[] = array("add_time" =>  100,"count" => 1);
        $words[] = array("add_time" =>  300,"count" => 1);

        $ret = uasort($words,'comp_time');
        foreach ($words as $key => $val) {
            echo $key;
        }
    }

    public function testHotsearch()
    {
        $x = @explode(',',C('hot_search'));//热门搜索
        $goodslist = Model('goods_browse')->getGuessLikeGoods(36490,10);
    }

    public function testUnion()
    {
        $a1 = array(1,3,5,7,9);
        $a2 = array(1,2,3,5,8,9);

        $result = algorithm::set_union($a1,$a2);
    }

    public function testUnionx()
    {
        $x = '满500减50 送<a href="http://192.168.0.200/shop/index.php?act=goods&op=index&goods_id=4550" title="妮维雅 冰爽透明防晒气雾 200ml 200ml" target="_blank">[赠品]</a>';
        $y = stripslashes($x);
        preg_match('#(.*?)<a href=".*?"\s+title="(.*?)".*?>.*?</a>#s',$x,$matches);
        $host = $matches[2];
    }

    public function testParams()
    {
        $params = array("xxx" => 1);
        $params = json_encode($params);
        $data = array('action' => 'groupbuy','params' => array('pay_sn' => 'fdfdsafdas'));
        $data = json_encode($data);
    }

    public function testPay()
    {
        $pri_key = BASE_DATA_PATH .'/api/alipay/key/rsa_private_key.pem';
        $pub_key = BASE_DATA_PATH.'/api/alipay/key/alipay_public_key.pem';

//    $xxx = 'gNvKQd0GXULuvr%2F1FKjQrKVI%2BeT%2B8qz2ohSohfkzn%2Fx5ajMUdDD1zBXsxbv9%2FJZbaLq7KihhJmlb28E02S6hI9OlG7f7%2BTJ%2FTtrh8Xy2%2FYiU2KNpjtowS%2FM3io23lgfyQgIEQ0xIpyMZg4NRdaoW6thnrgQzs%2B9rY57iFgdG%2B24%3D';
//    $xy = urldecode($xxx);

        //$pri = openssl_pkey_get_private($pri_key);
//    $pub = openssl_pkey_get_private($pri_key);

//    $pri = openssl_pkey_get_private($pri_key);
//    $err = openssl_error_string();
//    $pub = openssl_pkey_get_private($pri_key);
//    $err = openssl_error_string();

        $key = file_get_contents($pri_key);
        $pri = openssl_get_privatekey($key);
        $err = openssl_error_string();
//
        $key = file_get_contents($pub_key);
        $pub = openssl_get_publickey($key);
        $err = openssl_error_string();

        $body = 'discount=0.00&payment_type=1&subject=%E5%AE%9E%E7%89%A9%E8%AE%A2%E5%8D%95_580507223514222365&trade_no=2016012721001004800042946454&buyer_email=13911129867&gmt_create=2016-01-27+15%3A25%3A29&notify_type=trade_status_sync&quantity=1&out_trade_no=580507223514222365&seller_id=2088121219613123&notify_time=2016-01-27+15%3A25%3A29&body=%E5%95%86%E5%93%81%E8%AF%A6%E6%83%85&trade_status=WAIT_BUYER_PAY&is_total_fee_adjust=Y&total_fee=8.00&seller_email=napheir.ao%40lrlz.com&price=8.00&buyer_id=2088202332994802&notify_id=66af3b99b765d046d2d533eaf6558fcm68&use_coupon=N';

        //$sign= 'KR85bvz7AJzELDjpqgbSKydwk8B2HNrqN3QNS%2Bpb%2BlJ3e5i2jOiLsL6MVqBR1kZDLzox%2BzA9ld3ru2xvC%2Bk1JXFtuLAkPOx%2BdUFym7Qa8AI40xAZLkl3cx3moREvTH0v0QNHUgrf2s2NY1FYmc7FrQAUEMyKqEjFbNE9xcPIG%2Fo%3D';
        $body = 'discount=0.00&payment_type=1&subject=%E5%AE%9E%E7%89%A9%E8%AE%A2%E5%8D%95_280507226197761365&trade_no=2016012721001004800047345139&buyer_email=13911129867&gmt_create=2016-01-27+16%3A10%3A55&notify_type=trade_status_sync&quantity=1&out_trade_no=280507226197761365&seller_id=2088121219613123&notify_time=2016-01-27+16%3A10%3A55&body=%E5%95%86%E5%93%81%E8%AF%A6%E6%83%85&trade_status=WAIT_BUYER_PAY&is_total_fee_adjust=Y&total_fee=8.00&seller_email=napheir.ao%40lrlz.com&price=8.00&buyer_id=2088202332994802&notify_id=44d5cc359f27f5cb6e31fb10f480fb0m68&use_coupon=N';
        openssl_sign($body, $signed, $pri);

        $res = openssl_verify($body, $signed, $pub);
        $err = openssl_error_string();

        $s = base64_encode($signed);
        $s = urlencode($s);

        $sign='lnxpRmnHsaeUoPgtCrGm%2FtWohi3ORJk85q9Ic6X4c10q9O%2FASMXA2Z%2BzAH9%2BgPZAwsMywUA9O4HQ3ZCCrSpPhzJ8hEQ8Dc2SsMnvmeY6UqA7Zi6MZBTSeU1AuV3IV0tN4DTpnuk9ceetwQhNEwCiRo9vjrC%2B0TTHGfXb94OfJuM%3D';
        $signx = urldecode($sign);
        $signy = base64_decode($signx);

        $res = openssl_verify($body, $signy, $pub);
        $err = openssl_error_string();
//
//
//    $signed = base64_encode($signed);
//    $signed = urlencode($signed);
//
//    $body = 'body=%E5%95%86%E5%93%81%E8%AF%A6%E6%83%85&buyer_email=bestcoolbear%40163.com&buyer_id=2088402949754402&discount=0.00&gmt_create=2015-12-31+14%3A51%3A41&gmt_payment=2015-12-31+14%3A51%3A41&is_total_fee_adjust=N&notify_id=35ceee6a4348f21bb27838770223ad0j34&notify_time=2015-12-31+14%3A51%3A41&notify_type=trade_status_sync&out_trade_no=150504888692984277&payment_type=1&price=0.01&quantity=1&seller_email=napheir.ao%40lrlz.com&seller_id=2088121219613123&subject=%E5%AE%9E%E7%89%A9%E8%AE%A2%E5%8D%95_150504888692984277&total_fee=0.01&trade_no=2015123121001004400035472261&trade_status=TRADE_SUCCESS&use_coupon=N';
//    $data = 'gNvKQd0GXULuvr%2F1FKjQrKVI%2BeT%2B8qz2ohSohfkzn%2Fx5ajMUdDD1zBXsxbv9%2FJZbaLq7KihhJmlb28E02S6hI9OlG7f7%2BTJ%2FTtrh8Xy2%2FYiU2KNpjtowS%2FM3io23lgfyQgIEQ0xIpyMZg4NRdaoW6thnrgQzs%2B9rY57iFgdG%2B24%3D';
//    openssl_sign($body, $sig, $pri);
//    $sig64 = base64_encode($sig);
//    $res = openssl_verify($body, $sig, $pub);
//    $err = openssl_error_string();
    }

    public function testSign()
    {
        //$body= 'body=order_sn=8000000000295701&buyer_email=13911129867&buyer_id=2088202332994802&discount=0.00&gmt_create=2016-10-29 20:51:36&gmt_payment=2016-10-29 20:51:37&is_total_fee_adjust=N&notify_id=0960c111697dbcdfbedad9ed94625adm6a&notify_time=2016-10-29 20:51:38&notify_type=trade_status_sync&out_trade_no=650531089488891490&payment_type=1&price=1.80&quantity=1&seller_email=napheir.ao@lrlz.com&seller_id=2088121219613123&subject=实物订单_650531089488891490&total_fee=1.80&trade_no=2016102921001004800260656982&trade_status=TRADE_SUCCESS&use_coupon=N';

        $body = "xxxxffff";

        $pri_key = BASE_DATA_PATH .'/api/alipay/key/rsa_private_key.pem';
        $pub_key = BASE_DATA_PATH.'/api/alipay/key/alipay_public_key.pem';

        $key = file_get_contents($pri_key);
        $pri = openssl_get_privatekey($key);
        openssl_sign($body, $signed, $pri);
        $sign = base64_encode($signed);

        $key = file_get_contents($pub_key);
        $pub = openssl_get_publickey($key);

        $res = openssl_verify($body, $signed, $pub);
    }

    public function testB64()
    {
        $pub_key = BASE_DATA_PATH.'/api/alipay/key/alipay_public_key.pem';

        $key = file_get_contents($pub_key);
        $pub = openssl_get_publickey($key);
        $err = openssl_error_string();

        $data = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL+I5/3VVhfpc3TmsUjuwc8mrZycavjuE4MV9Z+EXDvPy55PAYjDvKfMtRMCsJYgO4R0fyyb1OhvglyXUKLFajxOhUm/4K2hnI1E/+8zPbY0CU5Osr4C9sxWHtY66ugTx+O4W3e4CxYTfHn+C6EuiYJ2DWJig+Obphd8CPFYFzLxAgMBAAECgYA+og9zEytXIHEv/ixlNCZOjlBhkUjt5DSfPjQXGNpseLQWLbHLvm5X1Po1oECMpzevRcU8mizSYXyYuKaWw8XMJ7/CC6A8fSBdlUeLEfTfisurEnzeUsal2K/n+WAAFj+TUncnqYtEqCCT+9c4jVkfik7FNxjbcio9p27QKDzYwQJBAOlWs593knByuSFOVnIiphhKlZy+6MUxquxhP+3NlXVaTCHHbBl8xt3fLqLmrXPUHvAt39wJ8fAvLDuNUQgP2gkCQQDSIt7bZi2hmzqfXK9rqVyJ16o0Hwqa8Z6PYhodSSsf61h3+wxdEpDFxIV9JnPWBPhCxcX+d1VddyLZacXbKuupAkBpwFesID8II5Zv19cp5zYrsDHaVlOce4QhmXmlxxTDmOcEMCN38asXhzzVq4JVCn/zDnd0fDVgS6DaZJOi+bwxAkAJg2iheCvCsDtkMZcDgcRdvTTIbUtWnm+2QBO8la5tIIN90xDJOejx+yar9syxuMHgjAGdtptXwugB/cbmWDgZAkEAiRoMv6p7WKNU34kZqpvXMIhN6hUihquVFZDJ7wiBr1tCLw/kW3uhciCViM68FgLStuefTfssgr9+oHVw/o4GjA==';
        $pem = "-----BEGIN PUBLIC KEY-----\n" . $data . "-----END PUBLIC KEY-----\n";
        $x = openssl_pkey_get_private($pem);
        $y = openssl_error_string();
    }

    public function testLocalSign()
    {

//        $pri_key = BASE_DATA_PATH.'/api/alipay/key/rsa_private_key.pem';
//        $pub_key = BASE_DATA_PATH.'/api/alipay/key/rsa_public_key.pem';

        $body ='body=order_sn=8000000000295404&buyer_email=13911129867&buyer_id=2088202332994802&discount=0.00&gmt_create=2016-10-29 20:24:18&gmt_payment=2016-10-29 20:24:18&is_total_fee_adjust=N&notify_id=f29b67caf1da662d7210f04b3675f3am6a&notify_time=2016-10-29 23:52:32&notify_type=trade_status_sync&out_trade_no=850531087853085490&payment_type=1&price=0.76&quantity=1&seller_email=napheir.ao@lrlz.com&seller_id=2088121219613123&subject=实物订单_850531087853085490&total_fee=0.76&trade_no=2016102921001004800260616422&trade_status=TRADE_SUCCESS&use_coupon=N';
        $sign ='WG+9QtqYOlmxVRzTVyso2toN1wOxXz4aBUS7ZRL7nzvDCdXaZRU3I2bN8EZ2pcag8DiU6eUXI99YY0MaTjnAJgGGESzd5S1KHDSg0ZNdsZueaLhmYQNGpap7jyRxBSDNIR+tEPU5FzgDivU4uK4f5PWu0FiOd9jOfXLIkGneRxg=';

        $pub_key = '/Users/stanley-king/Desktop/payment/alipay/key/alipay_public_key.pem';

//        $pri_key = '/Users/stanley-king/.ssh/rsa_private_key.pem';
//        $pub_key = '/Users/stanley-king/.ssh/rsa_public_key.pem';

//        $pri_key = '/Users/stanley-king/work/PHPProject/rsa_private_key.pem';
//        $pub_key = '/Users/stanley-king/work/PHPProject/rsa_public_key.pem';

//        $key = file_get_contents($pri_key);
//        $pri = openssl_get_privatekey($key);
//        openssl_sign($body, $signed, $pri);
//        $sign = base64_encode($signed);

        $key = file_get_contents($pub_key);
        $pub = openssl_get_publickey($key);

        $signed = base64_decode($sign);

        $res = openssl_verify($body, $signed, $pub);
    }

    public function testCmbpay()
    {
        $x = 'BranchID=0021&CoNo=006438&BillNo=5320089577&Amount=2.05&Date=20161030&ExpireTimeSpan=30&MerchantUrl=http%3A%2F%2F121.43.114.153%2Fmobile%2Fcmbpay_notify.php&MerchantPara=pay_sn%3A610531141658503490%7Corder_sn%3A8000000000298701%7Cmember_id%3A36490&MerchantCode=%7C4xKshccAHleC65HWnU828KX1n5MtZAcd1Ma4XfVTwPibT3QJLqgRwZhxSclZyyV8WE%2Fkv8jT01eYd1y%2Aed6SsJHw2BuxSoIqGeXL4b0HMTqYaZ%2AcGEyJ4n8nVYW7vDn%2AM%2FvnDRoCuahqmft0i2suJlgZP07w36WzDPmN9SKRd60%2A4HCG2GdSy0gJ1f8tcW9C7qW6ltC7XzWlZ9yJnuKeDSJ4JHzSy24invATyz8qOuehypiWCCRLYouNCJorAqFETHZgB5kfEEJzH%2AODIf24Fywe%2Amuib96thBMGwJ1A%2AuLl4FqlchHx6QYCUyxxRNcmnF7YPkfcQs43g4ekaiHKudxmAZ%2AwJMKxPRen3g9TUyXvqSW%2FpxyPELKlKJQYUGodBn6kZN0yQ7Pw%7C4fd51f4fe55b76e5e17538d7969b2fdd8f0b8e67&MerchantRetUrl=http%3A%2F%2F121.43.114.153%2Fmobile%2Findex.php&MerchantRetPara=act%3Dpay_return%26op%3Dcmbpay%26pay_sn%3D610531141658503490%26order_sn%3D8000000000298701%26member_id%3D36490';
        $y = urldecode($x);
    }

    public function testPubkey()
    {
        $file = fopen('/Users/stanley-king/work/PHPProject/public.key','r');
        $data = fread($file,1024);
        fclose($file);
        $base64 = base64_encode($data);
    }

    public function testPublicKey()
    {
        $CMPPAY_ONLINE = true;
        if($CMPPAY_ONLINE == true) {
            $pub_key = 'MIGJAoGBALKsktbh7j9O9pM0p7qnxxImgODqxjpiT7Xl2bvZCywJtwsNI6CchqAagOYGJjG0NZsnjFunTw5YM9TD5KxsUOILAL6IaNMH/fWREhVjkUDJ4CYtLWlKozElvXRp1iZxf66yHHhN4t7TE5S9NWpEBSn37TEfFLU99Go1WReI1XN1AgMBAAE=';
        } else {
            $pub_key = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALuUIwMGhvbpCwYzKCMzvSMQiLAAj5y74cN09N1TXVONPWhvLWkuzEPSd1ogPJLWiVyEG7gEIBT3zTlCV+NMou0CAwEAAQ==';
        }

        $pk = chunk_split($pub_key, 64, "\n");
        $pk = "-----BEGIN PUBLIC KEY-----\n{$pk}-----END PUBLIC KEY-----\n";

        $pkid = openssl_pkey_get_public($pub_key);
    }
    public function testNull()
    {
        $x = 'a:1:{s:4:"item";a:6:{s:25:"s61_05308797997352196.png";a:3:{s:5:"image";s:25:"s61_05308797997352196.png";s:4:"type";s:7:"special";s:4:"data";s:2:"53";}s:25:"s61_05308798368461066.png";a:3:{s:5:"image";s:25:"s61_05308798368461066.png";s:4:"type";s:7:"special";s:4:"data";s:2:"49";}s:25:"s61_05308798643188523.png";a:3:{s:5:"image";s:25:"s61_05308798643188523.png";s:4:"type";s:7:"special";s:4:"data";s:2:"50";}s:25:"s61_05308798882646585.png";a:3:{s:5:"image";s:25:"s61_05308798882646585.png";s:4:"type";s:7:"special";s:4:"data";s:2:"51";}s:25:"s61_05308799439510460.png";a:3:{s:5:"image";s:25:"s61_05308799439510460.png";s:4:"type";s:7:"special";s:4:"data";s:2:"46";}s:25:"s61_05308799642656955.png";a:3:{s:5:"image";s:25:"s61_05308799642656955.png";s:4:"type";s:7:"special";s:4:"data";s:2:"47";}}}';
        $y = unserialize($x);
    }

    public function testDefine()
    {
        $i = 0;
        while ( $i < 5) {
            sleep(1);
            echo "{i} " . TIME_STAMP . "\n";
            ++$i;
        }
    }

    public function testNotin()
    {
        $x = array(1,3,4,9,8,5);
        $y = array(3,2,3,4,9,8);

        $z = algorithm::not_in($x,$y);
    }
    public function testPasswd()
    {
        $x = md5('panda@qaz@makeup');
        $y = md5('xmmz@20161104');
    }

    public function testTime()
    {
        list($usec, $sec) = explode(" ", microtime());

        $t = time();
        $x = intval(microtime(true) * 10000);
        $y = intval(microtime(true) * 10000);
        $k = $y - $x;
        $z = microtime(true);
        $w = microtime(true);

        echo ($y - $x);
    }
}
