14

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

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Gordon, Thanks for your reply. We actually execute the service by sending the URI containing the service operations and service class name from command line. So my doubt is if EXEC works in PHPUnit. If it does, it would solve my problem(as I can use EXEC to run whatever I run on command line and test the service)
@sri The Test Helpers extension of PHPUnit can rename functions. See github.com/sebastianbergmann/php-test-helpers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.