wyrihaximus/react-phpunit-run-tests-in-fiber
PHPUnit trait to run each test inside a PHP Fiber, making it easy to use ReactPHP async/await in tests. Includes optional per-test or per-class timeout attributes to fail slow tests (without stopping the running fiber).
Install the package in your Laravel project’s dev dependencies:
composer require --dev wyrihaximus/react-phpunit-run-tests-in-fiber
Prerequisites: PHP 8.4+, PHPUnit 12.3+, and react/async (already required implicitly via wyrihaximus/react-phpunit-run-tests-in-fiber).
Start by adding the trait to one test class that needs to test await()-based async logic:
use PHPUnit\Framework\TestCase;
use WyriHaximus\React\PHPUnit\RunTestsInFibersTrait;
final class AsyncServiceTest extends TestCase
{
use RunTestsInFibersTrait;
#[test]
public function itRunsAwaitInFiber(): void
{
// Your await() calls now work here
$result = \React\Async\await(someAsyncPromise());
$this->assertTrue($result);
}
}
💡 First use case: Testing ReactPHP-based services (e.g., non-blocking HTTP clients, event loops) that require
await()but would otherwise fail with "Cannot await outside fiber" errors.
react/http-client or react/socket without manual loop setup.#[\WyriHaximus\React\PHPUnit\TimeOut(10)] // Default 10s timeout
final class AsyncServiceTest extends TestCase
{
use RunTestsInFibersTrait;
#[\WyriHaximus\React\PHPUnit\TimeOut(1)] // Override for slow ops
public function itTimesOutOnSlowOperation(): void { ... }
}
loop->addTimer()), since the trait manages the loop lifecycle around each test.Promise\timeout() from react/promise).composer.json:
{
"require-dev": {
"php": "^8.4",
"phpunit/phpunit": "^12.3"
}
}
Bus::dispatchSync()) — Laravel’s sync architecture conflicts with ReactPHP’s event loop. Best for isolated async modules, not full-stack Laravel tests.await() on promises// @codeCoverageIgnore on await() lines if needed.How can I help you explore Laravel packages today?