Testing RESTful web services using PHPUnit

孤街浪徒 提交于 2019-12-20 10:36:42

问题


Can anyone please let me know how to test the RESTful web services using PHPUnit? PHPUnit doesn't seem to have that capability.


回答1:


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)
        );
    }
}

In case you want to test the responses from a Webservice, mock/stub the API of the Webservice.

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).

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.



来源:https://stackoverflow.com/questions/5683392/testing-restful-web-services-using-phpunit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!