【问题标题】:PHPUnit - Guzzle: API calls not returning the desired responsePHPUnit - Guzzle:API 调用未返回所需响应
【发布时间】:2018-05-04 19:20:35
【问题描述】:

我正在使用 PHPUnit 和 Guzzle 测试 REST api,如果 username(传入request 参数)在 db 中不可用,它会在 db 中创建一条新记录,它会以 JSON 格式发送响应,例如下面:

{
    "success": true,
    "id": "<unique_ID>"
}

如果 username 已经在 db 中可用,那么它会以 JSON 格式发送响应,如下所示:

{
    "success": false,
    "error": "username is already available"
}

这是我的 PHPUnit 测试用例,用于测试上述 API 和 Guzzle:

  1. 这是一个setUp函数,用于设置Guzzle客户端

    public function setUp()
    {
        $this->client = new GuzzleHttp\Client([
            'base_uri' => 'http://localhost/test/'
        ]);
    }
    
  2. 这是实际的测试功能:

    public function testActualResult()
    {
        $response = $this->client->post('service.php', [
            'json' => [
                'operation' => 'create_user',
                'user_name' => 'testUser1'            
            ]
        ]);
    
        var_dump($response);
    }
    

每当我测试这个时,我都会得到这样的响应:

class GuzzleHttp\Psr7\Response#99 (6) {
  private $reasonPhrase =>
  string(2) "OK"
  private $statusCode =>
  int(200)
  private $headers =>
  array(5) {
    'Date' =>
    array(1) {
      [0] =>
      string(29) "Tue, 21 Nov 2017 10:27:22 GMT"
    }
    'Server' =>
    array(1) {
      [0] =>
      string(47) "Apache/2.4.26 (Win32) OpenSSL/1.0.2l PHP/5.6.31"
    }
    'X-Powered-By' =>
    array(1) {
      [0] =>
      string(10) "PHP/5.6.31"
    }
    'Content-Length' =>
    array(1) {
      [0] =>
      string(4) "1357"
    }
    'Content-Type' =>
    array(1) {
      [0] =>
      string(16) "application/json"
    }
  }
  private $headerNames =>
  array(5) {
    'date' =>
    string(4) "Date"
    'server' =>
    string(6) "Server"
    'x-powered-by' =>
    string(12) "X-Powered-By"
    'content-length' =>
    string(14) "Content-Length"
    'content-type' =>
    string(12) "Content-Type"
  }
  private $protocol =>
  string(3) "1.1"
  private $stream =>
  class GuzzleHttp\Psr7\Stream#86 (7) {
    private $stream =>
    resource(408) of type (stream)
    private $size =>
    NULL
    private $seekable =>
    bool(true)
    private $readable =>
    bool(true)
    private $writable =>
    bool(true)
    private $uri =>
    string(10) "php://temp"
    private $customMetadata =>
    array(0) {
    }
  }
}

但我没有收到 API 调用本身发回的所需响应。

如果我使用 POSTMAN 测试我上面提到的 API,它会完美运行并返回所需的响应。

【问题讨论】:

  • 不是您问题的实际答案,但是:单元测试 API 被认为是不好的做法。您的单元测试应该测试您的类和代码,而不是它们的集成。如果您的 API 出现故障,您的单元测试应该仍然是绿色的。您应该考虑改用集成测试工具对此进行测试。它们更适合此类任务。
  • 感谢@OptimusCrime,但我已经看到很多他们使用 PHPUnit 和 Guzzle 测试 API 的示例/帖子。没有人提到像你这样的人。

标签: php phpunit guzzle guzzle6


【解决方案1】:

你读过GuzzlePHP documentation吗?在“快速入门”->“使用响应”下,描述了当您想要获取响应的主体时,您需要使用 $response 上的 getBody() 函数。

您所做的只是转储整个请求变量,其中包含更多信息,然后您需要执行您想做的事情。

查看Using Responses 示例:

$response = $client->post('your parameters here');
$body = $response->getBody();
echo $body;

【讨论】:

  • 感谢您的回复。但它仍然没有返回所需的响应。这就是返回class GuzzleHttp\Psr7\Stream#86 (7) { private $stream =&gt; resource(408) of type (stream) private $size =&gt; NULL private $seekable =&gt; bool(true) private $readable =&gt; bool(true) private $writable =&gt; bool(true) private $uri =&gt; string(10) "php://temp" private $customMetadata =&gt; array(0) { } }
  • @TonyMontana 你确定你不 var_dump 变量而不是 eching 吗?我可以验证getBody() 是否将响应作为文本返回。
  • 好的,如果我使用json_decode($response-&gt;getBody()),我会得到预期的响应。但如果不使用json_decode,我会得到上述回复。感谢@OptimusCrime 的建议以及这篇博文knpuniversity.com/screencast/rest/testing-phpunit
  • 对不起是json_decode($response-&gt;getBody(), true)
猜你喜欢
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 2019-03-22
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多