如何在php中单元测试curl调用 [英] how to unit test curl call in php
本文介绍了如何在php中单元测试curl调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何进行单元测试curl实现?
public function get(){
$ ch = curl_init($ this-> request-> getUrl());
curl_setopt($ ch,CURLOPT_HEADER,false);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ result = curl_exec($ ch);
$ code = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
$ type = curl_getinfo($ ch,CURLINFO_CONTENT_TYPE);
curl_close($ ch);
if(!strstr($ type,'application / json')){
throw new HttpResponseException('JSON response not found');
}
return new HttpResponse($ code,$ result);
}
我需要测试返回的内容类型,以便它可以抛出异常。
解决方案
正如thomasrutter建议的,创建一个类来抽象cURL函数的使用。
interface HttpRequest
{
public function setOption($ name,$ value);
public function execute();
public function getInfo($ name);
public function close();
}
类CurlRequest实现HttpRequest
{
private $ handle = null;
public function __construct($ url){
$ this-> handle = curl_init($ url);
}
public function setOption($ name,$ value){
curl_setopt($ this-> handle,$ name,$ value);
}
public function execute(){
return curl_exec($ this-> handle);
}
public function getInfo($ name){
return curl_getinfo($ this-> handle,$ name);
}
public function close(){
curl_close($ this-> handle);
}
}
现在你可以使用< c $ c> HttpRequest 接口而不调用任何cURL函数。
public function testGetThrowsWhenContentTypeIsNotJson {
$ http = $ this-> getMock('HttpRequest');
$ http-> expects($ this-> any())
- > method('getInfo')
- > will($ this-> returnValue不是JSON'));
$ this-> setExpectedException('HttpResponseException');
//使用$ http创建测试类,而不是真正的CurlRequest
$ fixture = new ClassUnderTest($ http);
$ fixture-> get();
}
编辑修复了简单的PHP解析错误。 p>
How would you go about unit testing a curl implementation?
public function get() {
$ch = curl_init($this->request->getUrl());
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if (!strstr($type, 'application/json')) {
throw new HttpResponseException('JSON response not found');
}
return new HttpResponse($code, $result);
}
I need to test the content type returned so that it can throw an exception.
解决方案
As thomasrutter suggested, create a class to abstract the usage of the cURL functions.
interface HttpRequest
{
public function setOption($name, $value);
public function execute();
public function getInfo($name);
public function close();
}
class CurlRequest implements HttpRequest
{
private $handle = null;
public function __construct($url) {
$this->handle = curl_init($url);
}
public function setOption($name, $value) {
curl_setopt($this->handle, $name, $value);
}
public function execute() {
return curl_exec($this->handle);
}
public function getInfo($name) {
return curl_getinfo($this->handle, $name);
}
public function close() {
curl_close($this->handle);
}
}
Now you can test using a mock of the HttpRequest
interface without invoking any of the cURL functions.
public function testGetThrowsWhenContentTypeIsNotJson() {
$http = $this->getMock('HttpRequest');
$http->expects($this->any())
->method('getInfo')
->will($this->returnValue('not JSON'));
$this->setExpectedException('HttpResponseException');
// create class under test using $http instead of a real CurlRequest
$fixture = new ClassUnderTest($http);
$fixture->get();
}
Edit Fixed simple PHP parse error.
这篇关于如何在php中单元测试curl调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文