PHPUnit - Guzzle:API 调用未返回所需的响应
PHPUnit - Guzzle: API calls not returning the desired response
我正在使用 PHPUnit 和 Guzzle 测试 REST api,如果 username
(在 request
参数中传递)尚未创建,它会在数据库中创建一个新记录在 db 中可用,它在 JSON 中发送响应,如下所示:
{
"success": true,
"id": "<unique_ID>"
}
如果 username
在数据库中已经可用,那么它会在 JSON 中发送响应,如下所示:
{
"success": false,
"error": "username is already available"
}
这是我的 PHPUnit 测试用例,用于测试上述 API 以及 Guzzle:
这是一个 setUp
函数,用于设置 Guzzle 客户端
public function setUp()
{
$this->client = new GuzzleHttp\Client([
'base_uri' => 'http://localhost/test/'
]);
}
这是实际的测试函数:
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,它会完美运行并返回所需的响应。
你读过GuzzlePHP documentation了吗?在 'Quickstart' -> 'Using Responses' 下描述了当你想获得响应的主体时,你需要在 $response
.[= 上使用 getBody()
函数15=]
您正在做的只是转储整个请求变量,其中包含比您需要执行的操作更多的信息。
请参阅 Using Responses 示例:
$response = $client->post('your parameters here');
$body = $response->getBody();
echo $body;
我正在使用 PHPUnit 和 Guzzle 测试 REST api,如果 username
(在 request
参数中传递)尚未创建,它会在数据库中创建一个新记录在 db 中可用,它在 JSON 中发送响应,如下所示:
{
"success": true,
"id": "<unique_ID>"
}
如果 username
在数据库中已经可用,那么它会在 JSON 中发送响应,如下所示:
{
"success": false,
"error": "username is already available"
}
这是我的 PHPUnit 测试用例,用于测试上述 API 以及 Guzzle:
这是一个
setUp
函数,用于设置 Guzzle 客户端public function setUp() { $this->client = new GuzzleHttp\Client([ 'base_uri' => 'http://localhost/test/' ]); }
这是实际的测试函数:
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,它会完美运行并返回所需的响应。
你读过GuzzlePHP documentation了吗?在 'Quickstart' -> 'Using Responses' 下描述了当你想获得响应的主体时,你需要在 $response
.[= 上使用 getBody()
函数15=]
您正在做的只是转储整个请求变量,其中包含比您需要执行的操作更多的信息。
请参阅 Using Responses 示例:
$response = $client->post('your parameters here');
$body = $response->getBody();
echo $body;