Testing RESTful web services using PHPUnit(使用 PHPUnit 测试 RESTful Web 服务)
问题描述
谁能告诉我如何使用 PHPUnit 测试 RESTful Web 服务?PHPUnit 似乎没有这种能力.
Can anyone please let me know how to test the RESTful web services using PHPUnit? PHPUnit doesn't seem to have that capability.
推荐答案
将Request抽象成一个请求对象.这样您就可以测试您的代码,而无需实际发出真正的请求.那么测试就很容易了.
Abstract the Request into a Request Object. This way you can test your code without actually having to make real Requests. Testing that is easy then.
class RequestTest extends PHPUnit_Framework_TestCase
{
public function testRequest()
{
$request = new Request();
$request->setMethod('PUT');
$request->setPutData(…);
$this->assertSomething(
$this->testSubjectUsingRequest->process($request)
);
}
}
如果您想测试来自 Web 服务的 响应,请模拟/存根 Web 服务的 API.
In case you want to test the responses from a Webservice, mock/stub the API of the Webservice.
PHPUnit 章节中有一个章节是关于 存根和模拟 Web 服务 虽然建议的内置 Web 服务模拟设施适用于带有 WSDL 的 Soap 服务,所以您必须手动配置您的 Mocks(就像您使用任何模拟资源).
There is a chapter in the PHPUnit chapter about Stubbing and Mocking Web Services although the suggested in-built webservice mocking facilities apply to Soap Services with WSDL, so you'd have to configure your Mocks by hand (just as you would with any mocked resource).
如果这不能回答您的问题,请更新您的问题,提供有关您尝试使用它执行/测试的 RESTful 服务的更多详细信息.
If this doesn't answer your question please update your question with more details about the RESTful service what you are trying to do/test with it.
这篇关于使用 PHPUnit 测试 RESTful Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHPUnit 测试 RESTful Web 服务


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01