Skip to content

Commit

Permalink
Update test suite to improve PHP 8.4+ support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 22, 2024
1 parent 9dcf6c6 commit 6b19f37
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/FunctionAnyTestRejectedShouldReportUnhandled.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Calling any() with rejected promises should report unhandled rejection
--SKIPIF--
<?php if (PHP_VERSION_ID > 80300) die("Skipped: PHP < 8.4 only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
Expand Down
30 changes: 30 additions & 0 deletions tests/FunctionAnyTestRejectedShouldReportUnhandledOnPhp84.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Calling any() with rejected promises should report unhandled rejection
--SKIPIF--
<?php if (PHP_VERSION_ID < 80400) die("Skipped: PHP 8.4+ only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
--FILE--
<?php

use function React\Promise\any;
use function React\Promise\reject;

require __DIR__ . '/../vendor/autoload.php';

any([
reject(new RuntimeException('foo')),
reject(new RuntimeException('bar'))
]);

?>
--EXPECTF--
Unhandled promise rejection with React\Promise\Exception\CompositeException: All promises rejected. in %s:%d
Stack trace:
#0 %s/src/Promise.php(%d): {closure:%s:%d}(%S)
#1 %s/src/Promise.php(%d): React\Promise\Promise->call(%S)
#2 %s/src/functions.php(%d): React\Promise\Promise->__construct(%S)
#3 %s(%d): React\Promise\any(%S)
#4 %A
#5 {main}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Calling reject() and then finally() should call handler and report unhandled rejection for new exception from handler
--SKIPIF--
<?php if (PHP_VERSION_ID > 80300) die("Skipped: PHP < 8.4 only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Calling reject() and then finally() should call handler and report unhandled rejection for new exception from handler
--SKIPIF--
<?php if (PHP_VERSION_ID < 80400) die("Skipped: PHP 8.4+ only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
--FILE--
<?php

use function React\Promise\reject;

require __DIR__ . '/../vendor/autoload.php';

reject(new RuntimeException('foo'))->finally(function (): void {
throw new \RuntimeException('Finally!');
});

?>
--EXPECTF--
Unhandled promise rejection with RuntimeException: Finally! in %s:%d
Stack trace:
#0 %s/src/Internal/RejectedPromise.php(%d): {closure:%s:%d}(%S)
#1 %s/src/Internal/RejectedPromise.php(%d): React\Promise\Internal\RejectedPromise->{closure:%s:%d}(%S)
#2 %s/src/Internal/RejectedPromise.php(%d): React\Promise\Internal\RejectedPromise->then(%S)
#3 %s(%d): React\Promise\Internal\RejectedPromise->finally(%S)
#4 %A
#5 {main}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Calling reject() and then then() should report unhandled rejection for new exception from handler
--SKIPIF--
<?php if (PHP_VERSION_ID > 80300) die("Skipped: PHP < 8.4 only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Calling reject() and then then() should report unhandled rejection for new exception from handler
--SKIPIF--
<?php if (PHP_VERSION_ID < 80400) die("Skipped: PHP 8.4+ only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
--FILE--
<?php

use function React\Promise\reject;

require __DIR__ . '/../vendor/autoload.php';

reject(new RuntimeException('foo'))->then(null, function () {
throw new \RuntimeException('bar');
});

?>
--EXPECTF--
Unhandled promise rejection with RuntimeException: bar in %s:%d
Stack trace:
#0 %s/src/Internal/RejectedPromise.php(%d): {closure:%s:%d}(%S)
#1 %s(%d): React\Promise\Internal\RejectedPromise->then(%S)
#2 %A
#3 {main}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--TEST--
Calling reject() and then then() with invalid type should report unhandled rejection for TypeError
--SKIPIF--
<?php if (PHP_VERSION_ID < 80000) die("Skipped: PHP 8+ only."); ?>
<?php if (PHP_VERSION_ID < 80000 || PHP_VERSION_ID > 80300) die("Skipped: PHP 8.0 - PHP 8.3 only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Calling reject() and then then() with invalid type should report unhandled rejection for TypeError
--SKIPIF--
<?php if (PHP_VERSION_ID < 80400) die("Skipped: PHP 8.4+ only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
--FILE--
<?php

use function React\Promise\reject;

require __DIR__ . '/../vendor/autoload.php';

reject(new RuntimeException('foo'))->then(null, function (UnexpectedValueException $unexpected): void { // @phpstan-ignore-line
echo 'This will never be shown because the types do not match' . PHP_EOL;
});

?>
--EXPECTF--
Unhandled promise rejection with TypeError: {closure:%s:%d}(): Argument #1 ($unexpected) must be of type UnexpectedValueException, RuntimeException given, called in %s/src/Internal/RejectedPromise.php on line %d in %s:%d
Stack trace:
#0 %s/src/Internal/RejectedPromise.php(%d): {closure:%s:%d}(%S)
#1 %s(%d): React\Promise\Internal\RejectedPromise->then(%S)
#2 %A
#3 {main}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--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
--SKIPIF--
<?php if (PHP_VERSION_ID > 80300) die("Skipped: PHP < 8.4 only."); ?>
--INI--
# suppress legacy PHPUnit 7 warning for Xdebug 3
xdebug.default_enable=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--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
--SKIPIF--
<?php if (PHP_VERSION_ID < 80400) die("Skipped: PHP 8.4+ only."); ?>
--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:%d}(%S)
#1 %s(%d): trigger_error(%S)
#2 %s/src/Internal/RejectedPromise.php(%d): {closure:%s:%d}(%S)
#3 %s/src/functions.php(%d): React\Promise\Internal\RejectedPromise->__destruct()
#4 %s(%d): React\Promise\reject(%S)
#5 %A
#6 {main}

0 comments on commit 6b19f37

Please sign in to comment.