Skip to content

Update async calls with new package and interface #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
46 changes: 16 additions & 30 deletions src/CurlPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
}
9 changes: 7 additions & 2 deletions src/PromiseCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will replace exception related to request with exception related to callback code. Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the callback should handle the exception or rethrow it. But @joelwurtz is the promise master in da house.

}
}

/**
Expand Down
35 changes: 30 additions & 5 deletions tests/CurlPromiseTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
namespace Http\Curl\Tests;

use Http\Client\Promise;
use Http\Client\Exception\TransferException;
use Http\Promise\Promise;
use Http\Curl\CurlPromise;
use Http\Curl\MultiRunner;

Expand Down Expand Up @@ -34,14 +35,38 @@ public function testCoreCalls()

$core->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);
}
}
}
4 changes: 2 additions & 2 deletions tests/PromiseCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
);

Expand Down