<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/16
 * Time: 下午11:55
 */
define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));

require_once(BASE_ROOT_PATH . '/fooder.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_upgrade.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_check.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_validator.php');
require_once(BASE_ROOT_PATH . '/helper/area_helper.php');

class areaTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass() {
        Base::run_util();
    }
    public function testProvince()
    {
        $area = new area\upgrade();
        $area->province();
    }

    public function testSafeurl()
    {
        $text = "韩国馆-个人洗护";
        $base64 = base64_encode($text);
        $title = util::base64url_encode($text);

        $xxx = urlencode($title);
        $xxy = urlencode($base64);

        $titlex = util::base64url_decode($title);
        $url = "http://p.lrlz.com/mshop/special?special_id=121&title={$title}";
        $url = urlencode($url);
        $url = urldecode($url);


        $urlx = "http://p.lrlz.com/mshop/special?special_id=121&title={$base64}";
        $urlx= urlencode($urlx);
        $urlx = urldecode($urlx);
    }
    public function testUrl()
    {
        $x = 'http%3A%2F%2Fp.lrlz.com%2Fmshop%2Fspecial%3Fspecial_id%3D450%26is_special%3Dtrue%26title%3D5ZGY5bel5YaF6LStdG9w5qacIDYyOA%2C%2C%26from%3Dsinglemessage%26isappinstalled%3D0';
        $y = urldecode($x);
    }

    public function testCity()
    {
        $area = new area\upgrade();
        $area->city();
    }
    public function testConutry()
    {
        $area = new area\upgrade();
        $area->country();
    }

    public function testExport()
    {
        $area = new area\area_check();
        $result = $area->export();
        $x = json_encode($result,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        wcache('area_2.0', array('data' => serialize($result)), 'mb_area');
    }
    public function testUpdate()
    {
        area_helper::uplast_country();
    }

    public function testValidate()
    {
        $validator = new area\area_validator('area');
        $result = $validator->from_areaid(145);
        $result_name = $validator->from_name('上海','上海市','松江区');
    }

    public function testNosub()
    {
        $validator = new area\area_validator('area');
        $cities = $validator->nosub_city();

        $output = [];
        $input = [];
        foreach ($cities as $city)
        {
            $area_id = $city['area_id'];
            $items = $validator->city($city['area_id']);
            $prov = $items['province'];
            $input[$area_id] = ['其它区'];
            $str_out = "area_id={$area_id} prov_name = {$prov['area_name']} , city_name ={$city['area_name']}";
            $output[] = $str_out;
        }

        $in_json = json_encode($input,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        $out_json = json_encode($output,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    }

    public function testAddCountry()
    {
        $path = BASE_DATA_PATH . '/area/input';
        $content = file_get_contents($path);
        $items = json_decode($content);

        $mod_area = Model('area');
        foreach ($items as $parent_id => $names)
        {
            foreach ($names as $name) {
                $ret = $mod_area->insert(array('area_name' => $name,'area_deep' => 3,'area_parent_id' => $parent_id));
                Log::record("insert aread_id = {$ret},area_name={$name}");
            }
        }
    }

    public function testFixAreadid()
    {
        area_helper::fix_areaid();
    }
    public function testFixAreadinfo()
    {
        area_helper::fix_areainfo();
    }

    public function testReplace()
    {
        $phrase  = "You should eat fruits, vegetables, and fiber every day.";
        $healthy = array("fruits", "vegetables", "fiber");
        $yummy   = array("pizza", "beer", "ice cream");

        $newphrase = str_replace($healthy, $yummy, $phrase);

        $str = "上海	上海市	徐汇区	";
        $x = str_replace(array(" "),array("\t"),$str);

    }
}
