0

I'm new at phpspec (coming from phpunit) and I have problems setting the behavior of a mock returned by another mock.

I'm creating a wrapper class around the Guzzle client and I want to check the output of the response.

Here's the spec:

function it_gets_response_status_code(Client $client, Url $url, Response $response)
{
    $this->beConstructedWith($client);

    $url->__toString()->willReturn('http://example.com');
    $data = ['foo' => 'bar'];

    $response->getStatusCode()->willReturn(200);
    $client->request('POST', $url, ['form_params' => $data])->willReturn($response);

    $this->post($url, $data);
    assert($this->getResponseStatusCode() === 200); // Failing! :(
}

and the corresponding functions in my class:

public function post(Url $url, array $data)
{
    $this->response = $this->client->request('POST', (string) $url, ['form_params' => $data]);
}

public function getResponseStatusCode()
{
    return $this->response->getStatusCode();
}

The assertion is failing and when I check what is this status code, I see that instead of the integer 200, it's an instance of PhpSpec\Wrapper\Subject. What am I missing here?

I've searched and googled but cannot find resources about using the mock returned by another mock in phpspec. I'm wondering if the reason for this is that it's a code smell? If so I'd be glad to see how I could do this differently (currently I cannot see how I could keep the code simple and doing differently).

Iam Zesh
  • 1,797
  • 2
  • 20
  • 42

1 Answers1

0

try:

assert($this->getResponseStatusCode()->getWrappedObject() === 200);

this:

$response->getStatusCode()->willReturn(200)

returns a '200' string wrapped in an Subject object, on which you can then make mock/stub calls if needed. To get the real value of the subject you need to call getWrappedObject

gvf
  • 1,039
  • 7
  • 6