PhpunitFunctionCustomize.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/5/3
  6. * Time: 9:03 PM
  7. */
  8. namespace addons\unishop\extend;
  9. use addons\unishop\controller\User;
  10. use think\Config;
  11. use think\exception\HttpResponseException;
  12. use think\Request;
  13. use think\Response;
  14. trait PhpunitFunctionCustomize
  15. {
  16. /**
  17. * 测试账户
  18. * @var string
  19. */
  20. static $username = 'unishop';
  21. static $mobile = '11111511115';
  22. static $password = '123456';
  23. /**
  24. * 短信动作
  25. * @var string
  26. */
  27. static $smsCode = '1111';
  28. protected $eventRegister = 'register'; // 注册
  29. protected $eventReserpwd = 'resetpwd'; // 重置密码
  30. /**
  31. * 访问本地接口
  32. * @param $class
  33. * @param string $action
  34. * @return array
  35. */
  36. public function request($class, $action = 'index', $params = [], $method = 'POST')
  37. {
  38. $data = [];
  39. if (!empty($class)) {
  40. $header = $get = $post = [];
  41. if (!empty($params['header'])) {
  42. $header = $params['header'];
  43. unset($params['header']);
  44. }
  45. $method = strtolower($method);
  46. $params['millisecond'] = 0;
  47. switch ($method) {
  48. case 'get':
  49. $get = $params;
  50. break;
  51. case 'post':
  52. $post = $params;
  53. break;
  54. }
  55. try {
  56. $data = [];
  57. Request::destroy();
  58. $controller = strtolower(substr($class, strrpos($class, '\\') + 1));
  59. $request = Request::instance([
  60. 'route' => [
  61. 'addon' => 'unishop',
  62. 'controller' => $controller,
  63. 'action' => $action
  64. ],
  65. 'controller' => $controller,
  66. 'action' => $action,
  67. 'get' => $get,
  68. 'post' => $post,
  69. 'request' => $params,
  70. 'header' => $header
  71. ]);
  72. $obj = new $class($request);
  73. $obj->$action();
  74. } catch (HttpResponseException $e) {
  75. $data = $e->getResponse();
  76. }
  77. }
  78. // 输出数据到客户端
  79. if ($data instanceof Response) {
  80. $response = $data;
  81. } elseif (!is_null($data)) {
  82. // 默认自动识别响应输出类型
  83. $type = $request->isAjax() ?
  84. Config::get('default_ajax_return') :
  85. Config::get('default_return_type');
  86. $response = Response::create($data, $type);
  87. } else {
  88. $response = Response::create();
  89. }
  90. return $response->getData();
  91. }
  92. /**
  93. * 模拟用户登录
  94. */
  95. public function userLogin()
  96. {
  97. $contents = $this->request(User::class, 'login',[
  98. 'mobile' => self::$mobile,
  99. 'password' => self::$password
  100. ]);
  101. if ($contents['code'] == 0) {
  102. // 模拟发送短信
  103. \app\common\model\Sms::create(['event' => $this->eventRegister, 'mobile' => self::$mobile, 'code' => self::$smsCode, 'ip' => 'phpunit', 'createtime' => time()]);
  104. $this->request(User::class, 'register', [
  105. 'captcha' => self::$smsCode,
  106. 'mobile' => self::$mobile,
  107. 'password' => self::$password,
  108. 'username' => self::$username
  109. ]);
  110. $contents = $this->request(User::class, 'login',[
  111. 'mobile' => self::$mobile,
  112. 'password' => self::$password
  113. ]);
  114. }
  115. return $contents;
  116. }
  117. }