php-http / httplug

HTTPlug, the HTTP client abstraction for PHP
http://httplug.io
MIT License
2.57k stars 39 forks source link

Add sendRequests method as a trait #54

Closed joelwurtz closed 9 years ago

joelwurtz commented 9 years ago

See discussion in #48

joelwurtz commented 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 ?

sagikazarmark commented 9 years ago

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.

joelwurtz commented 9 years ago

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)

sagikazarmark commented 9 years ago

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.

joelwurtz commented 9 years ago

Yes it works, will update the PR

joelwurtz commented 9 years ago

Should be good with last comments

sagikazarmark commented 9 years ago

Can you squash the commits?

joelwurtz commented 9 years ago

done

sagikazarmark commented 9 years ago

Thank you @joelwurtz