- How do I install this package for Laravel async testing?
- Run `composer require wyrihaximus/async-test-utilities` to install. Ensure your Laravel project uses PHP 8.1+ and has the fiber extension enabled (e.g., via `php.ini` or Docker). No additional Laravel-specific setup is needed beyond extending `AsyncTestCase` in your test classes.
- Can I use this package with Laravel queues (e.g., Horizon or Laravel Queues)?
- Yes, this package is perfect for testing async queue workers. Each test runs in an isolated fiber, so you can dispatch jobs and assert results without interference from other tests. Use the `TimeOut` attribute to prevent hanging tests if a job fails.
- What Laravel versions support this package?
- This package works with Laravel 9+ (PHP 8.1+ required). Laravel 8 or older may need upgrades to support fibers. Ensure your `phpunit.xml` or CI environment enables the fiber extension, as Laravel’s default test runner is synchronous.
- How do I set custom timeouts for async tests?
- Use the `#[TimeOut]` attribute on test classes or methods. Method-level timeouts override class-level defaults. For example, `#[TimeOut(0.5)]` sets a 500ms timeout. Default timeout is 30 seconds if unspecified.
- Will this work with Laravel’s event listeners or observers?
- Absolutely. Test event listeners by dispatching events in fibers and using helpers like `expectCallableExactly()` to verify async callbacks. Isolate tests with fibers to avoid race conditions between listeners or observers in parallel test suites.
- Does this package conflict with other async libraries like Amp or Swoole?
- Yes, this package depends on ReactPHP for fibers. Avoid mixing it with Amp or Swoole in the same project unless isolated to test scope. ReactPHP’s event loop is lightweight and designed for testing, but conflicts may arise if both libraries try to manage the event loop.
- How do I debug flaky async tests in Laravel?
- Flakiness often stems from race conditions or shared state. Use aggressive timeouts (`#[TimeOut(0.1)]`) to fail fast, and implement retry logic for intermittent tests. Tools like Xdebug may need fiber-aware configurations (e.g., `xdebug.mode=fiber`).
- Can I use this for HTTP client testing (e.g., Guzzle async requests)?
- Yes, fibers allow you to test async HTTP clients like Guzzle without blocking. Dispatch requests in fibers and use `await()` to synchronize assertions. The `TimeOut` attribute ensures tests fail quickly if requests hang.
- How does this integrate with Laravel’s service container for mocking?
- The package works seamlessly with Laravel’s container. Mock async services (e.g., `Bus`, `Queue`, or `Events`) by binding them in fibers. Use Laravel’s `Mockery` or `PHPUnit` mocks as usual, but wrap async calls in fibers for testing.
- What if my CI environment doesn’t support PHP fibers?
- Enable fibers in your CI by adding `php -dextension=fiber` to your build commands (e.g., GitHub Actions or CircleCI). For Docker, ensure the `php:8.1-fpm` image includes the fiber extension. Check the package’s CI workflow for examples.