Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
18 changes: 18 additions & 0 deletions tests/FunctionRejectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/SimpleRejectedTestThenable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace React\Promise;

class SimpleRejectedTestThenable
{
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
try {
if ($onRejected) {
$onRejected('foo');
}

return new self();
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
}