Skip to content

Commit

Permalink
Adjusting the Mock class for compatibility with guzzlehttp version 6
Browse files Browse the repository at this point in the history
  • Loading branch information
lluz committed Jul 18, 2017
1 parent 864155c commit 6435fb1
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions src/Mock.php
Expand Up @@ -8,32 +8,29 @@
use Aeris\GuzzleHttpMock\Exception\UnexpectedHttpRequestException; use Aeris\GuzzleHttpMock\Exception\UnexpectedHttpRequestException;
use Aeris\GuzzleHttpMock\Expectation\RequestExpectation; use Aeris\GuzzleHttpMock\Expectation\RequestExpectation;
use Aeris\GuzzleHttpMock\Exception\Exception as HttpMockException; use Aeris\GuzzleHttpMock\Exception\Exception as HttpMockException;
use GuzzleHttp\ClientInterface; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Event\BeforeEvent; use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Message\RequestInterface; use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Message\Request; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Message\ResponseInterface;


/** /**
* Sets expectations against requests made with the Guzzle Http Client, * Sets expectations against requests made with the Guzzle Http Client,
* and mocks responses. * and mocks responses.
*/ */
class Mock implements SubscriberInterface { class Mock {


/** @var \Aeris\GuzzleHttpMock\Expectation\RequestExpectation[] */ /** @var \Aeris\GuzzleHttpMock\Expectation\RequestExpectation[] */
protected $requestExpectations = []; protected $requestExpectations = [];


/** @var UnexpectedHttpRequestException[] */ /** @var UnexpectedHttpRequestException[] */
protected $exceptions = []; protected $exceptions = [];


public function attachToClient(ClientInterface &$client) { public static function createWithMiddleware() {
$client->getEmitter()->attach($this); return HandlerStack::create(new self());
} }


public function shouldReceiveRequest(RequestInterface &$request = null) { public function shouldReceiveRequest(RequestInterface &$request = null) {
$expectation = new Expectation\RequestExpectation($request); $expectation = new RequestExpectation($request);
$this->requestExpectations[] = $expectation; $this->requestExpectations[] = $expectation;


return $expectation; return $expectation;
Expand All @@ -56,42 +53,43 @@ public function verify() {
} }
} }


public function getEvents() { public function __invoke(RequestInterface $request, array $options)
return [ {
// Provide name and optional priority try {
'before' => ['onBefore', 'last'], $response = $this->makeRequest($request,$options);
]; }
} catch (HttpMockException $error) {

$this->fail($error);
public function onBefore(BeforeEvent $event) {
$request = $event->getRequest(); // Set a stub response.

// The exception will actually be thrown in
try { // `verify()`
$response = $this->makeRequest($request); // If we threw the exception here,
$event->intercept($response); // it would be caught by Guzzle,
} // and wrapped into a RequestException
catch (HttpMockException $error) { return \GuzzleHttp\Promise\promise_for(new Response(200));
$this->fail($error, $event); }
}
} return \GuzzleHttp\Promise\promise_for($response);
}


/** /**
* @param RequestInterface $request * @param RequestInterface $request
* @return ResponseInterface * @return ResponseInterface
* @throws CompoundUnexpectedHttpRequestException * @throws CompoundUnexpectedHttpRequestException
*/ */
private function makeRequest(RequestInterface $request) { private function makeRequest(RequestInterface $request, array $options) {
$state = array_reduce( $state = array_reduce(
$this->requestExpectations, $this->requestExpectations,
function (array $state, Expectation\RequestExpectation $requestExpectation) use ($request) { function (array $state, Expectation\RequestExpectation $requestExpectation) use ($request, $options) {
// We got a successful response -- we're good to go. // We got a successful response -- we're good to go.
if (isset($state['response'])) { if (isset($state['response'])) {
return $state; return $state;
} }


// Try to make a request against the expectation // Try to make a request against the expectation
try { try {
$state['response'] = $requestExpectation->makeRequest($request); $state['response'] = $requestExpectation->makeRequest($request, $options);
} }
catch (UnexpectedHttpRequestException $error) { catch (UnexpectedHttpRequestException $error) {
// Save the error // Save the error
Expand All @@ -109,23 +107,15 @@ function (array $state, Expectation\RequestExpectation $requestExpectation) use
if (is_null($state['response'])) { if (is_null($state['response'])) {
$msg = array_reduce($state['errors'], function($msg, \Exception $err) { $msg = array_reduce($state['errors'], function($msg, \Exception $err) {
return $msg . PHP_EOL . $err->getMessage(); return $msg . PHP_EOL . $err->getMessage();
}, "No mock matches request `{$request->getMethod()} {$request->getUrl()}`:"); }, "No mock matches request `{$request->getMethod()} {".(string)$request->getUri()."}`:");


throw new UnexpectedHttpRequestException($msg); throw new UnexpectedHttpRequestException($msg);
} }


return $state['response']; return $state['response'];
} }


protected function fail(\Exception $error, BeforeEvent $event) { protected function fail(\Exception $error) {
$this->exceptions[] = $error; $this->exceptions[] = $error;

// Set a stub response.
// The exception will actually be thrown in
// `verify()`
// If we threw the exception here,
// it would be caught by Guzzle,
// and wrapped into a RequestException
$event->intercept(new Response(200));
} }
} }

0 comments on commit 6435fb1

Please sign in to comment.