How to test web services using PHPUnit?

I want to check out a couple of SOAP web services. What types of tests can I run?

+4
source share
3 answers

It is better to test local consumer classes with mocks SoapClient, which return a pre-written XML result, because Unit-Tests are designed to run quickly and be independent of remote services.

  • Create the Mock class of your Client class (you must have an object wrapper for SoapClient in order to be able to test it completely).
  • Use $this->returnValue() to return the pre-written XML responses or headers that your system expects.

See: http://www.phpunit.de/manual/current/en/test-doubles.html

If your system depends on the availability of these remote services, you can implement a watchdog service that checks if ressource is available, but this is not what should be done in the tests themselves.

Regards, Thomas

+8
source

Testing the SOAP web service will be exactly the same as testing the β€œlocal” method: you will have to call this method, pass it some parameters and chechink the return value to make sure that it matches the parameters you gave.

Of course, using web services, the call will be a little more complicated, since you have to work with SoapClient to call the method, but the idea will still be the same.

The biggest problems that I see are the following:

  • Web service calls are slow (they go over the network), which means your tests will take time to complete, which means you won’t be able to run them as often
  • With a web service, you could potentially have more than one possible reason for the failure; which means you will have more problems figuring out why the test failed:
    • This may be unsuccessful because there is a mistake - which is an ideal case
    • But it can also fail because the remote server is down.
    • Or due to network unavailability
    • And probably some other possible reasons.
  • Of course, since the code will be executed on a remote server, and not on the machine running PHPUnit, it will be much more difficult to get code coverage (for example)
0
source

I found http://www.versioneye.com/package/fakeweb , this

FakeWeb is a helper for faking web requests in Ruby. It works on a global level, without changing the code or writing extensive stubs.

0
source

All Articles