Question

Using the Guzzle MockPlugin in a PHPUnit test. I found that the response content body is null whatever I do.

Am I doing something wrong, or is it a bug?

Test setup

$client = new Client();
$plugin = new MockPlugin();
$plugin->addResponse(new Response(200, null, "This is never sent..."));
$client->addSubscriber($plugin);

$this->httpClientFactoryMock
     ->expects($this->any())
     ->method('getClient')
     ->will($this->returnValue($client));

Method under test

$client = $this->httpClientFactory->getClient();
$request = $client->post($this->url, null, $content->asXML());
$response = $request->send();

$response->body doesn't have "This is never sent..." anywhere :( I can however put stuff in the header, so I'm sure the plugin is working, at least to some extent.

Was it helpful?

Solution

Problem was I wasn't send or reading correctly.

According to this I need to read the content as $response->getBody(true) to read it as a string.

Some of you might have noticed the $content->asXML() in the post function. So I actually need to do $response->xml(), and remember to put the ->asXML() on the sent content. Now it works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top