amphp/phpunit-util
Small utility for simplifying async testing in PHPUnit with AMPHP’s fiber-based concurrency. Integrates seamlessly with AsyncTestCase to run tests as coroutines, ensuring smooth execution of I/O-bound operations like sockets, streams, and HTTP requests. Works with PHP 8.1+. Ideal for testing concurr...
Install the package as a dev dependency: composer require --dev amphp/phpunit-util. Requires PHP 8.1+ and PHPUnit 9+. Then extend Amp\PHPUnit\AsyncTestCase instead of the standard PHPUnit\Framework\TestCase. Your test methods can now be written as coroutines using Amp\call(), returning generators, or async functions—suspensions (e.g., socket reads, delays) are automatically handled. Start by writing a simple async test that connects to a service or uses fibers, like the example in the README: instantiate a socket, write data, and assert the response—all while using suspension-aware code.
AsyncTestCase as the base class for all async tests—its setUp() and tearDown() are fiber-aware and can suspend. Avoid the deprecated setUpAsync()/tearDownAsync() from v2.Amp\call() in test methods: Amp\call(fn() => $this->doAsyncThing()) works seamlessly, and unhandled coroutines fail the test.createCallback() for mocking async interactions: e.g., $mock = $this->createCallback(MyService::class, 'run'); $mock->expects($this->once())->with($this->identicalTo($arg)); returns a closure that integrates with PHPUnit’s mocking and argument assertions.AsyncTestCase handles timeouts natively—failures now report generator stack traces when timeouts occur (v1.2.0+).amphp/socket, amphp/http-client, or amphp/artax—tests run in isolated fibers with automatic loop/fiber cleanup.Loop::run() manually—the test runner wraps tests in fibers and manages the event loop. Manual loop invocation leads to exceptions or hangs.global or static properties may not behave as expected with suspended coroutines.tearDown(): Since tearDown() now runs after the async test completes, ensure it’s also synchronous or carefully await async cleanup only if necessary—unawaited suspensions in tearDown() may cause test hangs or false passes.await instead of all(): Per v3.0.0 changes, internal usage moved from deprecated Amp\Future\all() to Amp\Future\await. Ensure your own test code mirrors this for consistency.createCallback(): The new with() integration is powerful but subtle—e.g., createCallback(Service::class, 'process', [1, 'x']) ensures the closure is only invoked if arguments match and in that order.TracingDriver::dump() integration (v1.2.0+) by enabling PHPUnit’s verbose output or inspecting test output—timeout failure messages now include the generator line where it blocked.How can I help you explore Laravel packages today?