14

I want to mock a response to the Guzzle request:

 $response = new Response(200, ['X-Foo' => 'Bar']);

 //how do I set content of $response to--> "some mocked content"

 $client = Mockery::mock('GuzzleHttp\Client');
 $client->shouldReceive('get')->once()->andReturn($response);

I noticed I need to add as third parameter the interface:

 GuzzleHttp\Stream\StreamInterface

but there are so many implementations of it, and I want to return a simple string. Any ideas?

Edit: now I use this:

 $response = new Response(200, [], GuzzleHttp\Stream\Stream::factory('bad xml here'));

but when I check this:

$response->getBody()->getContents()

I get an empty string. Why is this?

Edit 2: this happened to me only when I used xdebug, when it runs normally it works great!

halfer
  • 19,824
  • 17
  • 99
  • 186
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
  • Why not just mock the `Response` as well?! I suppose that the behaviour of the class under test depends on the response. So you'd want to mock it too, so you'd be sure that each time the input data for your CUT is the same. – Havelock Dec 16 '14 at 14:30

5 Answers5

32

We'll just keep doing this. The previous answer is for Guzzle 5, this is for Guzzle 6:

use GuzzleHttp\Psr7;

$stream = Psr7\stream_for('{"data" : "test"}');
$response = new Response(200, ['Content-Type' => 'application/json'], $stream);
tomvo
  • 1,409
  • 1
  • 12
  • 21
  • 8
    Looking at the [Guzzle source](https://github.com/guzzle/psr7/blob/1.2.1/src/Response.php#L96), Response does this for you automatically. Just pass a string. – Tim Nov 11 '15 at 12:44
12

The previous answer is for Guzzle 3. Guzzle 5 uses the following:

<?php
$body = GuzzleHttp\Stream\Stream::factory('some mocked content');
$response = new Response(200, ['X-Foo' => 'Bar'], $body);
Michael Dowling
  • 5,098
  • 4
  • 32
  • 28
7

Using @tomvo answer and the comment from @Tim - this is what I did for testing Guzzle 6 inside my Laravel app:

use GuzzleHttp\Psr7\Response;

$string = json_encode(['data' => 'test']);
$response = new Response(200, ['Content-Type' => 'application/json'], $string);

$guzzle = Mockery::mock(GuzzleHttp\Client::class);
$guzzle->shouldReceive('get')->once()->andReturn($response);
Laurence
  • 58,936
  • 21
  • 171
  • 212
3

Guzzle\Http\Message\Response allows you to specify the third parameter as a string.

$body = '<html><body>Hello world!</body></html>';
$response = new Response(200, ['X-Foo' => 'Bar'], $body);

If you'd prefer a solution that implements Guzzle\Stream\StreamInterface, then I recommend using Guzzle\Http\EntityBody for the most straightforward implementation:

$body = Guzzle\Http\EntityBody::fromString('<html><body>Hello world!</body></html>');
$response = new Response(200, ['X-Foo' => 'Bar'], $body);
Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
1

For Guzzle 7, you can use the GuzzleHttp\Psr7\Utils::streamFor() method as follows:

$data = json_encode(['X-Foo' => 'Bar']);
$stream = Utils::streamFor($data);

And then you can pass the $stream object to the andReturn method of the mocked client.

M074554N
  • 61
  • 1
  • 6