Skip to content

Commit

Permalink
Improve callAsync
Browse files Browse the repository at this point in the history
 - add check for hasResponse: if response is not received, the request exception should be rethrown.
 - move response parsing from finally block and only parse response when response is safely received.
 - finally block only responsible for releasing stream resources.
  • Loading branch information
meng-tian committed Apr 24, 2016
1 parent 9d322bd commit 621c9ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;

class SoapClient implements SoapClientInterface
{
Expand Down Expand Up @@ -37,19 +38,30 @@ function () use ($name, $arguments, $options, $inputHeaders, &$outputHeaders) {
/** @var HttpBinding $httpBinding */
$httpBinding = (yield $this->deferredHttpBinding);
$request = $httpBinding->request($name, $arguments, $options, $inputHeaders);

try {
$response = (yield $this->client->sendAsync($request));
yield $this->parseResponse($httpBinding, $response, $name, $outputHeaders);
} catch (RequestException $exception) {
$response = $exception->getResponse();
} finally {
try {
yield $httpBinding->response($response, $name, $outputHeaders);
} finally {
$request->getBody()->close();
$response->getBody()->close();
if ($exception->hasResponse()) {
$response = $exception->getResponse();
yield $this->parseResponse($httpBinding, $response, $name, $outputHeaders);
} else {
throw $exception;
}
} finally {
$request->getBody()->close();
}
}
);
}

private function parseResponse(HttpBinding $httpBinding, ResponseInterface $response, $name, &$outputHeaders)
{
try {
return $httpBinding->response($response, $name, $outputHeaders);
} finally {
$response->getBody()->close();
}
}
}
53 changes: 52 additions & 1 deletion tests/unit/SoapClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ public function magicCallHttpBindingFailed()
'someSoapMethod', [['some-key' => 'some-value']]
);

$this->httpBindingMock->expects($this->never())->method('response');

$client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
$client->someSoapMethod(['some-key' => 'some-value'])->wait();
}

/**
* @test
*/
public function magicCallClientReturnError()
public function magicCall500Response()
{
$this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);

Expand All @@ -95,6 +97,55 @@ public function magicCallClientReturnError()
$this->assertEquals('SoapResult', $client->someSoapMethod(['some-key' => 'some-value'])->wait());
}

/**
* @test
* @expectedException \GuzzleHttp\Exception\RequestException
*/
public function magicCallResponseNotReceived()
{
$this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);

$this->httpBindingMock->method('request')
->willReturn(
new Request('POST', 'www.endpoint.com')
)
->with(
'someSoapMethod', [['some-key' => 'some-value']]
);

$this->httpBindingMock->expects($this->never())->method('response');

$this->handlerMock->append(GuzzleRequestException::create(new Request('POST', 'www.endpoint.com')));

$client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
$client->someSoapMethod(['some-key' => 'some-value'])->wait();
}

/**
* @test
* @expectedException \Exception
*/
public function magicCallUndefinedResponse()
{
$this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);

$this->httpBindingMock->method('request')
->willReturn(
new Request('POST', 'www.endpoint.com')
)
->with(
'someSoapMethod', [['some-key' => 'some-value']]
);

$this->httpBindingMock->expects($this->never())->method('response');

$this->handlerMock->append(new \Exception());

$client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
$client->someSoapMethod(['some-key' => 'some-value'])->wait();

}

/**
* @test
* @expectedException \SoapFault
Expand Down

0 comments on commit 621c9ba

Please sign in to comment.