Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpunit Asynchronicity Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Asynchronicity

Using this library you can make a test wait for certain conditions, e.g. to test the output of another process.

See my blog post on the subject for an explanation of the concepts and some code samples. Please note that this article covers version 1 of the library.

Usage

With PHPUnit

use Asynchronicity\PHPUnit\Asynchronicity;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

final class ProcessTest extends TestCase
{
    use Asynchronicity;

    /**
     * @test
     */
    public function it_creates_a_pid_file(): void
    {
        // start the asynchronous process that will eventually create a PID file...

        self::assertEventually(
            function (): void {
                Assert::assertFileExists(__DIR__ . '/pid');
            }
        );
    }
}

With Behat

Within a Behat FeatureContext you could use it for example that a page eventually contains some text:

use Asynchronicity\PHPUnit\Asynchronicity;
use Behat\MinkExtension\Context\MinkContext;
use PHPUnit\Framework\Assert;

final class FeatureContext extends MinkContext
{
    use Asynchronicity;

    /**
     * @Then the stock level has been updated to :expectedStockLevel
     */
    public function thenTheFileHasBeenCreated(string $expectedStockLevel): void
    {
        self::assertEventually(function () use ($expectedStockLevel): void {
            $this->visit('/stock-levels');

            $actualStockLevel = $this->getSession()->getPage())->find('css', '.stock-level')->getText();

            Assert::assertEquals($expectedStockLevel, $actualStockLevel);
        });
    }
}

Comments and suggestions

  • You can use $this inside these callables.
  • You can add use ($...) to pass in extra data.
  • You can throw any type of exception inside the callable to indicate that what you're looking for is not yet the case.
  • Often it's convenient to just use the usual assertion methods (PHPUnit or otherwise) inside the callable. They will often provide the right amount of detail in their error messages too.
  • assertEventually() supports extra arguments for setting the timeout and wait time in milliseconds.
  • You can use any callable as the first argument to assertEventually(), including objects with an __invoke() method or something like [$object, 'methodName'].
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky