Library for asserting things that happen asynchronously with PHPUnit
MIT License
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.
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');
}
);
}
}
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);
});
}
}
$this
inside these callables.use ($...)
to pass in extra data.assertEventually()
supports extra arguments for setting the timeout and wait time in milliseconds.assertEventually()
, including objects with an __invoke()
method or something like [$object, 'methodName']
.