Testing a class in phpspec involving Guzzle -
i'm trying build class queries external api. each method corresponds endpoint makes call 'master call' method responsible sending request api.
for example:
// $this->http guzzlehttp\client 5.3 public function call($httpmethod, $endpoint, array $parameters = []) { $parameters = array_merge($parameters, [ 'headers' => [ 'something' => 'something' ] ]); $request = $this->http->createrequest($httpmethod, $this->baseurl . $endpoint, $parameters); return $this->http->send($request); } public function getall() { return $this->call('get', 'all'); }
what i'm supposed mock? should use willbecalled()
and/or willreturn()
on http client's createrequest()
, send()
methods?
when mock send()
, says: argument 1 passed double\guzzlehttp\client\p2::send() must implement interface guzzlehttp\message\requestinterface, null given
, i'm not sure how provide fake that, because creating dummy interface requires me implement 30 methods on class.
here's test right now:
function it_lists_all_the_things(httpclient $http) { $this->call('get', 'all')->willbecalled(); $http->createrequest()->willbecalled(); $http->send()->willreturn(['foo' => 'bar']); $this->getall()->shouldhavekeywithvalue('foo', 'bar'); }
you should mock behaviour, this:
public function let(client $http) { $this->beconstructedwith($http, 'http://someurl.com/'); } function it_calls_api_correctly(client $http, request $request) { $parameters = array_merge([ 'headers' => [ 'something' => 'something' ] ]); $http->createrequest('get', 'http://someurl.com/all', $parameters)->shouldbecalled()->willreturn($request); $http->send($request)->shouldbecalled(); $this->getall(); }
Comments
Post a Comment