<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class StarifyTest extends TestCase
{
    protected $token = '9fb69ef93b176ca394101d6231ec6358';

    /**
     * 测试模拟请求
     * @param string $method
     * @param string $url
     * @param array $params
     */
    public function request(string $method,string $url,array $params=[],$needToken=true){
        print_r(PHP_EOL.PHP_EOL.json_encode($params,JSON_UNESCAPED_UNICODE).PHP_EOL);
        if($needToken){
            $token=empty($this->token)?$this->getToken():$this->token;
        }else{
            $token='';
        }
        $headers = [
            'X-Requested-With'=>'XMLHttpRequest',
            'Content-Type' => 'application/x-www-form-urlencoded',
            'token' => $token // AuthCheck的时候会进行刷新赋值
        ];
        $server = $this->transformHeadersToServerVars($headers);
        $response = $this->call($method,$url,$params,[], [], $server);
        print_r('HTTP状态码：'.$response->getStatusCode().PHP_EOL);
        print_r('返回值：'.PHP_EOL);
        $data = json_decode($response->getContent(),true);
//        $this->assertEquals(0,$data['ret']??-1);
        echo PHP_EOL.PHP_EOL.json_encode($data,JSON_UNESCAPED_UNICODE);
        return $data;
    }

    protected function getToken(){
        $response = $this->call('post','/api/app/login',['user_name'=>$this->username,'password'=>$this->password,'app_code'=>'JYGJ','app_key'=>'D0AB3EE73EDD8F70EB23E37231FCBC33']);
        if($response->getStatusCode()!=200){
            echo '登录失败';exit;
        }
        $data = json_decode($response->getContent(),true);
        if(!isset($data['code'])||isset($data['code']) && $data['code']!=0){
            print_r($data);
            exit;
        }
        return $data['data']['api_token'];
    }

    /**
     * 获取用户列表
     * */
    public function testStarifyUserList(){
        $data=[];
        $this->request('get','/api/admin/user-list?page=1&page_size=10&keyword=',$data);
    }

    /**
     * 获取用户列表
     * */
    public function testWaCloudBillList(){
        $data=[];
        $this->request('get','/api/admin/wa-cloud-bill-list?date=&user_id=1000090&page=1&page_size=10&keyword=',$data);
    }

    /**
     * 保存星光用户充值记录
     * */
    public function testSaveStarifyWaCloudBill(){
        $data=[];
        $data['amount']=100;
        $data['type']=1;
        $data['id']=0;
        $data['user_id']=1000090;
        $this->request('post','/api/admin/wa-cloud-bill-save',$data);
    }

}
