matthiasnoback/phpunit-asynchronicity
PHPUnit/Behat helper for testing asynchronous behavior. Provides assertEventually() to retry a callable until assertions pass or a timeout occurs—useful for waiting on files, processes, or UI updates, with configurable timeout and polling interval.
Illuminate\Queue).Illuminate\Events).Bus, DispatchesJobs, HandleQueue) but lacks native integration, requiring explicit adoption.assertEquals) to async-aware assertions (e.g., assertWillReceive), aligning with modern event-driven architectures.refreshDatabase(), actingAs()) but requires manual setup for async contexts (e.g., mocking queues).Queue::fake() + custom listeners).assertTrue() with assertWillReceive()).Pest/PHPUnit parallelization?Queue::fake() vs. custom mocks)?pestphp/pest (if using Pest).laravel/framework testing helpers (e.g., create(), assertDatabaseHas()).Queue::fake() and Queue::assertPushed().Event::fake() for async listeners.spatie/laravel-test-factory or orchestra/testbench for broader async testing.assertTrue(Job::dispatch()->wait()->success)) with async equivalents.matthiasnoback/phpunit-asynchronicity to composer.json:
composer require --dev matthiasnoback/phpunit-asynchronicity
phpunit.xml to load the extension:
<extensions>
<extension class="MatthiasNoback\PHPUnit\Async\AsyncExtension"/>
</extensions>
assertWillReceive() for queues/events.$this->assertWillReceive('App\Jobs\ProcessOrder::dispatch')
->withArgs([$orderId])
->once();
Queue::fake() for deterministic async testing:
Queue::fake();
$this->assertWillReceive(...);
assertWillReceive).DB::beginTransaction().// Assert job is dispatched within 5s).Queue::afterCommit() hooks).assertWillReceive()->fail('Job timed out')).tightenco/ziggy or spatie/laravel-activitylog to trace async execution.phpunit --group async to isolate async-heavy tests.Queue::assertPushed()).- name: Run async tests
run: php artisan test --group async --timeout=30
| Failure Type | Root Cause | Mitigation |
|---|---|---|
| Timeout Errors | Async job takes > default timeout. | Increase timeout or mock slower services. |
| Race Conditions | Parallel tests interfere with async state. | Use Queue::fake() or sequential test groups. |
| Flaky Assertions | Non-deterministic async behavior. | Add retries or use assertWillReceive()->wait() with jitter. |
| Mocking Gaps | Async dependencies not fully mocked. | Extend Queue::fake() or use Mockery. |
| Database Inconsistency | Async jobs modify DB outside transaction. | Test with DB::transaction() or rollbacks. |
assertWillReceive intuitive?").How can I help you explore Laravel packages today?