UserTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. use Laravel\Lumen\Testing\DatabaseMigrations;
  3. use Laravel\Lumen\Testing\DatabaseTransactions;
  4. class UserTest extends TestCase
  5. {
  6. protected $token = '40d45add515486ab70a8a93336a835b2';
  7. /**
  8. * 测试模拟请求
  9. * @param string $method
  10. * @param string $url
  11. * @param array $params
  12. */
  13. public function request(string $method,string $url,array $params=[],$needToken=true){
  14. print_r(PHP_EOL.PHP_EOL.json_encode($params,JSON_UNESCAPED_UNICODE).PHP_EOL);
  15. if($needToken){
  16. $token=empty($this->token)?$this->getToken():$this->token;
  17. }else{
  18. $token='';
  19. }
  20. $headers = [
  21. 'X-Requested-With'=>'XMLHttpRequest',
  22. 'Content-Type' => 'application/x-www-form-urlencoded',
  23. 'token' => $token // AuthCheck的时候会进行刷新赋值
  24. ];
  25. $server = $this->transformHeadersToServerVars($headers);
  26. $response = $this->call($method,$url,$params,[], [], $server);
  27. print_r('HTTP状态码:'.$response->getStatusCode().PHP_EOL);
  28. print_r('返回值:'.PHP_EOL);
  29. $data = json_decode($response->getContent(),true);
  30. // $this->assertEquals(0,$data['ret']??-1);
  31. echo PHP_EOL.PHP_EOL.json_encode($data,JSON_UNESCAPED_UNICODE);
  32. return $data;
  33. }
  34. protected function getToken(){
  35. $response = $this->call('post','/api/user/login',['username'=>$this->username,'password'=>$this->password,'app_code'=>'JYGJ','app_key'=>'D0AB3EE73EDD8F70EB23E37231FCBC33']);
  36. if($response->getStatusCode()!=200){
  37. echo '登录失败';exit;
  38. }
  39. $data = json_decode($response->getContent(),true);
  40. if(!isset($data['code'])||isset($data['code']) && $data['code']!=0){
  41. print_r($data);
  42. exit;
  43. }
  44. return $data['data']['token'];
  45. }
  46. /**
  47. * A basic test example.
  48. *
  49. * @return void
  50. */
  51. public function testUserLogin()
  52. {
  53. $data=[];
  54. $data['user_name']='mp_admin';
  55. $data['password']='Mm123654!';
  56. $this->request('post','/api/admin/user/admin-login',$data,false);
  57. }
  58. public function testUserLogout()
  59. {
  60. $data=[
  61. 'api_token'=>'76884827a184f909ca778f9373d19dd4'
  62. ];
  63. $this->request('post','/api/app/user/logout',$data);
  64. }
  65. public function testGetUser()
  66. {
  67. $data=[
  68. ];
  69. $ret=$this->request('get','/api/admin/user/get-admin-user',$data);
  70. print_r($ret);die;
  71. $this->assertTrue(true);
  72. }
  73. public function testDef()
  74. {
  75. $data=[
  76. ];
  77. $this->request('get','/',$data);
  78. }
  79. public function testGetProductByTypeIds(){
  80. $data=[
  81. 'type_id'=>[3]
  82. ];
  83. $ret=$this->request('get','/api/product/get-list-by-type',$data);
  84. print_r($ret);die;
  85. $this->assertTrue(true);
  86. }
  87. }