Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit bb75909

Browse files
committed
feat(Response): add method to extract guzzle promise
1 parent 53ac8b0 commit bb75909

File tree

7 files changed

+88
-9
lines changed

7 files changed

+88
-9
lines changed

src/Core/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Client\Adapter\TokenProviderAware;
1111
use Commercetools\Core\Helper\CorrelationIdProvider;
1212
use Commercetools\Core\Client\Adapter\AdapterOptionInterface;
13+
use Commercetools\Core\Response\ApiPromiseGetInterface;
1314
use Commercetools\Core\Response\ErrorResponse;
1415
use Psr\Http\Message\RequestInterface;
1516
use Psr\Http\Message\ResponseInterface;
@@ -285,7 +286,7 @@ public function execute(ClientRequestInterface $request, array $headers = null,
285286
* Executes an API request asynchronously
286287
* @param ClientRequestInterface $request
287288
* @param array $clientOptions
288-
* @return ApiResponseInterface
289+
* @return ApiResponseInterface|ApiPromiseGetInterface
289290
*/
290291
public function executeAsync(ClientRequestInterface $request, array $headers = null, array $clientOptions = [])
291292
{

src/Core/Client/Adapter/Guzzle5Promise.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Psr\Http\Message\ResponseInterface;
1212
use Psr\Http\Message\StreamInterface;
1313

14-
class Guzzle5Promise implements AdapterPromiseInterface
14+
class Guzzle5Promise implements AdapterPromiseInterface, PromiseGetInterface
1515
{
1616
/**
1717
* @var FutureResponse
@@ -36,6 +36,14 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
3636
return $this;
3737
}
3838

39+
/**
40+
* @return FutureInterface
41+
*/
42+
public function getPromise()
43+
{
44+
return $this->promise;
45+
}
46+
3947
/**
4048
* @return ResponseInterface
4149
*/

src/Core/Client/Adapter/Guzzle6Promise.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Psr\Http\Message\ResponseInterface;
1010
use Psr\Http\Message\StreamInterface;
1111

12-
class Guzzle6Promise implements AdapterPromiseInterface
12+
class Guzzle6Promise implements AdapterPromiseInterface, PromiseGetInterface
1313
{
1414
protected $response;
1515
protected $promise;
@@ -20,6 +20,14 @@ public function __construct(PromiseInterface $promise)
2020
$this->promise = $promise;
2121
}
2222

23+
/**
24+
* @return PromiseInterface
25+
*/
26+
public function getPromise()
27+
{
28+
return $this->promise;
29+
}
30+
2331
public function then(callable $onFulfilled = null, callable $onRejected = null)
2432
{
2533
$this->promise = $this->promise->then($onFulfilled, $onRejected);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Client\Adapter;
4+
5+
use GuzzleHttp\Promise\PromiseInterface;
6+
use GuzzleHttp\Ring\Future\FutureInterface;
7+
8+
interface PromiseGetInterface
9+
{
10+
/**
11+
* @return PromiseInterface|FutureInterface Promise
12+
*/
13+
public function getPromise();
14+
}

src/Core/Response/AbstractApiResponse.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace Commercetools\Core\Response;
88

9+
use Commercetools\Core\Client\Adapter\PromiseGetInterface;
910
use Commercetools\Core\Error\ErrorContainer;
11+
use Commercetools\Core\Error\InvalidArgumentException;
12+
use GuzzleHttp\Ring\Future\FutureInterface;
13+
use Prophecy\Promise\PromiseInterface;
1014
use Psr\Http\Message\ResponseInterface;
1115
use Commercetools\Core\Client\Adapter\AdapterPromiseInterface;
1216
use Commercetools\Core\Error\Message;
@@ -18,7 +22,7 @@
1822
/**
1923
* @package Commercetools\Core\Response
2024
*/
21-
abstract class AbstractApiResponse implements ApiResponseInterface, ContextAwareInterface
25+
abstract class AbstractApiResponse implements ApiResponseInterface, ContextAwareInterface, ApiPromiseGetInterface
2226
{
2327
const X_CORRELATION_ID = 'X-Correlation-ID';
2428
use ContextTrait;
@@ -210,4 +214,16 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
210214

211215
return $this;
212216
}
217+
218+
/**
219+
* @inheritDoc
220+
*/
221+
public function getPromise()
222+
{
223+
$adapterResponse = $this->getResponse();
224+
if ($adapterResponse instanceof PromiseGetInterface) {
225+
return $adapterResponse->getPromise();
226+
}
227+
throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL);
228+
}
213229
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Response;
4+
5+
use Prophecy\Promise\PromiseInterface;
6+
use GuzzleHttp\Ring\Future\FutureInterface;
7+
8+
interface ApiPromiseGetInterface
9+
{
10+
/**
11+
* @return PromiseInterface|FutureInterface Promise
12+
*/
13+
public function getPromise();
14+
}

tests/unit/ClientTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Commercetools\Core;
88

9-
use Commercetools\Core\Client\Adapter\AdapterFactory;
109
use Commercetools\Core\Client\Adapter\AdapterOptionInterface;
1110
use Commercetools\Core\Client\Adapter\ConfigAware;
1211
use Commercetools\Core\Client\OAuth\Manager;
@@ -29,6 +28,8 @@
2928
use GuzzleHttp\Handler\MockHandler;
3029
use GuzzleHttp\HandlerStack;
3130
use GuzzleHttp\Middleware;
31+
use GuzzleHttp\Promise\PromiseInterface;
32+
use GuzzleHttp\Ring\Future\FutureInterface;
3233
use GuzzleHttp\Psr7\BufferStream;
3334
use GuzzleHttp\Psr7\Request;
3435
use GuzzleHttp\Psr7\Response;
@@ -745,11 +746,9 @@ public function testUserAgent()
745746
$this->assertSame('commercetools-php-sdk/' . AbstractHttpClient::VERSION, $userAgent[$n++]);
746747
$this->assertSame('GuzzleHttp/' . HttpClient::VERSION, trim($userAgent[$n++], '();'));
747748
if (extension_loaded('curl') && function_exists('curl_version')) {
748-
$this->assertSame('curl/' . \curl_version()['version'],trim($userAgent[$n++], '();'));
749+
$this->assertSame('curl/' . \curl_version()['version'], trim($userAgent[$n++], '();'));
749750
}
750751
$this->assertSame('PHP/' . PHP_VERSION, $userAgent[$n++]);
751-
752-
753752
}
754753
}
755754

@@ -805,7 +804,6 @@ public function testHtmlBody()
805804
$this->assertInstanceOf(ErrorContainer::class, $response->getErrors());
806805
$this->assertEmpty($response->getErrors());
807806
$this->assertSame('Length Required', $response->getMessage());
808-
809807
}
810808

811809
public function testSetClientOptions()
@@ -827,4 +825,24 @@ public function testSetClientOptions()
827825
$this->assertInstanceOf(ConfigAware::class, $client->getHttpClient());
828826
$this->assertFalse($client->getHttpClient()->getConfig('verify'));
829827
}
828+
829+
public function testGetPromise()
830+
{
831+
$client = $this->getMockClient($this->getConfig(), $this->getSingleOpResult(), 200);
832+
833+
$endpoint = new JsonEndpoint('test');
834+
$request = $this->getMockForAbstractClass(
835+
AbstractByIdGetRequest::class,
836+
[$endpoint, 'id']
837+
);
838+
$response = $client->executeAsync($request);
839+
840+
$this->assertFalse($response->isError());
841+
842+
if (version_compare(HttpClient::VERSION, '6.0.0', '>=')) {
843+
$this->assertInstanceOf(PromiseInterface::class, $response->getPromise());
844+
} else {
845+
$this->assertInstanceOf(FutureInterface::class, $response->getPromise());
846+
}
847+
}
830848
}

0 commit comments

Comments
 (0)