-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
set_rejection_handler()
function
- Loading branch information
Showing
10 changed files
with
322 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/FunctionSetRejectionHandlerShouldBeInvokedForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
Unhandled RuntimeException: foo | ||
done |
37 changes: 37 additions & 0 deletions
37
tests/FunctionSetRejectionHandlerShouldInvokeLastHandlerForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
The callback given to the last set_rejection_handler() should be invoked for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$ret = set_rejection_handler($first = function (Throwable $e): void { | ||
echo 'THIS WILL NEVER BE CALLED' . PHP_EOL; | ||
}); | ||
|
||
// previous should be null | ||
var_dump($ret === null); | ||
|
||
$ret = set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
// previous rejection handler should be first rejection handler callback | ||
var_dump($ret === $first); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) | ||
Unhandled RuntimeException: foo | ||
done |
30 changes: 30 additions & 0 deletions
30
tests/FunctionSetRejectionHandlerThatHasUnhandledShouldReportUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for outer unhandled rejection but should use default rejection handler for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
reject(new \UnexpectedValueException('bar')); | ||
|
||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECTF-- | ||
Unhandled promise rejection with UnexpectedValueException: bar in %s:%d | ||
Stack trace: | ||
#0 %A{main} | ||
Unhandled RuntimeException: foo | ||
done |
26 changes: 26 additions & 0 deletions
26
tests/FunctionSetRejectionHandlerThatThrowsShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should not throw an exception or the program should terminate for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
throw new \UnexpectedValueException('This function should never throw'); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught UnexpectedValueException from unhandled promise rejection handler: This function should never throw in %s:%d | ||
Stack trace: | ||
#0 %A{main} |
24 changes: 24 additions & 0 deletions
24
...ctionSetRejectionHandlerThatTriggersDefaultHandlerShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() may trigger a fatal error for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
trigger_error('Unexpected ' . get_class($e) . ': ' .$e->getMessage(), E_USER_ERROR); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Unexpected RuntimeException: foo in %s line %d |
35 changes: 35 additions & 0 deletions
35
...RejectionHandlerThatTriggersErrorHandlerThatThrowsShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() may trigger a fatal error which in turn throws an exception which will terminate the program for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_error_handler(function (int $_, string $errstr): void { | ||
throw new \OverflowException('This function should never throw'); | ||
}); | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
trigger_error($e->getMessage(), E_USER_ERROR); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught OverflowException from unhandled promise rejection handler: This function should never throw in %s:%d | ||
Stack trace: | ||
#0 [internal function]: {closure}(%S) | ||
#1 %s(%d): trigger_error(%S) | ||
#2 %s/src/Internal/RejectedPromise.php(%d): {closure}(%S) | ||
#3 %s/src/functions.php(%d): React\Promise\Internal\RejectedPromise->__destruct() | ||
#4 %s(%d): React\Promise\reject(%S) | ||
#5 %A{main} |
36 changes: 36 additions & 0 deletions
36
...ejectionHandlerThatUsesNestedSetRejectionHandlerShouldInvokeInnerHandlerForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for outer unhandled rejection and may set new rejection handler for inner unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
$ret = set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled inner ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
// previous rejection handler should be unset while handling a rejection | ||
var_dump($ret === null); | ||
|
||
reject(new \UnexpectedValueException('bar')); | ||
|
||
echo 'Unhandled outer ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
Unhandled inner UnexpectedValueException: bar | ||
Unhandled outer RuntimeException: foo | ||
done |