Skip to content

Commit 82b588c

Browse files
committed
wip: test unhandled rejections
1 parent 6f23097 commit 82b588c

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

src/Promise.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -219,43 +219,34 @@ private function handleThen(ThenChain $then):bool {
219219

220220
private function handleFinally(FinallyChain $finally):bool {
221221
if($this->getState() === PromiseState::RESOLVED) {
222-
try {
223-
$result = $finally->callOnResolved($this->resolvedValue);
224-
if($result instanceof PromiseInterface) {
225-
$this->chainPromise($result);
226-
}
227-
elseif(is_null($result)) {
228-
$this->stopChain = true;
229-
return true;
230-
}
231-
else {
232-
$this->resolvedValue = $result;
233-
$this->resolvedValueSet = true;
234-
$this->tryComplete();
235-
}
222+
$result = $finally->callOnResolved($this->resolvedValue);
223+
if($result instanceof PromiseInterface) {
224+
$this->chainPromise($result);
236225
}
237-
catch(Throwable $reason) {
238-
$this->reject($reason);
226+
elseif(is_null($result)) {
227+
$this->stopChain = true;
228+
return true;
239229
}
230+
else {
231+
$this->resolvedValue = $result;
232+
$this->resolvedValueSet = true;
233+
$this->tryComplete();
234+
}
235+
240236
}
241237
elseif($this->getState() === PromiseState::REJECTED) {
242-
try {
243-
$result = $finally->callOnRejected($this->rejectedReason);
244-
if($result instanceof PromiseInterface) {
245-
$this->chainPromise($result);
246-
}
247-
elseif(is_null($result)) {
248-
$this->stopChain = true;
249-
return true;
250-
}
251-
else {
252-
$this->resolvedValue = $result;
253-
$this->resolvedValueSet = true;
254-
$this->tryComplete();
255-
}
238+
$result = $finally->callOnRejected($this->rejectedReason);
239+
if($result instanceof PromiseInterface) {
240+
$this->chainPromise($result);
256241
}
257-
catch(Throwable $reason) {
258-
$this->reject($reason);
242+
elseif(is_null($result)) {
243+
$this->stopChain = true;
244+
return true;
245+
}
246+
else {
247+
$this->resolvedValue = $result;
248+
$this->resolvedValueSet = true;
249+
$this->tryComplete();
259250
}
260251
}
261252

test/phpunit/PromiseTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,23 @@ public function testFinallyCanReturnPromise() {
474474
self::assertSame("First return", $finallyLog[1]);
475475
}
476476

477-
public function testOnRejectedCalledWhenFinallyThrows() {
477+
public function testOnRejectedNotCalledWhenFinallyThrows() {
478478
$exception = new PromiseException("Oh dear, oh dear");
479+
$actualException = null;
479480
$promiseContainer = $this->getTestPromiseContainer();
480481

481482
self::expectException(PromiseException::class);
482483
self::expectExceptionMessage("Oh dear, oh dear");
483484
$sut = $promiseContainer->getPromise();
484-
$sut->finally(function() use ($exception) {
485+
$sut->finally(function(string $message) use ($exception) {
485486
throw $exception;
486-
})->then(
487-
self::mockCallable(1, "Example resolution"),
488-
self::mockCallable(0)
489-
);
487+
})->then(function(string $message) {
488+
return "$message-appended";
489+
})->catch(function(Throwable $reason) use(&$actualException) {
490+
$actualException = $reason;
491+
});
490492
$promiseContainer->resolve("Example resolution");
493+
self::assertNull($actualException);
491494
}
492495

493496
public function testGetStatePending() {

0 commit comments

Comments
 (0)