From d1a61a7fb02b203b404dd03a90ad456d609b27aa Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Sun, 13 Dec 2015 02:22:00 +0100 Subject: [PATCH 1/2] Update async calls with new package and interface --- composer.json | 2 +- src/CurlPromise.php | 46 ++++++++++++++------------------------- src/PromiseCore.php | 9 ++++++-- tests/CurlPromiseTest.php | 35 ++++++++++++++++++++++++----- tests/PromiseCoreTest.php | 4 ++-- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/composer.json b/composer.json index 8d70f31..d76ec55 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=5.5", "ext-curl": "*", - "php-http/httplug": "^1.0.0-alpha2", + "php-http/httplug": "1.0.0-alpha3", "php-http/message-factory": "^0.4@dev" }, "require-dev": { diff --git a/src/CurlPromise.php b/src/CurlPromise.php index 2456f75..7089d25 100644 --- a/src/CurlPromise.php +++ b/src/CurlPromise.php @@ -2,8 +2,7 @@ namespace Http\Curl; use Http\Client\Exception; -use Http\Client\Promise; -use Psr\Http\Message\ResponseInterface; +use Http\Promise\Promise; /** * Promise represents a response that may not be available yet, but will be resolved at some point @@ -81,41 +80,28 @@ public function getState() return $this->core->getState(); } - /** - * Return the value of the promise (fulfilled). - * - * @return ResponseInterface Response Object only when the Promise is fulfilled. - * - * @throws \LogicException When the promise is not fulfilled. - */ - public function getResponse() - { - return $this->core->getResponse(); - } - - /** - * Get the reason why the promise was rejected. - * - * If the exception is an instance of Http\Client\Exception\HttpException it will contain - * the response object with the status code and the http reason. - * - * @return Exception Exception Object only when the Promise is rejected. - * - * @throws \LogicException When the promise is not rejected. - */ - public function getException() - { - return $this->core->getException(); - } - /** * Wait for the promise to be fulfilled or rejected. * * When this method returns, the request has been resolved and the appropriate callable has * terminated. + * + * @param bool $unwrap + * + * @return mixed + * + * @throws \Exception When the rejection reason is an exception. */ - public function wait() + public function wait($unwrap = true) { $this->runner->wait($this->core); + + if ($unwrap) { + if ($this->core->getState() == self::REJECTED) { + throw $this->core->getException(); + } + + return $this->core->getResponse(); + } } } diff --git a/src/PromiseCore.php b/src/PromiseCore.php index 8d8f3d5..5420741 100644 --- a/src/PromiseCore.php +++ b/src/PromiseCore.php @@ -2,7 +2,7 @@ namespace Http\Curl; use Http\Client\Exception; -use Http\Client\Promise; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -192,7 +192,12 @@ public function reject(Exception $exception) { $this->exception = $exception; $this->state = Promise::REJECTED; - $this->exception = $this->call($this->onRejected, $this->exception); + + try { + $this->call($this->onRejected, $this->exception); + } catch (Exception $exception) { + $this->exception = $exception; + } } /** diff --git a/tests/CurlPromiseTest.php b/tests/CurlPromiseTest.php index 9dc052c..69da729 100644 --- a/tests/CurlPromiseTest.php +++ b/tests/CurlPromiseTest.php @@ -1,7 +1,8 @@ expects(static::once())->method('getState')->willReturn('STATE'); static::assertEquals('STATE', $promise->getState()); + } + public function testCoreCallWaitFulfilled() + { + $core = $this->createPromiseCore(); + $runner = $this->getMockBuilder(MultiRunner::class)->disableOriginalConstructor() + ->setMethods(['wait'])->getMock(); + /** @var MultiRunner|\PHPUnit_Framework_MockObject_MockObject $runner */ + $promise = new CurlPromise($core, $runner); + + $runner->expects(static::once())->method('wait')->with($core); + $core->expects(static::once())->method('getState')->willReturn(Promise::FULFILLED); $core->expects(static::once())->method('getResponse')->willReturn('RESPONSE'); - static::assertEquals('RESPONSE', $promise->getResponse()); + static::assertEquals('RESPONSE', $promise->wait()); + } - $core->expects(static::once())->method('getException')->willReturn('EXCEPTION'); - static::assertEquals('EXCEPTION', $promise->getException()); + public function testCoreCallWaitRejected() + { + $core = $this->createPromiseCore(); + $runner = $this->getMockBuilder(MultiRunner::class)->disableOriginalConstructor() + ->setMethods(['wait'])->getMock(); + /** @var MultiRunner|\PHPUnit_Framework_MockObject_MockObject $runner */ + $promise = new CurlPromise($core, $runner); $runner->expects(static::once())->method('wait')->with($core); - $promise->wait(); + $core->expects(static::once())->method('getState')->willReturn(Promise::REJECTED); + $core->expects(static::once())->method('getException')->willReturn(new TransferException()); + + try { + $promise->wait(); + } catch (TransferException $exception) { + static::assertTrue(true); + } } } diff --git a/tests/PromiseCoreTest.php b/tests/PromiseCoreTest.php index 1c94619..ecc67d3 100644 --- a/tests/PromiseCoreTest.php +++ b/tests/PromiseCoreTest.php @@ -3,7 +3,7 @@ use Http\Client\Exception; use Http\Client\Exception\RequestException; -use Http\Client\Promise; +use Http\Promise\Promise; use Http\Curl\PromiseCore; use Psr\Http\Message\ResponseInterface; @@ -56,7 +56,7 @@ public function testOnReject() $core = new PromiseCore($request, $this->handle); $core->addOnRejected( function (RequestException $exception) { - return new RequestException('Foo', $exception->getRequest(), $exception); + throw new RequestException('Foo', $exception->getRequest(), $exception); } ); From 47c739bfdf544fd0a32413ce728094fa4babe484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Mon, 14 Dec 2015 10:20:53 +0300 Subject: [PATCH 2/2] Minor changes. --- src/CurlPromise.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/CurlPromise.php b/src/CurlPromise.php index 7089d25..9d21d53 100644 --- a/src/CurlPromise.php +++ b/src/CurlPromise.php @@ -83,14 +83,15 @@ public function getState() /** * Wait for the promise to be fulfilled or rejected. * - * When this method returns, the request has been resolved and the appropriate callable has - * terminated. + * When this method returns, the request has been resolved and the appropriate callable has terminated. * - * @param bool $unwrap + * When called with the unwrap option * - * @return mixed + * @param bool $unwrap Whether to return resolved value / throw reason or not * - * @throws \Exception When the rejection reason is an exception. + * @return \Psr\Http\Message\ResponseInterface|null Resolved value, null if $unwrap is set to false + * + * @throws \Http\Client\Exception The rejection reason. */ public function wait($unwrap = true) { @@ -103,5 +104,6 @@ public function wait($unwrap = true) return $this->core->getResponse(); } + return null; } }