发布于 1970-01-01 08:00
  • 9 个回答
    • 其实每次请求的区别就是$_GET、$_POST、$_COOKIE、$_SERVER这些环境数据在发生改变,所以只要改变这些环境数据再执行Controller就能够模拟请求了

      我用单元测试的方式来实现,代码大概就是下面这个样子,其实每次模拟请求就是通过router去执行controller类的不同方法而已

      还可以使用phpunit --process-isolation参数把每次测试放到单独的php进程里执行,避免某些代码逻辑反复执行导致的状态污染问题

      PHP    /**
           * @example
           * $this->execute(array(
           *     'path' => '/foo/bar',
           *     'method' => 'GET',
           *     'get' => array(),
           *     'post' => array(),
           *     'headers' => array(
           *         'Content-Type' => 'text/html'
           *     ),
           * ));
           */
          protected function execute(array $options) {
              if (!isset($options['path'])) {
                  $options['path'] = $this->path;
              }
      
              $_SERVER = array();
              $_GET = $_POST = array();
      
              $_SERVER['REQUEST_URI'] = isset($options['path']) ? $options['path'] : '/';
      
              $method = isset($options['method']) ? strtoupper($options['method']) : 'GET';
              $_SERVER['REQUEST_METHOD'] = $method;
      
              if (isset($options['get'])) {
                  $_GET = $options['get'];
              }
      
              if (isset($options['post'])) {
                  $_POST = $options['post'];
              }
      
              $headers = isset($options['headers']) ? $options['headers'] : array();
              foreach ($headers as $key => $value) {
                  $key = 'HTTP_'.strtoupper(str_replace('-', '_', $key));
                  $_SERVER[$key] = $value;
              }
      
              return $this->router->execute();
          }
      
      2022-12-01 11:48 回答
    • 推荐一个 Chrome 扩展:Postman。

      2022-12-01 11:48 回答
    • firebox,chrome浏览器有很多form表单提交工具,安装一个就好了

      2022-12-01 11:48 回答
    • 请求数据模拟,直接请求即可,如果使用工具,尝试谷歌浏览器的post-man扩展,可以模拟各种请求和数据,包括post,get,put等等

      2022-12-01 11:48 回答
    • 听说过单元测试PHPUnit么?这个不但可以做单元测试,模拟API请求也是可以的,主要是要自己写下Post或Get请求

      2022-12-01 11:48 回答
    • web调试利器,Fiddler2

      2022-12-01 11:48 回答
    • 直接写curl post数据,灵活有方便

      2022-12-01 11:48 回答
    • phpunit
      codeception
      selenium

      自己用 guzzle 写

      2022-12-01 11:48 回答
    • 在chrome上装个插件 postman

      2022-12-01 11:48 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有