TestCase.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. abstract class TestCase extends Laravel\Lumen\Testing\TestCase
  3. {
  4. protected $token = '5da1beb37d511577b6fc3a9b67052344';
  5. protected $username = 'youwl';
  6. protected $password = 'Mm123654!';//'111111';
  7. /**
  8. * Creates the application.
  9. *
  10. * @return \Laravel\Lumen\Application
  11. */
  12. public function createApplication()
  13. {
  14. return require __DIR__.'/../bootstrap/app.php';
  15. }
  16. /**
  17. * 测试模拟请求
  18. * @param string $method
  19. * @param string $url
  20. * @param array ;
  21. */
  22. public function request(string $method,string $url,array $params=[], $content=null,$need_auth = 1){
  23. $token = '';
  24. if($need_auth) {
  25. $token = !empty($this->token) ? $this->token : $this->getToken();
  26. var_dump($token);
  27. }
  28. $headers = [
  29. 'token' => $token,
  30. 'X-Requested-With'=>'XMLHttpRequest',
  31. 'Content-Type' => 'application/x-www-form-urlencoded',
  32. // 'token' => '6ac7ad9ca379fc2d94e2df02eb457594'
  33. ];
  34. $this->refreshApplication();
  35. $this->setUp();
  36. $server = $this->transformHeadersToServerVars($headers);
  37. $response = $this->call($method,$url,$params,[], [], $server, $content);
  38. print_r('HTTP状态码:'.$response->getStatusCode().PHP_EOL);
  39. print_r('返回值:'.PHP_EOL);
  40. $data = json_decode($response->getContent(),true);
  41. print_r($data);
  42. $this->assertEquals(0,$data['ret']??-1);
  43. echo PHP_EOL.PHP_EOL.json_encode($data,JSON_UNESCAPED_UNICODE);
  44. }
  45. protected function getToken(){
  46. // return 'de5676e2dd0c2666d8b3f135b819d305';
  47. $headers = [
  48. 'X-Requested-With'=>'XMLHttpRequest',
  49. ];
  50. $server = $this->transformHeadersToServerVars($headers);
  51. $response = $this->call('post','/admin/admin-login',['user_name'=>$this->username,'password'=>$this->password],[],[],$server);
  52. if($response->getStatusCode()!=200){
  53. echo '登录失败';exit;
  54. }
  55. $data = json_decode($response->getContent(),true);
  56. if(isset($data['ret']) && $data['ret']!=0){
  57. print_r($data);
  58. exit;
  59. }
  60. return $data['data']['token'];
  61. }
  62. /**
  63. * 覆盖setUp方法 添加对sql查询监听
  64. */
  65. protected function setUp(): void
  66. {
  67. parent::setUp();
  68. sqlDump();
  69. }
  70. }