How to unit test your API?

Solution 1:

You should either create Mocks or use Isolation Framework in order to simulate API environment. Unit tests should not depend on resources like internet connections, network, endpoints etc.

If you intend to test real API calls you should create integration test project and use it for this purpose. But be aware integration tests are mostly not repeatable and would give you different results on each run.

Solution 2:

I'd recommend starting with a little research. These articles should help:

  • Unit Testing CakePHP Shells
  • Testing CakePHP controllers - Mock Objects edition
  • Testing Models with CakePHP 1.2 test suite

Solution 3:

Looks like you might be able to test the raw XML PUT and POST data without too much trouble. The CakePHP REST documentation says this:

If a POST or PUT request has an XML content-type, then the input is taken and passed to an instance of Cake's Xml object, which is assigned to the $data property of the controller. Because of this feature, handling XML and POST data in parallel is seamless: no changes are required to the controller or model code. Everything you need should end up in $this->data.

Try stepping through your controller code in debug mode to see what actually comes in through $this->data during an XML request.

As for avoiding the live database, would a SQLite in-memory database be any easier?