From 6701e92a6b2be1b7b041cff11677a182064fc7fa Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Tue, 29 Nov 2016 20:01:46 +0100 Subject: [PATCH 01/11] Implement Interop\Async\Promise --- composer.json | 3 +- src/FulfilledPromise.php | 17 +- src/LazyPromise.php | 9 +- src/Promise.php | 15 +- src/RejectedPromise.php | 20 +- src/functions.php | 23 +++ tests/FulfilledPromiseTest.php | 11 +- tests/FunctionRejectTest.php | 37 ++++ tests/FunctionResolveTest.php | 37 ++++ .../AsyncInteropRejectedTestTrait.php | 51 +++++ .../AsyncInteropResolvedTestTrait.php | 72 +++++++ tests/PromiseTest/AsyncInteropTestTrait.php | 186 ++++++++++++++++++ tests/PromiseTest/FullTestTrait.php | 5 +- tests/RejectedPromiseTest.php | 11 +- ...SimpleFulfilledAsyncInteropTestPromise.php | 13 ++ .../SimpleRejectedAsyncInteropTestPromise.php | 20 ++ 16 files changed, 520 insertions(+), 10 deletions(-) create mode 100644 tests/PromiseTest/AsyncInteropRejectedTestTrait.php create mode 100644 tests/PromiseTest/AsyncInteropResolvedTestTrait.php create mode 100644 tests/PromiseTest/AsyncInteropTestTrait.php create mode 100644 tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php create mode 100644 tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php diff --git a/composer.json b/composer.json index 7cf1750f..1bbbc3f5 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ {"name": "Jan Sorgalla", "email": "jsorgalla@gmail.com"} ], "require": { - "php": ">=5.4.0" + "php": ">=5.4.0", + "async-interop/promise": "^0.3.0" }, "autoload": { "psr-4": { diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index 27bd99a7..f036ddec 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -2,13 +2,15 @@ namespace React\Promise; -class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface +use Interop\Async\Promise as AsyncInteropPromise; + +class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise { private $value; public function __construct($value = null) { - if ($value instanceof PromiseInterface) { + if ($value instanceof PromiseInterface || $value instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.'); } @@ -66,4 +68,15 @@ public function always(callable $onFulfilledOrRejected) public function cancel() { } + + public function when(callable $onResolved) + { + try { + $onResolved(null, $this->value); + } catch (\Throwable $exception) { + AsyncInteropPromise\ErrorHandler::notify($exception); + } catch (\Exception $exception) { + AsyncInteropPromise\ErrorHandler::notify($exception); + } + } } diff --git a/src/LazyPromise.php b/src/LazyPromise.php index a2aecd2d..60c3db14 100644 --- a/src/LazyPromise.php +++ b/src/LazyPromise.php @@ -2,7 +2,9 @@ namespace React\Promise; -class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface +use Interop\Async\Promise as AsyncInteropPromise; + +class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise { private $factory; private $promise; @@ -37,6 +39,11 @@ public function cancel() return $this->promise()->cancel(); } + public function when(callable $onResolved) + { + return $this->promise()->when($onResolved); + } + /** * @internal * @see Promise::settle() diff --git a/src/Promise.php b/src/Promise.php index 0466a571..a3453416 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -2,7 +2,9 @@ namespace React\Promise; -class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface +use Interop\Async\Promise as AsyncInteropPromise; + +class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise { private $canceller; private $result; @@ -87,6 +89,17 @@ public function cancel() $this->call($canceller); } + public function when(callable $onResolved) + { + if (null !== $this->result) { + return $this->result()->when($onResolved); + } + + $this->handlers[] = function (AsyncInteropPromise $promise) use ($onResolved) { + $promise->when($onResolved); + }; + } + private function resolver(callable $onFulfilled = null, callable $onRejected = null) { return function ($resolve, $reject) use ($onFulfilled, $onRejected) { diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index 6ea75fc9..e9eee748 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -2,13 +2,15 @@ namespace React\Promise; -class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface +use Interop\Async\Promise as AsyncInteropPromise; + +class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise { private $reason; public function __construct($reason = null) { - if ($reason instanceof PromiseInterface) { + if ($reason instanceof PromiseInterface || $reason instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.'); } @@ -74,4 +76,18 @@ public function always(callable $onFulfilledOrRejected) public function cancel() { } + + public function when(callable $onResolved) + { + try { + $onResolved( + UnhandledRejectionException::resolve($this->reason), + null + ); + } catch (\Throwable $exception) { + AsyncInteropPromise\ErrorHandler::notify($exception); + } catch (\Exception $exception) { + AsyncInteropPromise\ErrorHandler::notify($exception); + } + } } diff --git a/src/functions.php b/src/functions.php index 004747c7..8e207ed4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,12 +2,27 @@ namespace React\Promise; +use Interop\Async\Promise as AsyncInteropPromise; + function resolve($promiseOrValue = null) { if ($promiseOrValue instanceof ExtendedPromiseInterface) { return $promiseOrValue; } + if ($promiseOrValue instanceof AsyncInteropPromise) { + return new Promise(function ($resolve, $reject) use ($promiseOrValue) { + $promiseOrValue->when(function ($reason = null, $value = null) use ($resolve, $reject) { + if ($reason) { + $reject($reason); + return; + } + + $resolve($value); + }); + }); + } + if (method_exists($promiseOrValue, 'then')) { $canceller = null; @@ -31,6 +46,14 @@ function reject($promiseOrValue = null) }); } + if ($promiseOrValue instanceof AsyncInteropPromise) { + return new Promise(function ($resolve, $reject) use ($promiseOrValue) { + $promiseOrValue->when(function ($reason = null, $value = null) use ($resolve, $reject) { + $reject($reason ? $reason : $value); + }); + }); + } + return new RejectedPromise($promiseOrValue); } diff --git a/tests/FulfilledPromiseTest.php b/tests/FulfilledPromiseTest.php index c70bbcc4..265e020f 100644 --- a/tests/FulfilledPromiseTest.php +++ b/tests/FulfilledPromiseTest.php @@ -7,7 +7,8 @@ class FulfilledPromiseTest extends TestCase { use PromiseTest\PromiseSettledTestTrait, - PromiseTest\PromiseFulfilledTestTrait; + PromiseTest\PromiseFulfilledTestTrait, + PromiseTest\AsyncInteropResolvedTestTrait; public function getPromiseTestAdapter(callable $canceller = null) { @@ -44,4 +45,12 @@ public function shouldThrowExceptionIfConstructedWithAPromise() return new FulfilledPromise(new FulfilledPromise()); } + + /** @test */ + public function shouldThrowExceptionIfConstructedWithAAsyncInteropPromise() + { + $this->setExpectedException('\InvalidArgumentException'); + + return new FulfilledPromise(new FulfilledPromise()); + } } diff --git a/tests/FunctionRejectTest.php b/tests/FunctionRejectTest.php index 84b8ec6a..dd4ab7b6 100644 --- a/tests/FunctionRejectTest.php +++ b/tests/FunctionRejectTest.php @@ -42,6 +42,24 @@ public function shouldRejectAFulfilledPromise() ); } + /** @test */ + public function shouldRejectAFulfilledAsyncInteropPromise() + { + $resolved = new SimpleFulfilledAsyncInteropTestPromise(); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo('foo')); + + reject($resolved) + ->then( + $this->expectCallableNever(), + $mock + ); + } + /** @test */ public function shouldRejectARejectedPromise() { @@ -61,4 +79,23 @@ public function shouldRejectARejectedPromise() $mock ); } + + /** @test */ + public function shouldRejectARejectedAsyncInteropPromise() + { + $exception = new \Exception('foo'); + $resolved = new SimpleRejectedAsyncInteropTestPromise($exception); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo($exception)); + + reject($resolved) + ->then( + $this->expectCallableNever(), + $mock + ); + } } diff --git a/tests/FunctionResolveTest.php b/tests/FunctionResolveTest.php index 29dd5d8e..2db976c3 100644 --- a/tests/FunctionResolveTest.php +++ b/tests/FunctionResolveTest.php @@ -71,6 +71,24 @@ public function shouldResolveACancellableThenable() $this->assertTrue($thenable->cancelCalled); } + /** @test */ + public function shouldResolveAFulfilledAsyncInteropPromise() + { + $promise = new SimpleFulfilledAsyncInteropTestPromise(); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo('foo')); + + resolve($promise) + ->then( + $mock, + $this->expectCallableNever() + ); + } + /** @test */ public function shouldRejectARejectedPromise() { @@ -91,6 +109,25 @@ public function shouldRejectARejectedPromise() ); } + /** @test */ + public function shouldRejectARejectedAsyncInteropPromise() + { + $exception = new \Exception('foo'); + $promise = new SimpleRejectedAsyncInteropTestPromise($exception); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo($exception)); + + resolve($promise) + ->then( + $this->expectCallableNever(), + $mock + ); + } + /** @test */ public function shouldSupportDeepNestingInPromiseChains() { diff --git a/tests/PromiseTest/AsyncInteropRejectedTestTrait.php b/tests/PromiseTest/AsyncInteropRejectedTestTrait.php new file mode 100644 index 00000000..c98f88fd --- /dev/null +++ b/tests/PromiseTest/AsyncInteropRejectedTestTrait.php @@ -0,0 +1,51 @@ +getPromiseTestAdapter(); + + $adapter->reject(new \RuntimeException); + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked = true; + }); + $this->assertTrue($invoked); + } + + public function testWhenOnErrorFailedAsyncInteropPromise() + { + if (PHP_VERSION_ID < 70000) { + $this->markTestSkipped("Error only exists on PHP 7+"); + } + + $adapter = $this->getPromiseTestAdapter(); + $adapter->reject(new \Error); + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "Error"); + $invoked = true; + }); + $this->assertTrue($invoked); + } + + public function testWhenOnExceptionFailedAsyncInteropPromiseWhenRejectedWithoutReason() + { + $adapter = $this->getPromiseTestAdapter(); + + $adapter->reject(); + $adapter->promise()->when(function ($e, $value) use (&$invoked) { + $this->assertInstanceOf("Exception", $e); + $this->assertNull($value); + $invoked = true; + }); + $this->assertTrue($invoked); + } +} diff --git a/tests/PromiseTest/AsyncInteropResolvedTestTrait.php b/tests/PromiseTest/AsyncInteropResolvedTestTrait.php new file mode 100644 index 00000000..401ec343 --- /dev/null +++ b/tests/PromiseTest/AsyncInteropResolvedTestTrait.php @@ -0,0 +1,72 @@ +getPromiseTestAdapter(); + + $adapter->resolve($value); + $adapter->promise()->when(function ($e, $v) use (&$invoked, $value) { + $this->assertSame(null, $e); + $this->assertSame($value, $v); + $invoked = true; + }); + $this->assertTrue($invoked); + } + + /** + * Implementations MAY fail upon resolution with an AsyncInteropPromise, + * but they definitely MUST NOT return an AsyncInteropPromise + */ + public function testAsyncInteropPromiseResolutionWithAsyncInteropPromise() + { + $adapter = $this->getPromiseTestAdapter(); + $adapter->resolve(true); + $success = $adapter->promise(); + + $adapter = $this->getPromiseTestAdapter(); + + $ex = false; + try { + $adapter->resolve($success); + } catch (\Throwable $e) { + $ex = true; + } catch (\Exception $e) { + $ex = true; + } + if (!$ex) { + $adapter->promise()->when(function ($e, $v) use (&$invoked) { + $invoked = true; + $this->assertFalse($v instanceof AsyncInteropPromise); + }); + $this->assertTrue($invoked); + } + } +} diff --git a/tests/PromiseTest/AsyncInteropTestTrait.php b/tests/PromiseTest/AsyncInteropTestTrait.php new file mode 100644 index 00000000..f30b4a19 --- /dev/null +++ b/tests/PromiseTest/AsyncInteropTestTrait.php @@ -0,0 +1,186 @@ +getPromiseTestAdapter(); + + $adapter->promise()->when(function ($e, $v) use (&$invoked, $value) { + $this->assertSame(null, $e); + $this->assertSame($value, $v); + $invoked = true; + }); + $adapter->resolve($value); + $this->assertTrue($invoked); + } + + public function testSuccessAllWhensExecuted() + { + $adapter = $this->getPromiseTestAdapter(); + $invoked = 0; + + $adapter->promise()->when(function ($e, $v) use (&$invoked) { + $this->assertSame(null, $e); + $this->assertSame(true, $v); + $invoked++; + }); + $adapter->promise()->when(function ($e, $v) use (&$invoked) { + $this->assertSame(null, $e); + $this->assertSame(true, $v); + $invoked++; + }); + + $adapter->resolve(true); + + $adapter->promise()->when(function ($e, $v) use (&$invoked) { + $this->assertSame(null, $e); + $this->assertSame(true, $v); + $invoked++; + }); + $adapter->promise()->when(function ($e, $v) use (&$invoked) { + $this->assertSame(null, $e); + $this->assertSame(true, $v); + $invoked++; + }); + + $this->assertSame(4, $invoked); + } + + public function testAsyncInteropPromiseSuccessForwardToErrorHandler() + { + $exception = new \Exception; + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo($exception)); + + $oldHandler = ErrorHandler::set($mock); + $adapter = $this->getPromiseTestAdapter(); + + $adapter->promise()->when(function () use ($exception) { + throw $exception; + }); + $adapter->resolve(); + + ErrorHandler::set($oldHandler); + } + + public function testAsyncInteropPromiseExceptionFailure() + { + $adapter = $this->getPromiseTestAdapter(); + + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked = true; + }); + $adapter->reject(new \RuntimeException); + $this->assertTrue($invoked); + } + + public function testAsyncInteropPromiseExceptionFailureWithoutReason() + { + $adapter = $this->getPromiseTestAdapter(); + + $adapter->promise()->when(function ($e, $value) use (&$invoked) { + $this->assertInstanceOf("Exception", $e); + $this->assertNull($value); + $invoked = true; + }); + $adapter->reject(); + $this->assertTrue($invoked); + } + + public function testFailureAllWhensExecuted() + { + $adapter = $this->getPromiseTestAdapter(); + $invoked = 0; + + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked++; + }); + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked++; + }); + + $adapter->reject(new \RuntimeException); + + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked++; + }); + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "RuntimeException"); + $invoked++; + }); + + $this->assertSame(4, $invoked); + } + + public function testAsyncInteropPromiseErrorFailure() + { + if (PHP_VERSION_ID < 70000) { + $this->markTestSkipped("Error only exists on PHP 7+"); + } + + $adapter = $this->getPromiseTestAdapter(); + + $adapter->promise()->when(function ($e) use (&$invoked) { + $this->assertSame(get_class($e), "Error"); + $invoked = true; + }); + $adapter->reject(new \Error); + $this->assertTrue($invoked); + } + + public function testAsyncInteropPromiseFailureForwardToErrorHandler() + { + $exception = new \Exception; + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo($exception)); + + $oldHandler = ErrorHandler::set($mock); + $adapter = $this->getPromiseTestAdapter(); + + $adapter->promise()->when(function ($e) { + throw $e; + }); + $adapter->reject($exception); + + ErrorHandler::set($oldHandler); + } +} diff --git a/tests/PromiseTest/FullTestTrait.php b/tests/PromiseTest/FullTestTrait.php index 5a331dd6..b1540f0b 100644 --- a/tests/PromiseTest/FullTestTrait.php +++ b/tests/PromiseTest/FullTestTrait.php @@ -10,5 +10,8 @@ trait FullTestTrait PromiseRejectedTestTrait, ResolveTestTrait, RejectTestTrait, - CancelTestTrait; + CancelTestTrait, + AsyncInteropResolvedTestTrait, + AsyncInteropRejectedTestTrait, + AsyncInteropTestTrait; } diff --git a/tests/RejectedPromiseTest.php b/tests/RejectedPromiseTest.php index bc2e11bd..3e97b885 100644 --- a/tests/RejectedPromiseTest.php +++ b/tests/RejectedPromiseTest.php @@ -7,7 +7,8 @@ class RejectedPromiseTest extends TestCase { use PromiseTest\PromiseSettledTestTrait, - PromiseTest\PromiseRejectedTestTrait; + PromiseTest\PromiseRejectedTestTrait, + PromiseTest\AsyncInteropRejectedTestTrait; public function getPromiseTestAdapter(callable $canceller = null) { @@ -44,4 +45,12 @@ public function shouldThrowExceptionIfConstructedWithAPromise() return new RejectedPromise(new RejectedPromise()); } + + /** @test */ + public function shouldThrowExceptionIfConstructedWithAAsyncInteropPromise() + { + $this->setExpectedException('\InvalidArgumentException'); + + return new RejectedPromise(new SimpleFulfilledAsyncInteropTestPromise()); + } } diff --git a/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php b/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php new file mode 100644 index 00000000..f6184bb3 --- /dev/null +++ b/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php @@ -0,0 +1,13 @@ +exception = $exception; + } + + public function when(callable $onResolved) + { + $onResolved($this->exception, null); + } +} From 82e0d4b87944388f037f2e7046d90551b4ec2301 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Fri, 30 Dec 2016 21:10:23 +0100 Subject: [PATCH 02/11] Use tests from async-interop/promise-test --- composer.json | 3 + tests/AsyncInterop/DeferredTest.php | 20 ++ tests/AsyncInterop/LazyPromiseTest.php | 25 +++ tests/AsyncInterop/PromiseTest.php | 25 +++ tests/FulfilledPromiseTest.php | 3 +- .../AsyncInteropRejectedTestTrait.php | 51 ----- .../AsyncInteropResolvedTestTrait.php | 72 ------- tests/PromiseTest/AsyncInteropTestTrait.php | 186 ------------------ tests/PromiseTest/FullTestTrait.php | 5 +- tests/RejectedPromiseTest.php | 3 +- 10 files changed, 76 insertions(+), 317 deletions(-) create mode 100644 tests/AsyncInterop/DeferredTest.php create mode 100644 tests/AsyncInterop/LazyPromiseTest.php create mode 100644 tests/AsyncInterop/PromiseTest.php delete mode 100644 tests/PromiseTest/AsyncInteropRejectedTestTrait.php delete mode 100644 tests/PromiseTest/AsyncInteropResolvedTestTrait.php delete mode 100644 tests/PromiseTest/AsyncInteropTestTrait.php diff --git a/composer.json b/composer.json index 1bbbc3f5..f491b196 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,9 @@ "php": ">=5.4.0", "async-interop/promise": "^0.3.0" }, + "require-dev": { + "async-interop/promise-test": "dev-master" + }, "autoload": { "psr-4": { "React\\Promise\\": "src/" diff --git a/tests/AsyncInterop/DeferredTest.php b/tests/AsyncInterop/DeferredTest.php new file mode 100644 index 00000000..959b01a9 --- /dev/null +++ b/tests/AsyncInterop/DeferredTest.php @@ -0,0 +1,20 @@ +promise(), + [$d, 'resolve'], + [$d, 'reject'] + ]; + } +} diff --git a/tests/AsyncInterop/LazyPromiseTest.php b/tests/AsyncInterop/LazyPromiseTest.php new file mode 100644 index 00000000..cc11455c --- /dev/null +++ b/tests/AsyncInterop/LazyPromiseTest.php @@ -0,0 +1,25 @@ +promise(); + }; + + return [ + new LazyPromise($factory), + [$d, 'resolve'], + [$d, 'reject'] + ]; + } +} diff --git a/tests/AsyncInterop/PromiseTest.php b/tests/AsyncInterop/PromiseTest.php new file mode 100644 index 00000000..e3c7a9e1 --- /dev/null +++ b/tests/AsyncInterop/PromiseTest.php @@ -0,0 +1,25 @@ +getPromiseTestAdapter(); - - $adapter->reject(new \RuntimeException); - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked = true; - }); - $this->assertTrue($invoked); - } - - public function testWhenOnErrorFailedAsyncInteropPromise() - { - if (PHP_VERSION_ID < 70000) { - $this->markTestSkipped("Error only exists on PHP 7+"); - } - - $adapter = $this->getPromiseTestAdapter(); - $adapter->reject(new \Error); - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "Error"); - $invoked = true; - }); - $this->assertTrue($invoked); - } - - public function testWhenOnExceptionFailedAsyncInteropPromiseWhenRejectedWithoutReason() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->reject(); - $adapter->promise()->when(function ($e, $value) use (&$invoked) { - $this->assertInstanceOf("Exception", $e); - $this->assertNull($value); - $invoked = true; - }); - $this->assertTrue($invoked); - } -} diff --git a/tests/PromiseTest/AsyncInteropResolvedTestTrait.php b/tests/PromiseTest/AsyncInteropResolvedTestTrait.php deleted file mode 100644 index 401ec343..00000000 --- a/tests/PromiseTest/AsyncInteropResolvedTestTrait.php +++ /dev/null @@ -1,72 +0,0 @@ -getPromiseTestAdapter(); - - $adapter->resolve($value); - $adapter->promise()->when(function ($e, $v) use (&$invoked, $value) { - $this->assertSame(null, $e); - $this->assertSame($value, $v); - $invoked = true; - }); - $this->assertTrue($invoked); - } - - /** - * Implementations MAY fail upon resolution with an AsyncInteropPromise, - * but they definitely MUST NOT return an AsyncInteropPromise - */ - public function testAsyncInteropPromiseResolutionWithAsyncInteropPromise() - { - $adapter = $this->getPromiseTestAdapter(); - $adapter->resolve(true); - $success = $adapter->promise(); - - $adapter = $this->getPromiseTestAdapter(); - - $ex = false; - try { - $adapter->resolve($success); - } catch (\Throwable $e) { - $ex = true; - } catch (\Exception $e) { - $ex = true; - } - if (!$ex) { - $adapter->promise()->when(function ($e, $v) use (&$invoked) { - $invoked = true; - $this->assertFalse($v instanceof AsyncInteropPromise); - }); - $this->assertTrue($invoked); - } - } -} diff --git a/tests/PromiseTest/AsyncInteropTestTrait.php b/tests/PromiseTest/AsyncInteropTestTrait.php deleted file mode 100644 index f30b4a19..00000000 --- a/tests/PromiseTest/AsyncInteropTestTrait.php +++ /dev/null @@ -1,186 +0,0 @@ -getPromiseTestAdapter(); - - $adapter->promise()->when(function ($e, $v) use (&$invoked, $value) { - $this->assertSame(null, $e); - $this->assertSame($value, $v); - $invoked = true; - }); - $adapter->resolve($value); - $this->assertTrue($invoked); - } - - public function testSuccessAllWhensExecuted() - { - $adapter = $this->getPromiseTestAdapter(); - $invoked = 0; - - $adapter->promise()->when(function ($e, $v) use (&$invoked) { - $this->assertSame(null, $e); - $this->assertSame(true, $v); - $invoked++; - }); - $adapter->promise()->when(function ($e, $v) use (&$invoked) { - $this->assertSame(null, $e); - $this->assertSame(true, $v); - $invoked++; - }); - - $adapter->resolve(true); - - $adapter->promise()->when(function ($e, $v) use (&$invoked) { - $this->assertSame(null, $e); - $this->assertSame(true, $v); - $invoked++; - }); - $adapter->promise()->when(function ($e, $v) use (&$invoked) { - $this->assertSame(null, $e); - $this->assertSame(true, $v); - $invoked++; - }); - - $this->assertSame(4, $invoked); - } - - public function testAsyncInteropPromiseSuccessForwardToErrorHandler() - { - $exception = new \Exception; - - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo($exception)); - - $oldHandler = ErrorHandler::set($mock); - $adapter = $this->getPromiseTestAdapter(); - - $adapter->promise()->when(function () use ($exception) { - throw $exception; - }); - $adapter->resolve(); - - ErrorHandler::set($oldHandler); - } - - public function testAsyncInteropPromiseExceptionFailure() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked = true; - }); - $adapter->reject(new \RuntimeException); - $this->assertTrue($invoked); - } - - public function testAsyncInteropPromiseExceptionFailureWithoutReason() - { - $adapter = $this->getPromiseTestAdapter(); - - $adapter->promise()->when(function ($e, $value) use (&$invoked) { - $this->assertInstanceOf("Exception", $e); - $this->assertNull($value); - $invoked = true; - }); - $adapter->reject(); - $this->assertTrue($invoked); - } - - public function testFailureAllWhensExecuted() - { - $adapter = $this->getPromiseTestAdapter(); - $invoked = 0; - - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked++; - }); - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked++; - }); - - $adapter->reject(new \RuntimeException); - - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked++; - }); - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "RuntimeException"); - $invoked++; - }); - - $this->assertSame(4, $invoked); - } - - public function testAsyncInteropPromiseErrorFailure() - { - if (PHP_VERSION_ID < 70000) { - $this->markTestSkipped("Error only exists on PHP 7+"); - } - - $adapter = $this->getPromiseTestAdapter(); - - $adapter->promise()->when(function ($e) use (&$invoked) { - $this->assertSame(get_class($e), "Error"); - $invoked = true; - }); - $adapter->reject(new \Error); - $this->assertTrue($invoked); - } - - public function testAsyncInteropPromiseFailureForwardToErrorHandler() - { - $exception = new \Exception; - - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo($exception)); - - $oldHandler = ErrorHandler::set($mock); - $adapter = $this->getPromiseTestAdapter(); - - $adapter->promise()->when(function ($e) { - throw $e; - }); - $adapter->reject($exception); - - ErrorHandler::set($oldHandler); - } -} diff --git a/tests/PromiseTest/FullTestTrait.php b/tests/PromiseTest/FullTestTrait.php index b1540f0b..5a331dd6 100644 --- a/tests/PromiseTest/FullTestTrait.php +++ b/tests/PromiseTest/FullTestTrait.php @@ -10,8 +10,5 @@ trait FullTestTrait PromiseRejectedTestTrait, ResolveTestTrait, RejectTestTrait, - CancelTestTrait, - AsyncInteropResolvedTestTrait, - AsyncInteropRejectedTestTrait, - AsyncInteropTestTrait; + CancelTestTrait; } diff --git a/tests/RejectedPromiseTest.php b/tests/RejectedPromiseTest.php index 3e97b885..e1369116 100644 --- a/tests/RejectedPromiseTest.php +++ b/tests/RejectedPromiseTest.php @@ -7,8 +7,7 @@ class RejectedPromiseTest extends TestCase { use PromiseTest\PromiseSettledTestTrait, - PromiseTest\PromiseRejectedTestTrait, - PromiseTest\AsyncInteropRejectedTestTrait; + PromiseTest\PromiseRejectedTestTrait; public function getPromiseTestAdapter(callable $canceller = null) { From 4b42c6043b08d4d2bd147debdf1854b46ebbb697 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Mon, 2 Jan 2017 20:52:24 +0100 Subject: [PATCH 03/11] Make PromiseInterface extend Interop\Async\Promise --- src/FulfilledPromise.php | 4 ++-- src/LazyPromise.php | 4 +--- src/Promise.php | 6 ++---- src/PromiseInterface.php | 4 +++- src/RejectedPromise.php | 4 ++-- tests/FulfilledPromiseTest.php | 2 +- tests/fixtures/SimpleFulfilledTestPromise.php | 5 +++++ tests/fixtures/SimpleRejectedTestPromise.php | 5 +++++ 8 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index f036ddec..d228d5d5 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -4,13 +4,13 @@ use Interop\Async\Promise as AsyncInteropPromise; -class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise +class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface { private $value; public function __construct($value = null) { - if ($value instanceof PromiseInterface || $value instanceof AsyncInteropPromise) { + if ($value instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.'); } diff --git a/src/LazyPromise.php b/src/LazyPromise.php index 60c3db14..1eed039d 100644 --- a/src/LazyPromise.php +++ b/src/LazyPromise.php @@ -2,9 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; - -class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise +class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface { private $factory; private $promise; diff --git a/src/Promise.php b/src/Promise.php index a3453416..994b3ea9 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -2,9 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; - -class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise +class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface { private $canceller; private $result; @@ -95,7 +93,7 @@ public function when(callable $onResolved) return $this->result()->when($onResolved); } - $this->handlers[] = function (AsyncInteropPromise $promise) use ($onResolved) { + $this->handlers[] = function (PromiseInterface $promise) use ($onResolved) { $promise->when($onResolved); }; } diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index da138e8f..f42e6852 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -2,7 +2,9 @@ namespace React\Promise; -interface PromiseInterface +use Interop\Async\Promise as AsyncInteropPromise; + +interface PromiseInterface extends AsyncInteropPromise { /** * @return PromiseInterface diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index e9eee748..cbe1b8df 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -4,13 +4,13 @@ use Interop\Async\Promise as AsyncInteropPromise; -class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface, AsyncInteropPromise +class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface { private $reason; public function __construct($reason = null) { - if ($reason instanceof PromiseInterface || $reason instanceof AsyncInteropPromise) { + if ($reason instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.'); } diff --git a/tests/FulfilledPromiseTest.php b/tests/FulfilledPromiseTest.php index 1387c24c..4166b897 100644 --- a/tests/FulfilledPromiseTest.php +++ b/tests/FulfilledPromiseTest.php @@ -50,6 +50,6 @@ public function shouldThrowExceptionIfConstructedWithAAsyncInteropPromise() { $this->setExpectedException('\InvalidArgumentException'); - return new FulfilledPromise(new FulfilledPromise()); + return new FulfilledPromise(new SimpleFulfilledAsyncInteropTestPromise()); } } diff --git a/tests/fixtures/SimpleFulfilledTestPromise.php b/tests/fixtures/SimpleFulfilledTestPromise.php index 92995225..4c3077c3 100644 --- a/tests/fixtures/SimpleFulfilledTestPromise.php +++ b/tests/fixtures/SimpleFulfilledTestPromise.php @@ -18,4 +18,9 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) return new RejectedPromise($exception); } } + + public function when(callable $onResolved) + { + $onResolved(null, 'foo'); + } } diff --git a/tests/fixtures/SimpleRejectedTestPromise.php b/tests/fixtures/SimpleRejectedTestPromise.php index 0614f120..9ed9bac4 100644 --- a/tests/fixtures/SimpleRejectedTestPromise.php +++ b/tests/fixtures/SimpleRejectedTestPromise.php @@ -18,4 +18,9 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) return new RejectedPromise($exception); } } + + public function when(callable $onResolved) + { + $onResolved('foo'); + } } From 91ab8a407118c0892ec863bc61990d5c13408fd7 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Sat, 7 Jan 2017 17:03:00 +0100 Subject: [PATCH 04/11] Upgrade to async-interop/promise ^0.4.0 --- composer.json | 4 ++-- src/FulfilledPromise.php | 2 +- src/PromiseInterface.php | 2 +- src/RejectedPromise.php | 2 +- src/functions.php | 2 +- tests/AsyncInterop/DeferredTest.php | 2 +- tests/AsyncInterop/LazyPromiseTest.php | 2 +- tests/AsyncInterop/PromiseTest.php | 2 +- tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php | 2 +- tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index f491b196..c169aaf7 100644 --- a/composer.json +++ b/composer.json @@ -7,10 +7,10 @@ ], "require": { "php": ">=5.4.0", - "async-interop/promise": "^0.3.0" + "async-interop/promise": "^0.4.0" }, "require-dev": { - "async-interop/promise-test": "dev-master" + "async-interop/promise-test": "^0.4.1" }, "autoload": { "psr-4": { diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index d228d5d5..74df9050 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface { diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index f42e6852..e02caac2 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; interface PromiseInterface extends AsyncInteropPromise { diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index cbe1b8df..9853a6ce 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface { diff --git a/src/functions.php b/src/functions.php index 8e207ed4..5f287398 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; function resolve($promiseOrValue = null) { diff --git a/tests/AsyncInterop/DeferredTest.php b/tests/AsyncInterop/DeferredTest.php index 959b01a9..4d5e19d1 100644 --- a/tests/AsyncInterop/DeferredTest.php +++ b/tests/AsyncInterop/DeferredTest.php @@ -2,7 +2,7 @@ namespace React\Promise\AsyncInterop; -use Interop\Async\Promise\Test; +use AsyncInterop\Promise\Test; use React\Promise\Deferred; class DeferredTest extends Test diff --git a/tests/AsyncInterop/LazyPromiseTest.php b/tests/AsyncInterop/LazyPromiseTest.php index cc11455c..c84ca694 100644 --- a/tests/AsyncInterop/LazyPromiseTest.php +++ b/tests/AsyncInterop/LazyPromiseTest.php @@ -2,7 +2,7 @@ namespace React\Promise\AsyncInterop; -use Interop\Async\Promise\Test; +use AsyncInterop\Promise\Test; use React\Promise\Deferred; use React\Promise\LazyPromise; diff --git a/tests/AsyncInterop/PromiseTest.php b/tests/AsyncInterop/PromiseTest.php index e3c7a9e1..2df5709d 100644 --- a/tests/AsyncInterop/PromiseTest.php +++ b/tests/AsyncInterop/PromiseTest.php @@ -2,7 +2,7 @@ namespace React\Promise\AsyncInterop; -use Interop\Async\Promise\Test; +use AsyncInterop\Promise\Test; use React\Promise\Promise; class PromiseTest extends Test diff --git a/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php b/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php index f6184bb3..3b43a6ab 100644 --- a/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php +++ b/tests/fixtures/SimpleFulfilledAsyncInteropTestPromise.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; class SimpleFulfilledAsyncInteropTestPromise implements AsyncInteropPromise { diff --git a/tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php b/tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php index a037f5e3..6891c41b 100644 --- a/tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php +++ b/tests/fixtures/SimpleRejectedAsyncInteropTestPromise.php @@ -2,7 +2,7 @@ namespace React\Promise; -use Interop\Async\Promise as AsyncInteropPromise; +use AsyncInterop\Promise as AsyncInteropPromise; class SimpleRejectedAsyncInteropTestPromise implements AsyncInteropPromise { From cb77cb51eafe90fdba8b82171ff8be2dbbc28e02 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Sun, 8 Jan 2017 19:47:04 +0100 Subject: [PATCH 05/11] Revert "Make PromiseInterface extend Interop\Async\Promise" This reverts commit 4b42c6043b08d4d2bd147debdf1854b46ebbb697. --- src/FulfilledPromise.php | 4 ++-- src/LazyPromise.php | 4 +++- src/Promise.php | 6 ++++-- src/PromiseInterface.php | 4 +--- src/RejectedPromise.php | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index 8108b252..91f23c44 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -4,13 +4,13 @@ use AsyncInterop\Promise as AsyncInteropPromise; -final class FulfilledPromise implements PromiseInterface +final class FulfilledPromise implements PromiseInterface, AsyncInteropPromise { private $value; public function __construct($value = null) { - if ($value instanceof AsyncInteropPromise) { + if ($value instanceof PromiseInterface || $value instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.'); } diff --git a/src/LazyPromise.php b/src/LazyPromise.php index fd087f51..1f788fef 100644 --- a/src/LazyPromise.php +++ b/src/LazyPromise.php @@ -2,7 +2,9 @@ namespace React\Promise; -final class LazyPromise implements PromiseInterface +use AsyncInterop\Promise as AsyncInteropPromise; + +final class LazyPromise implements PromiseInterface, AsyncInteropPromise { private $factory; private $promise; diff --git a/src/Promise.php b/src/Promise.php index 838670ad..e253c66b 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -2,7 +2,9 @@ namespace React\Promise; -final class Promise implements PromiseInterface +use AsyncInterop\Promise as AsyncInteropPromise; + +final class Promise implements PromiseInterface, AsyncInteropPromise { private $canceller; private $result; @@ -93,7 +95,7 @@ public function when(callable $onResolved) return $this->result()->when($onResolved); } - $this->handlers[] = function (PromiseInterface $promise) use ($onResolved) { + $this->handlers[] = function (AsyncInteropPromise $promise) use ($onResolved) { $promise->when($onResolved); }; } diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index 137a9175..f926cda5 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -2,9 +2,7 @@ namespace React\Promise; -use AsyncInterop\Promise as AsyncInteropPromise; - -interface PromiseInterface extends AsyncInteropPromise +interface PromiseInterface { /** * @return PromiseInterface diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index 5da45088..c4d34739 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -4,13 +4,13 @@ use AsyncInterop\Promise as AsyncInteropPromise; -final class RejectedPromise implements PromiseInterface +final class RejectedPromise implements PromiseInterface, AsyncInteropPromise { private $reason; public function __construct($reason = null) { - if ($reason instanceof AsyncInteropPromise) { + if ($reason instanceof PromiseInterface || $reason instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.'); } From 53ce643a51de4c45bdd71d8a8767d8a2339b4940 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Tue, 17 Jan 2017 19:50:08 +0100 Subject: [PATCH 06/11] Add async-interop/promise compatibility documentation --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 2490c83b..065df188 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Table of Contents * [map()](#map) * [reduce()](#reduce) * [PromisorInterface](#promisorinterface) + * [AsyncInterop\Promise compatibility](#asyncinteroppromise-compatibility) 4. [Examples](#examples) * [How to use Deferred](#how-to-use-deferred) * [How promise forwarding works](#how-promise-forwarding-works) @@ -158,6 +159,11 @@ Neither its state nor its result (or error) can be modified. * [RejectedPromise](#rejectedpromise) * [LazyPromise](#lazypromise) +All implementations also implement the +[`AsyncInterop\Promise`](https://github.com/async-interop/promise) interface. +See [`AsyncInterop\Promise` compatibility](#asyncinteroppromise-compatibility), +for further information. + #### PromiseInterface::then() ```php @@ -512,6 +518,26 @@ The `React\Promise\PromisorInterface` provides a common interface for objects that provide a promise. `React\Promise\Deferred` implements it, but since it is part of the public API anyone can implement it. +### AsyncInterop\Promise compatibility + +All promise implementations from React/Promise also implement the +[`AsyncInterop\Promise`](https://github.com/async-interop/promise) interface and +its `when()` method. + +The `AsyncInterop\Promise` is intended for interoperability with other libraries +and their combinator and conversion functions. + +Note, that although the interface is named *Promise*, it has nothing to do with +promises commonly known from the +[CommonJS Promises/A](http://wiki.commonjs.org/wiki/Promises/A), +[Promises/A+](https://promisesaplus.com) or +[ECMAScript 2015 (ES6)](http://www.ecma-international.org/ecma-262/6.0/#sec-promise-constructor) +specifications. + +If you're using React/Promise as your primary value placeholder implementation, +it is recommended to **not** use the `when()` method directly but the methods +provided by the [`PromiseInterface`](#promiseinterface). + Examples -------- From b9c518331ab50bf2057e29e84f18ab97926453c9 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Tue, 17 Jan 2017 20:06:49 +0100 Subject: [PATCH 07/11] Make PromiseInterface extend AsyncInterop\Promise again --- README.md | 19 ++++++++++--------- src/FulfilledPromise.php | 4 ++-- src/LazyPromise.php | 4 +--- src/Promise.php | 6 ++---- src/PromiseInterface.php | 4 +++- src/RejectedPromise.php | 4 ++-- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 065df188..26874f49 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,11 @@ and an associated value, or rejection (failure) and an associated reason. Once in the fulfilled or rejected state, a promise becomes immutable. Neither its state nor its result (or error) can be modified. +This interface extends the +[`AsyncInterop\Promise`](https://github.com/async-interop/promise) interface. +See [`AsyncInterop\Promise` compatibility](#asyncinteroppromise-compatibility), +for further information. + #### Implementations * [Promise](#promise-1) @@ -159,11 +164,6 @@ Neither its state nor its result (or error) can be modified. * [RejectedPromise](#rejectedpromise) * [LazyPromise](#lazypromise) -All implementations also implement the -[`AsyncInterop\Promise`](https://github.com/async-interop/promise) interface. -See [`AsyncInterop\Promise` compatibility](#asyncinteroppromise-compatibility), -for further information. - #### PromiseInterface::then() ```php @@ -520,12 +520,13 @@ is part of the public API anyone can implement it. ### AsyncInterop\Promise compatibility -All promise implementations from React/Promise also implement the +The [`PromiseInterface`](#promiseinterface) extends the [`AsyncInterop\Promise`](https://github.com/async-interop/promise) interface and -its `when()` method. +thereby all promise implementations from React/Promise implement its `when()` +method. -The `AsyncInterop\Promise` is intended for interoperability with other libraries -and their combinator and conversion functions. +The `AsyncInterop\Promise` interface is intended for interoperability with other +libraries and their combinator and conversion functions. Note, that although the interface is named *Promise*, it has nothing to do with promises commonly known from the diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index 91f23c44..8108b252 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -4,13 +4,13 @@ use AsyncInterop\Promise as AsyncInteropPromise; -final class FulfilledPromise implements PromiseInterface, AsyncInteropPromise +final class FulfilledPromise implements PromiseInterface { private $value; public function __construct($value = null) { - if ($value instanceof PromiseInterface || $value instanceof AsyncInteropPromise) { + if ($value instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.'); } diff --git a/src/LazyPromise.php b/src/LazyPromise.php index 588ee385..f735b121 100644 --- a/src/LazyPromise.php +++ b/src/LazyPromise.php @@ -2,9 +2,7 @@ namespace React\Promise; -use AsyncInterop\Promise as AsyncInteropPromise; - -final class LazyPromise implements PromiseInterface, AsyncInteropPromise +final class LazyPromise implements PromiseInterface { private $factory; private $promise; diff --git a/src/Promise.php b/src/Promise.php index a3a1f3cc..da622f30 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -2,9 +2,7 @@ namespace React\Promise; -use AsyncInterop\Promise as AsyncInteropPromise; - -final class Promise implements PromiseInterface, AsyncInteropPromise +final class Promise implements PromiseInterface { private $canceller; private $result; @@ -94,7 +92,7 @@ public function when(callable $onResolved) return $this->result()->when($onResolved); } - $this->handlers[] = function (AsyncInteropPromise $promise) use ($onResolved) { + $this->handlers[] = function (PromiseInterface $promise) use ($onResolved) { $promise->when($onResolved); }; } diff --git a/src/PromiseInterface.php b/src/PromiseInterface.php index f926cda5..137a9175 100644 --- a/src/PromiseInterface.php +++ b/src/PromiseInterface.php @@ -2,7 +2,9 @@ namespace React\Promise; -interface PromiseInterface +use AsyncInterop\Promise as AsyncInteropPromise; + +interface PromiseInterface extends AsyncInteropPromise { /** * @return PromiseInterface diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index c4d34739..5da45088 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -4,13 +4,13 @@ use AsyncInterop\Promise as AsyncInteropPromise; -final class RejectedPromise implements PromiseInterface, AsyncInteropPromise +final class RejectedPromise implements PromiseInterface { private $reason; public function __construct($reason = null) { - if ($reason instanceof PromiseInterface || $reason instanceof AsyncInteropPromise) { + if ($reason instanceof AsyncInteropPromise) { throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.'); } From 40fa021f63310020fa93e201f673379cad88f868 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Wed, 18 Jan 2017 08:10:10 +0100 Subject: [PATCH 08/11] Document AsyncInterop\Promise support of resolve() --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 26874f49..b1a4cb21 100644 --- a/README.md +++ b/README.md @@ -408,6 +408,10 @@ Creates a promise for the supplied `$promiseOrValue`. If `$promiseOrValue` is a value, it will be the resolution value of the returned promise. +If `$promiseOrValue` is a +[`AsyncInterop\Promise`](https://github.com/async-interop/promise), a trusted +promise that follows the state of the `AsyncInterop\Promise` is returned. + If `$promiseOrValue` is a thenable (any object that provides a `then()` method), a trusted promise that follows the state of the thenable is returned. From c2fbbcf715240af5a972cbe26fc30cedc7197b39 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 19 Jan 2017 20:45:45 +0100 Subject: [PATCH 09/11] Add examples for resolve() --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index b1a4cb21..d36aca97 100644 --- a/README.md +++ b/README.md @@ -408,15 +408,48 @@ Creates a promise for the supplied `$promiseOrValue`. If `$promiseOrValue` is a value, it will be the resolution value of the returned promise. +```php +React\Promise\resolve(1)->then(function ($value) { + assert(1 === $value); +}); +``` + If `$promiseOrValue` is a [`AsyncInterop\Promise`](https://github.com/async-interop/promise), a trusted promise that follows the state of the `AsyncInterop\Promise` is returned. +```php +$asyncInteropPromise = getAsyncInteropPromiseWhichResolvesTo1(); + +assert($asyncInteropPromise instanceof AsyncInterop\Promise); + +React\Promise\resolve($asyncInteropPromise)->then(function ($value) { + assert(1 === $value); +}); +``` + If `$promiseOrValue` is a thenable (any object that provides a `then()` method), a trusted promise that follows the state of the thenable is returned. +```php +$thenable = getThenableWhichResolvesTo1(); + +assert(method_exists($thenable, 'then')); + +React\Promise\resolve($asyncInteropPromise)->then(function ($value) { + assert(1 === $value); +}); +``` + If `$promiseOrValue` is a promise, it will be returned as is. +```php +$promise1 = React\Promise\resolve(1); +$promise2 = React\Promise\resolve($promise1); + +assert($promise1 === $promise2); +``` + #### reject() ```php From b7efdff1fa0262704663f92a7964b690bca0a84e Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 19 Jan 2017 20:50:56 +0100 Subject: [PATCH 10/11] Reword paragraph about high level consumption methods over when() --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d36aca97..0edfde67 100644 --- a/README.md +++ b/README.md @@ -573,8 +573,9 @@ promises commonly known from the specifications. If you're using React/Promise as your primary value placeholder implementation, -it is recommended to **not** use the `when()` method directly but the methods -provided by the [`PromiseInterface`](#promiseinterface). +it is recommended to **not** use the `when()` method directly but the high level +consumption methods `then()`, `done()`, `otherwise()` and `always()` to consume +a promise's value or reason. Examples -------- From 0c8a3fd3b463b73d359bca2ffe0c4510a7164f28 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 19 Jan 2017 23:40:51 +0100 Subject: [PATCH 11/11] Tweak and add examples to AsyncInterop\Promise compatibility chapter --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0edfde67..d0ba6b85 100644 --- a/README.md +++ b/README.md @@ -565,8 +565,8 @@ method. The `AsyncInterop\Promise` interface is intended for interoperability with other libraries and their combinator and conversion functions. -Note, that although the interface is named *Promise*, it has nothing to do with -promises commonly known from the +Note, that although the `AsyncInterop\Promise` interface is named *Promise*, +it's API is different from promises commonly known from the [CommonJS Promises/A](http://wiki.commonjs.org/wiki/Promises/A), [Promises/A+](https://promisesaplus.com) or [ECMAScript 2015 (ES6)](http://www.ecma-international.org/ecma-262/6.0/#sec-promise-constructor) @@ -577,6 +577,48 @@ it is recommended to **not** use the `when()` method directly but the high level consumption methods `then()`, `done()`, `otherwise()` and `always()` to consume a promise's value or reason. +#### Examples + +You can cast every foreign promise implementing `AsyncInterop\Promise` to a +trusted promise by passing it through [`resolve()`](#resolve). + +It is also possible to return an `AsyncInterop\Promise` instance inside promise +chains. + +```php +$asyncInteropPromise = OtherLib\doAsyncTaskAndReturnAsyncInteropPromise(); + +$reactPromise = React\Promise\resolve($asyncInteropPromise) + ->then(function ($value) { + // You can also return `AsyncInterop\Promise` instances + // from promise handlers + return NextLib\transformValueAndReturnAsyncInteropPromise($value); + }) + ->then(function ($transformedValue) { + + }) +; +``` + +Since promises from React/Promise also implement the `AsyncInterop\Promise` +interface, you can use these promises with combinator, conversion and coroutine +implementations of other libraries which support `AsyncInterop\Promise`. + +```php +$observable = Observable::fromPromise( + $reactSocketClient->connect('google.com:443') +); + +coroutine(function() { + $promiseArray = [ + $reactSocketClient->connect('google.com:443'), + $reactSocketClient->connect('google.de:443') + ]; + + $stream = (yield first($promiseArray)); +}); +``` + Examples --------