From 6bcf2cb664159e09eef2d668e88b3fa7b908e2fa Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Wed, 23 Nov 2016 21:25:27 +0100 Subject: [PATCH] Support foreign thenables in reject() --- src/functions.php | 2 +- tests/FunctionRejectTest.php | 18 ++++++++++++++++ tests/fixtures/SimpleRejectedTestThenable.php | 21 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/SimpleRejectedTestThenable.php diff --git a/src/functions.php b/src/functions.php index 4f49b011..f1856a20 100644 --- a/src/functions.php +++ b/src/functions.php @@ -25,7 +25,7 @@ function resolve($promiseOrValue = null) function reject($promiseOrValue = null) { - if ($promiseOrValue instanceof PromiseInterface) { + if (method_exists($promiseOrValue, 'then')) { return resolve($promiseOrValue)->then(function ($value) { return new RejectedPromise($value); }); diff --git a/tests/FunctionRejectTest.php b/tests/FunctionRejectTest.php index 84b8ec6a..127a78bd 100644 --- a/tests/FunctionRejectTest.php +++ b/tests/FunctionRejectTest.php @@ -61,4 +61,22 @@ public function shouldRejectARejectedPromise() $mock ); } + + /** @test */ + public function shouldRejectAThenable() + { + $thenable = new SimpleRejectedTestThenable(); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo('foo')); + + reject($thenable) + ->then( + $this->expectCallableNever(), + $mock + ); + } } diff --git a/tests/fixtures/SimpleRejectedTestThenable.php b/tests/fixtures/SimpleRejectedTestThenable.php new file mode 100644 index 00000000..812f1081 --- /dev/null +++ b/tests/fixtures/SimpleRejectedTestThenable.php @@ -0,0 +1,21 @@ +