How can I get the response to a request in an Integration Test?


We’re looking at moving our Integration Tests for our API to CakePHP’s PHPUnit. We need to test that the returned values from the API are sane (valid JSON, etc…)

The helper methods on the IntegrationTestCase abstract class look useful, but from what I can tell there’s no way to actually read the response to one of these requests. Am I missing something?

It seems that without the helper methods provided by IntegrationTestCase it would be much harder to make requests. So what’s my best option here?


I just realised that I can do this by simply accessing $this->_response. At first I didn’t think I could do this because its visibility is protected, but then I realised that I can access it because my test inherits from IntegrationTestCase.

You can use the following methods along with some others

$this->assertResponseEquals('A complete string what you gona get')
$this->assertResponseContains('A string what should be included in the repsonse')
$this->assertResponseNotContains('A string what should not be in the response, like "You are loged in"')
$this->assertEquals('rrd', $this -> viewVariable('username'));

Perhaps the most basic assertion is $this->assertResponseOk()

Yeah I saw them, not useful to me in this instance though as I need to test things like “response is valid json” or “auth token returned is valid”.

I’ll certainly be using those methods elsewhere.

If you use _serilaize than you should not worry about if the response is valid json. Anyway you find your solution :slight_smile: