PHPackages                             seregazhuk/react-promise-testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. seregazhuk/react-promise-testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

seregazhuk/react-promise-testing
================================

PHPUnit-based library for testing ReactPHP promises

v0.6.1(5y ago)335.4k↑79.1%76MITPHPPHP &gt;=8.0

Since Nov 10Pushed 6mo ago4 watchersCompare

[ Source](https://github.com/seregazhuk/php-react-promise-testing)[ Packagist](https://packagist.org/packages/seregazhuk/react-promise-testing)[ Docs](https://github.com/seregazhuk/php-react-promise-testing)[ RSS](/packages/seregazhuk-react-promise-testing/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)DependenciesVersions (16)Used By (6)

ReactPHP Promises Testing
=========================

[](#reactphp-promises-testing)

A library that provides a set of convenient assertions for testing ReactPHP promises. Under the hood uses [clue/php-block-react](https://github.com/clue/php-block-react) to block promises.

[![Build Status](https://camo.githubusercontent.com/2dcf978f530d88ffebb489d52a6850c4da0efc3a46de2bef93d0e55bb2df7ccb/68747470733a2f2f7472617669732d63692e6f72672f7365726567617a68756b2f7068702d72656163742d70726f6d6973652d74657374696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/seregazhuk/php-react-promise-testing)[![Maintainability](https://camo.githubusercontent.com/b9c8126fd1f0a6b89314683a938656f809602d2b31585dd3f7ae5ec2666f2bc8/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36383932333063646165303964326533323630302f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/seregazhuk/php-react-promise-testing/maintainability)[![Test Coverage](https://camo.githubusercontent.com/03e82cc7a78ddeaed86ceb19e1bb9c98b06a90736545cf79829f99e1b17ed0ea/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36383932333063646165303964326533323630302f746573745f636f766572616765)](https://codeclimate.com/github/seregazhuk/php-react-promise-testing/test_coverage)[![Total Downloads](https://camo.githubusercontent.com/1474c9eb843f62bed62fb49012d5e7b023c796cb00a421da55e98d90901d817a/68747470733a2f2f706f7365722e707567782e6f72672f7365726567617a68756b2f72656163742d70726f6d6973652d74657374696e672f646f776e6c6f616473)](//packagist.org/packages/seregazhuk/react-promise-testing)

When testing asynchronous code and promises things can be a bit tricky. This library provides a set of convenient assertions for testing ReactPHP promises.

**Table of Contents**

- [Installation](#installation)
- [Quick Start](#quick-start)
- [Assertions](#assertions)

    - [assertPromiseFulfills()](#assertpromisefulfills)
    - [assertPromiseFulfillsWith()](#assertpromisefulfillswith)
    - [assertPromiseFulfillsWithInstanceOf()](#assertpromisefulfillswithinstanceof)
    - [assertPromiseRejects()](#assertpromiserejects())
    - [assertPromiseRejectsWith()](#assertpromiserejectswith)
    - [assertTrueAboutPromise()](#asserttrueaboutpromise)
    - [assertFalseAboutPromise()](#assertfalseaboutpromise)
- [Helpers](#helpers)

    - [waitForPromiseToFulfill()](#waitforpromisetofulfill)
    - [waitForPromise()](#waitforpromise)

Installation
------------

[](#installation)

### Dependencies

[](#dependencies)

Library requires PHP 8.0 or above.

The recommended way to install this library is via [Composer](https://getcomposer.org). [New to Composer?](https://getcomposer.org/doc/00-intro.md)

See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

```
composer require seregazhuk/react-promise-testing --dev

```

Quick Start
-----------

[](#quick-start)

Use the trait `seregazhuk\React\PromiseTesting\AssertsPromise` or extend your test classes from `seregazhuk\React\PromiseTesting\TestCase` class, which itself extends PHPUnit `TestCase`.

```
final class MyTest extends TestCase
{
    /** @test */
    public function promise_fulfills_with_a_response_object()
    {
        $browser = new Clue\React\Buzz\Browser($this->eventLoop());
        $promise = $browser->get('http://www.google.com/');
        $this->assertPromiseFulfillsWithInstanceOf($promise, ResponseInterface::class);
    }
}
```

Using the trait:

```
use PHPUnit\Framework\TestCase;
use seregazhuk\React\PromiseTesting\AssertsPromise;

final class MyTest extends TestCase
{
    use AssertsPromise;

    /** @test */
    public function promise_fulfills_with_a_response_object()
    {
        $browser = new Clue\React\Buzz\Browser($this->eventLoop());
        $promise = $browser->get('http://www.google.com/');
        $this->assertPromiseFulfillsWithInstanceOf($promise, ResponseInterface::class);
    }
}
```

Test above checks that a specified promise fulfills with an instance of `ResponseInterface`.

Event loop
----------

[](#event-loop)

To make promise assertions we need to run the loop. Before each test a new instance of the event loop is being created (inside `setUp()` method). If you need the loop to build your dependencies you **should**use `eventLoop()` method to retrieve it.

Assertions
----------

[](#assertions)

### assertPromiseFulfills()

[](#assertpromisefulfills)

`public function assertPromiseFulfills(PromiseInterface $promise, int $timeout = null): void`

The test fails if the `$promise` rejects.

You can specify `$timeout` in seconds to wait for promise to be resolved. If the promise was not fulfilled in specified timeout the test fails. When not specified, timeout is set to 2 seconds.

```
final class PromiseFulfillsTest extends TestCase
{
    /** @test */
    public function promise_fulfills(): void
    {
        $deferred = new Deferred();
        $deferred->reject();
        $this->assertPromiseFulfills($deferred->promise(), 1);
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 189 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillTest::promise_fulfills
Failed asserting that promise fulfills. Promise was rejected.
```

### assertPromiseFulfillsWith()

[](#assertpromisefulfillswith)

`assertPromiseFulfillsWith(PromiseInterface $promise, $value, int $timeout = null): void`

The test fails if the `$promise` doesn't fulfills with a specified `$value`.

You can specify `$timeout` in seconds to wait for promise to be fulfilled. If the promise was not fulfilled in specified timeout the test fails. When not specified, timeout is set to 2 seconds.

```
final class PromiseFulfillsWithTest extends TestCase
{
    /** @test */
    public function promise_fulfills_with_a_specified_value(): void
    {
        $deferred = new Deferred();
        $deferred->resolve(1234);
        $this->assertPromiseFulfillsWith($deferred->promise(), 1);
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 180 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillsWithTest::promise_fulfills_with_a_specified_value
Failed asserting that promise fulfills with a specified value.
Failed asserting that 1234 matches expected 1.
```

### assertPromiseFulfillsWithInstanceOf()

[](#assertpromisefulfillswithinstanceof)

`assertPromiseFulfillsWithInstanceOf(PromiseInterface $promise, string $class, int $timeout = null): void`

The test fails if the `$promise` doesn't fulfills with an instance of specified `$class`.

You can specify `$timeout` in seconds to wait for promise to be fulfilled. If the promise was not fulfilled in specified timeout the test fails. When not specified, timeout is set to 2 seconds.

```
final class PromiseFulfillsWithInstanceOfTest extends TestCase
{
    /** @test */
    public function promise_fulfills_with_an_instance_of_class(): void
    {
        $deferred = new Deferred();
        $deferred->resolve(new MyClass);
        $this->assertPromiseFulfillsWithInstanceOf($deferred->promise(), MyClass::class);
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 180 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillsWithWithInstanceOfTest::promise_fulfills_with_an_instance_of_class
Failed asserting that promise fulfills with a value of class MyClass.
```

### assertPromiseRejects()

[](#assertpromiserejects)

`assertPromiseRejects(PromiseInterface $promise, int $timeout = null): void`

The test fails if the `$promise` fulfills.

You can specify `$timeout` in seconds to wait for promise to be resolved. If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`. When not specified, timeout is set to 2 seconds.

```
final class PromiseRejectsTest extends TestCase
{
    /** @test */
    public function promise_rejects(): void
    {
        $deferred = new Deferred();
        $deferred->resolve();
        $this->assertPromiseRejects($deferred->promise());
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 175 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\PromiseRejectsTest::promise_rejects
Failed asserting that promise rejects. Promise was fulfilled.
```

### assertPromiseRejectsWith()

[](#assertpromiserejectswith)

`assertPromiseRejectsWith(PromiseInterface $promise, string $reasonExceptionClass, int $timeout = null): void`

The test fails if the `$promise` doesn't reject with a specified exception class.

You can specify `$timeout` in seconds to wait for promise to be resolved. If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`. When not specified, timeout is set to 2 seconds.

```
final class PromiseRejectsWithTest extends TestCase
{
    /** @test */
    public function promise_rejects_with_a_specified_reason(): void
    {
        $deferred = new Deferred();
        $deferred->reject(new \LogicException());
        $this->assertPromiseRejectsWith($deferred->promise(), \InvalidArgumentException::class);
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 136 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\PromiseRejectsWithTest::promise_rejects_with_a_specified_reason
Failed asserting that promise rejects with a specified reason.
Failed asserting that LogicException Object (...) is an instance of class "InvalidArgumentException".
```

### assertTrueAboutPromise()

[](#asserttrueaboutpromise)

`assertTrueAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void`

The test fails if the value encapsulated in the Promise does not conform to an arbitrary predicate.

You can specify `$timeout` in seconds to wait for promise to be resolved. If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`. When not specified, timeout is set to 2 seconds.

```
final class AssertTrueAboutPromiseTest extends TestCase
{
    /** @test */
    public function promise_encapsulates_integer(): void
    {
        $deferred = new Deferred();
        $deferred->resolve(23);

        $this->assertTrueAboutPromise($deferred->promise(), function ($val) {
            return is_object($val);
        });
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 136 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\AssertTrueAboutPromiseTest::promise_encapsulates_integer
Failed asserting that false is true.
```

### assertFalseAboutPromise()

[](#assertfalseaboutpromise)

`assertFalseAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void`

The test fails if the value encapsulated in the Promise conforms to an arbitrary predicate.

You can specify `$timeout` in seconds to wait for promise to be resolved. If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`. When not specified, timeout is set to 2 seconds.

```
final class AssertFalseAboutPromiseTest extends TestCase
{
    /** @test */
    public function promise_encapsulates_object(): void
    {
        $deferred = new Deferred();
        $deferred->resolve(23);

        $this->assertFalseAboutPromise($deferred->promise(), function ($val) {
            return is_int($val);
        });
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 136 ms, Memory: 4.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\AssertFalseAboutPromiseTest::promise_encapsulates_object
Failed asserting that true is false.
```

Helpers
-------

[](#helpers)

### waitForPromiseToFulfill()

[](#waitforpromisetofulfill)

`function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null)`.

This helper can be used when you want to resolve a promise and get the resolution value.

Tries to resolve a `$promise` in a specified `$timeout` seconds and returns resolved value. If `$timeout` is not set uses 2 seconds by default. The test fails if the `$promise` doesn't fulfill.

```
final class WaitForPromiseToFulfillTest extends TestCase
{
    /** @test */
    public function promise_fulfills(): void
    {
        $deferred = new Deferred();

        $deferred->reject(new \Exception());
        $value = $this->waitForPromiseToFulfill($deferred->promise());
    }
}
```

```
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 223 ms, Memory: 6.00MB

There was 1 failure:

1) seregazhuk\React\PromiseTesting\tests\WaitForPromiseToFulfillTest::promise_fulfills
Failed to fulfill a promise. It was rejected with Exception.
```

### waitForPromise()

[](#waitforpromise)

`function waitForPromise(PromiseInterface $promise, int $timeout = null)`.

Tries to resolve a specified `$promise` in a specified `$timeout` seconds. If `$timeout` is not set uses 2 seconds by default. If the promise fulfills returns a resolution value, otherwise throws an exception. If the promise rejects throws the rejection reason, if the promise doesn't fulfill in a specified `$timeout` throws `React\Promise\Timer\TimeoutException`.

This helper can be useful when you need to get the value from the fulfilled promise in a synchronous way:

```
$value = $this->waitForPromise($cache->get('key'));
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance48

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 89.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~96 days

Recently: every ~102 days

Total

14

Last Release

1842d ago

PHP version history (3 changes)v0.1.0PHP &gt;=5.6

v0.2.0PHP &gt;=7.2

v0.6.1PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb0e185b75648fb1c0095795e222f210700b86ae8c575c3cd6dd28a970ae2f34?d=identicon)[seregazhuk](/maintainers/seregazhuk)

---

Top Contributors

[![seregazhuk](https://avatars.githubusercontent.com/u/9959761?v=4)](https://github.com/seregazhuk "seregazhuk (97 commits)")[![ace411](https://avatars.githubusercontent.com/u/11040337?v=4)](https://github.com/ace411 "ace411 (5 commits)")[![ttrig](https://avatars.githubusercontent.com/u/2156132?v=4)](https://github.com/ttrig "ttrig (3 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (2 commits)")[![awohsen](https://avatars.githubusercontent.com/u/93778317?v=4)](https://github.com/awohsen "awohsen (1 commits)")

---

Tags

asyncphpphpunitpromisesreactphptestingtestpromisereactphp

### Embed Badge

![Health badge](/badges/seregazhuk-react-promise-testing/health.svg)

```
[![Health](https://phpackages.com/badges/seregazhuk-react-promise-testing/health.svg)](https://phpackages.com/packages/seregazhuk-react-promise-testing)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.5k](/packages/mockery-mockery)[php-mock/php-mock

PHP-Mock can mock built-in PHP functions (e.g. time()). PHP-Mock relies on PHP's namespace fallback policy. No further extension is needed.

36918.1M98](/packages/php-mock-php-mock)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33412.5M345](/packages/brain-monkey)[ta-tikoma/phpunit-architecture-test

Methods for testing application architecture

10745.9M13](/packages/ta-tikoma-phpunit-architecture-test)[php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

1718.2M396](/packages/php-mock-php-mock-phpunit)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

138850.9k5](/packages/fr3d-swagger-assertions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
