diff --git a/composer.json b/composer.json index 12d9195..676dfcf 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..9d21d53 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 @@ -82,40 +81,29 @@ public function getState() } /** - * Return the value of the promise (fulfilled). - * - * @return ResponseInterface Response Object only when the Promise is fulfilled. + * Wait for the promise to be fulfilled or rejected. * - * @throws \LogicException When the promise is not fulfilled. - */ - public function getResponse() - { - return $this->core->getResponse(); - } - - /** - * Get the reason why the promise was rejected. + * When this method returns, the request has been resolved and the appropriate callable has terminated. * - * 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. + * When called with the unwrap option * - * @return Exception Exception Object only when the Promise is rejected. + * @param bool $unwrap Whether to return resolved value / throw reason or not * - * @throws \LogicException When the promise is not rejected. - */ - public function getException() - { - return $this->core->getException(); - } - - /** - * Wait for the promise to be fulfilled or rejected. + * @return \Psr\Http\Message\ResponseInterface|null Resolved value, null if $unwrap is set to false * - * When this method returns, the request has been resolved and the appropriate callable has - * terminated. + * @throws \Http\Client\Exception The rejection reason. */ - 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(); + } + return null; } } 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); } );