在 PHPUnit 中验证 HTTP 响应代码
Validating HTTP Response Codes in PHPUnit
我正在为 return HTTP 响应代码的几种方法编写单元测试。我找不到断言 HTTP 响应代码的方法。也许我遗漏了一些明显的东西,或者我对 PHPUnit 有一些误解。
我正在使用 PHPUnit 4.5 稳定版。
class 消息的相关部分:
public function validate() {
// Decode JSON to array.
if (!$json = json_decode($this->read(), TRUE)) {
return http_response_code(415);
}
return $json;
}
// Abstracted file_get_contents a bit to facilitate unit testing.
public $_file_input = 'php://input';
public function read() {
return file_get_contents($this->_file_input);
}
单元测试:
// Load invalid JSON file and verify that validate() fails.
public function testValidateWhenInvalid() {
$stub1 = $this->getMockForAbstractClass('Message');
$path = __DIR__ . '/testDataMalformed.json';
$stub1->_file_input = $path;
$result = $stub1->validate();
// At this point, we have decoded the JSON file inside validate() and have expected it to fail.
// Validate that the return value from HTTP 415.
$this->assertEquals('415', $result);
}
PHP单位returns:
1) MessageTest::testValidateWhenInvalid
Failed asserting that 'true' matches expected '415'.
我不确定为什么 $result 是 returning 'true' 。 . .特别是作为字符串值。也不确定我的 'expected' 参数应该是什么。
According to the docs 可以调用不带参数的http_response_code()
方法来接收当前的响应码。
<?php
http_response_code(401);
echo http_response_code(); //Output: 401
?>
因此您的测试应该如下所示:
public function testValidateWhenInvalid() {
$stub1 = $this->getMockForAbstractClass('Message');
$path = __DIR__ . '/testDataMalformed.json';
$stub1->_file_input = $path;
$result = $stub1->validate();
// At this point, we have decoded the JSON file inside validate() and have expected it to fail.
// Validate that the return value from HTTP 415.
$this->assertEquals(415, http_response_code()); //Note you will get an int for the return value, not a string
}
我正在为 return HTTP 响应代码的几种方法编写单元测试。我找不到断言 HTTP 响应代码的方法。也许我遗漏了一些明显的东西,或者我对 PHPUnit 有一些误解。
我正在使用 PHPUnit 4.5 稳定版。
class 消息的相关部分:
public function validate() {
// Decode JSON to array.
if (!$json = json_decode($this->read(), TRUE)) {
return http_response_code(415);
}
return $json;
}
// Abstracted file_get_contents a bit to facilitate unit testing.
public $_file_input = 'php://input';
public function read() {
return file_get_contents($this->_file_input);
}
单元测试:
// Load invalid JSON file and verify that validate() fails.
public function testValidateWhenInvalid() {
$stub1 = $this->getMockForAbstractClass('Message');
$path = __DIR__ . '/testDataMalformed.json';
$stub1->_file_input = $path;
$result = $stub1->validate();
// At this point, we have decoded the JSON file inside validate() and have expected it to fail.
// Validate that the return value from HTTP 415.
$this->assertEquals('415', $result);
}
PHP单位returns:
1) MessageTest::testValidateWhenInvalid
Failed asserting that 'true' matches expected '415'.
我不确定为什么 $result 是 returning 'true' 。 . .特别是作为字符串值。也不确定我的 'expected' 参数应该是什么。
According to the docs 可以调用不带参数的http_response_code()
方法来接收当前的响应码。
<?php
http_response_code(401);
echo http_response_code(); //Output: 401
?>
因此您的测试应该如下所示:
public function testValidateWhenInvalid() {
$stub1 = $this->getMockForAbstractClass('Message');
$path = __DIR__ . '/testDataMalformed.json';
$stub1->_file_input = $path;
$result = $stub1->validate();
// At this point, we have decoded the JSON file inside validate() and have expected it to fail.
// Validate that the return value from HTTP 415.
$this->assertEquals(415, http_response_code()); //Note you will get an int for the return value, not a string
}