Skip to content

Improve interoperability with reactphp/promise #31

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 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)

$this->onFulfilled = function (ResponseInterface $response) use ($onFulfilled, $newPromise) {
try {
$newPromise->resolve($onFulfilled($response));
$return = $onFulfilled($response);

$newPromise->resolve(null !== $return ? $return : $response);
} catch (Exception $exception) {
$newPromise->reject($exception);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Http\Adapter\React\Promise;
use Http\Adapter\React\ReactFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

class PromiseTest extends TestCase
{
Expand All @@ -31,4 +32,25 @@ public function testChain()
self::assertEquals(200, $response->getStatusCode());
self::assertEquals(300, $updatedResponse->getStatusCode());
}

public function testOnFulfilledOptionalReturn()
{
$promise = new Promise($this->loop);
$response = new Response(200);

// create a random mock so we can assert $onFulfilled is called with the correct response
/** @var \SplObjectStorage|\PHPUnit_Framework_MockObject_MockObject $mock */
$mock = $this->getMockBuilder(\SplObjectStorage::class)->getMock();
$mock->expects(self::once())->method('attach')->with($response);

$lastPromise = $promise->then(function (ResponseInterface $response) use ($mock) {
$mock->attach($response);
});

$promise->resolve($response);
$lastResponse = $lastPromise->wait();

// even though our $onFulfilled doesn't return a value, we expect the promise to unwrap the original response
self::assertSame($response, $lastResponse);
}
}