如何在不使用 PhpBrower 的情况下在 Codeception 中保存来自 REST 客户端的 JSON 响应

How to save a JSON-response from a REST-client in Codeception without using PhpBrower

我正在尝试使用为我处理请求的客户端库来测试我自己的 Rest-API。 我写了一个帮助程序,其中客户端被初始化并添加了一个名为 "wantToGetUserOverClient($userId)" 的操作。当我 var_export() JSON 时,客户端执行请求和 returns JSON,它按预期工作。 我尝试扩展 Rest 模块并直接分配 public 响应字段但出现异常:

[ModuleException] PhpBrowser: Page not loaded. Use `$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it

我的问题: 在 Codeception REST 模块响应中保存客户端响应的最佳方式是什么,这样我就可以使用已经存在的 JSON-actions。 例如:

$I->wantToGetUserOverClient(1234);
$I->canSeeResponseIsJson();

没有使用 PhpBrower 加载页面。

提前致谢。

我的帮手:

class ApiHelper extends \Codeception\Module\REST
{
    private $backendApi;

    public function __construct(ModuleContainer $moduleContainer, $config = null)
    {
        parent::__construct($moduleContainer, $config);
        $this->backendApi = new BackendRestAPI();
    }

    public function wantToGetUserOverClient($userId)
    {
        $this->response = $this->backendApi->user()->one($userId);
    }
}

您滥用了 REST 模块。
但如果你坚持,你必须用你的模块替换 PhpBrowser。
配置:

REST:
  depends: YourModule

要使 seeResponseIsJson 工作,YourModule 必须扩展 Codeception\Lib\InnerBrowser 并实施 _getResponseContent method.