Closed joelwurtz closed 9 years ago
Method should be good, but i'm having a hard time to write specification for the Trait (never done phpspec before) and it's seems quite hards since i use the sendRequest method internally, do you have an idea for the spec @sagikazarmark ?
Nice work @joelwurtz! :+1: :+1:
Unfortunately it is hard to test internal methods with phpspec. I usually use a hack with the stub: inject the response/exception in the stub's constructor. You can then return it in the sendRequest method.
Alternatively you can inject a HttpPsrClient mock and call its sendRequest in the stub. This way you have greater control over what's happening. Then in your tests you can add calls to the HttpPsrClient mock.
Yeah but the problem is that i need a implementation for the ResponseInterface (i have try to use prophecy but it only mock method and the interface is not used so it always fail in the sendRequests method)
i have try to use prophecy but it only mock method and the interface is not used so it always fail in the sendRequests method
I am not sure I understand this.
You can do something like this:
class BatchRequestSpec extends ObjectBehavior
{
function let()
{
$this->beAnInstanceOf('spec\Http\Client\Util\BatchRequestStub');
}
function let(HttpPsrClient $client)
{
$this->beAnInstanceOf('spec\Http\Client\Util\BatchRequestStub', [$client]);
}
function it_does_something(HttpPsrClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response, Exception $e)
{
$client->sendRequest($request1)->willReturn($response);
$client->sendRequest($request2)->willThrow($e);
$this->sendRequests([$request1, $request2]);
}
}
class BatchRequestStub
{
use BatchRequest;
protected $client;
public function __construct(HttpPsrClient $client)
{
$this->client = $client;
}
/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request, array $options = [])
{
return $this->client->sendRequest($request, $options);
}
}
At least I think it should work.
Yes it works, will update the PR
Should be good with last comments
Can you squash the commits?
done
Thank you @joelwurtz
See discussion in #48