1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 19:42:38 +02:00

Added vendor directory to source control

This commit is contained in:
Daniel 2021-10-05 13:39:35 -03:00
parent b65ed2bc68
commit 1a661614b7
20999 changed files with 2890743 additions and 2883485 deletions

@ -1 +0,0 @@
Subproject commit f3cff96a19736714524ca0dd1d4130de73dbbbc4

31
vendor/react/promise/.travis.yml vendored Normal file
View file

@ -0,0 +1,31 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- nightly # ignore errors, see below
- hhvm # ignore errors, see below
# lock distro so new future defaults will not break the build
dist: trusty
matrix:
allow_failures:
- php: hhvm
- php: nightly
install:
- composer install
script:
- ./vendor/bin/phpunit -v --coverage-text --coverage-clover=./build/logs/clover.xml
after_script:
- if [ -f ./build/logs/clover.xml ]; then travis_retry composer require satooshi/php-coveralls --no-interaction --update-with-dependencies; fi
- if [ -f ./build/logs/clover.xml ]; then php vendor/bin/coveralls -v; fi

162
vendor/react/promise/CHANGELOG.md vendored Normal file
View file

@ -0,0 +1,162 @@
CHANGELOG for 2.x
=================
* 2.8.0 (2020-05-12)
* Mark `FulfilledPromise`, `RejectedPromise` and `LazyPromise` as deprecated for Promise v2 (and remove for Promise v3).
(#143 and #165 by @clue)
```php
// deprecated
$fulfilled = new React\Promise\FulfilledPromise($value);
$rejected = new React\Promise\RejectedPromise($reason);
// recommended alternatives
$fulfilled = React\Promise\resolve($value);
$rejected = React\Promise\reject($reason);
```
* Fix: Fix checking whether cancellable promise is an object and avoid possible warning.
(#168 by @smscr and @jsor)
* Improve documentation and add docblocks to functions and interfaces.
(#135 by @CharlotteDunois)
* Add `.gitattributes` to exclude dev files from exports.
(#154 by @reedy)
* Improve test suite, run tests on PHP 7.4 and update PHPUnit test setup.
(#163 by @clue)
* 2.7.1 (2018-01-07)
* Fix: file_exists warning when resolving with long strings.
(#130 by @sbesselsen)
* Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function.
(#133 by @WyriHaximus)
* 2.7.0 (2018-06-13)
* Feature: Improve memory consumption for pending promises by using static internal callbacks without binding to self.
(#124 by @clue)
* 2.6.0 (2018-06-11)
* Feature: Significantly improve memory consumption and performance by only passing resolver args
to resolver and canceller if callback requires them. Also use static callbacks without
binding to promise, clean up canceller function reference when they are no longer
needed and hide resolver and canceller references from call stack on PHP 7+.
(#113, #115, #116, #117, #118, #119 and #123 by @clue)
These changes combined mean that rejecting promises with an `Exception` should
no longer cause any internal circular references which could cause some unexpected
memory growth in previous versions. By explicitly avoiding and explicitly
cleaning up said references, we can avoid relying on PHP's circular garbage collector
to kick in which significantly improves performance when rejecting many promises.
* Mark legacy progress support / notification API as deprecated
(#112 by @clue)
* Recommend rejecting promises by throwing an exception
(#114 by @jsor)
* Improve documentation to properly instantiate LazyPromise
(#121 by @holtkamp)
* Follower cancellation propagation was originally planned for this release
but has been reverted for now and is planned for a future release.
(#99 by @jsor and #122 by @clue)
* 2.5.1 (2017-03-25)
* Fix circular references when resolving with a promise which follows
itself (#94).
* 2.5.0 (2016-12-22)
* Revert automatic cancellation of pending collection promises once the
output promise resolves. This was introduced in 42d86b7 (PR #36, released
in [v2.3.0](https://github.com/reactphp/promise/releases/tag/v2.3.0)) and
was both unintended and backward incompatible.
If you need automatic cancellation, you can use something like:
```php
function allAndCancel(array $promises)
{
return \React\Promise\all($promises)
->always(function() use ($promises) {
foreach ($promises as $promise) {
if ($promise instanceof \React\Promise\CancellablePromiseInterface) {
$promise->cancel();
}
}
});
}
```
* `all()` and `map()` functions now preserve the order of the array (#77).
* Fix circular references when resolving a promise with itself (#71).
* 2.4.1 (2016-05-03)
* Fix `some()` not cancelling pending promises when too much input promises
reject (16ff799).
* 2.4.0 (2016-03-31)
* Support foreign thenables in `resolve()`.
Any object that provides a `then()` method is now assimilated to a trusted
promise that follows the state of this thenable (#52).
* Fix `some()` and `any()` for input arrays containing not enough items
(#34).
* 2.3.0 (2016-03-24)
* Allow cancellation of promises returned by functions working on promise
collections (#36).
* Handle `\Throwable` in the same way as `\Exception` (#51 by @joshdifabio).
* 2.2.2 (2016-02-26)
* Fix cancellation handlers called multiple times (#47 by @clue).
* 2.2.1 (2015-07-03)
* Fix stack error when resolving a promise in its own fulfillment or
rejection handlers.
* 2.2.0 (2014-12-30)
* Introduce new `ExtendedPromiseInterface` implemented by all promises.
* Add new `done()` method (part of the `ExtendedPromiseInterface`).
* Add new `otherwise()` method (part of the `ExtendedPromiseInterface`).
* Add new `always()` method (part of the `ExtendedPromiseInterface`).
* Add new `progress()` method (part of the `ExtendedPromiseInterface`).
* Rename `Deferred::progress` to `Deferred::notify` to avoid confusion with
`ExtendedPromiseInterface::progress` (a `Deferred::progress` alias is
still available for backward compatibility)
* `resolve()` now always returns a `ExtendedPromiseInterface`.
* 2.1.0 (2014-10-15)
* Introduce new `CancellablePromiseInterface` implemented by all promises.
* Add new `cancel()` method (part of the `CancellablePromiseInterface`).
* 2.0.0 (2013-12-10)
New major release. The goal is to streamline the API and to make it more
compliant with other promise libraries and especially with the new upcoming
[ES6 promises specification](https://github.com/domenic/promises-unwrapping/).
* Add standalone Promise class.
* Add new `race()` function.
* BC break: Bump minimum PHP version to PHP 5.4.
* BC break: Remove `ResolverInterface` and `PromiseInterface` from
`Deferred`.
* BC break: Change signature of `PromiseInterface`.
* BC break: Remove `When` and `Util` classes and move static methods to
functions.
* BC break: `FulfilledPromise` and `RejectedPromise` now throw an exception
when initialized with a promise instead of a value/reason.
* BC break: `Deferred::resolve()` and `Deferred::reject()` no longer return
a promise.

22
vendor/react/promise/LICENSE vendored Normal file
View file

@ -0,0 +1,22 @@
Copyright (c) 2012-2016 Jan Sorgalla
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

876
vendor/react/promise/README.md vendored Normal file
View file

@ -0,0 +1,876 @@
Promise
=======
A lightweight implementation of
[CommonJS Promises/A](http://wiki.commonjs.org/wiki/Promises/A) for PHP.
[![Build Status](https://travis-ci.org/reactphp/promise.svg?branch=master)](http://travis-ci.org/reactphp/promise)
[![Coverage Status](https://coveralls.io/repos/github/reactphp/promise/badge.svg?branch=master)](https://coveralls.io/github/reactphp/promise?branch=master)
Table of Contents
-----------------
1. [Introduction](#introduction)
2. [Concepts](#concepts)
* [Deferred](#deferred)
* [Promise](#promise-1)
3. [API](#api)
* [Deferred](#deferred-1)
* [Deferred::promise()](#deferredpromise)
* [Deferred::resolve()](#deferredresolve)
* [Deferred::reject()](#deferredreject)
* [Deferred::notify()](#deferrednotify)
* [PromiseInterface](#promiseinterface)
* [PromiseInterface::then()](#promiseinterfacethen)
* [ExtendedPromiseInterface](#extendedpromiseinterface)
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
* [ExtendedPromiseInterface::otherwise()](#extendedpromiseinterfaceotherwise)
* [ExtendedPromiseInterface::always()](#extendedpromiseinterfacealways)
* [ExtendedPromiseInterface::progress()](#extendedpromiseinterfaceprogress)
* [CancellablePromiseInterface](#cancellablepromiseinterface)
* [CancellablePromiseInterface::cancel()](#cancellablepromiseinterfacecancel)
* [Promise](#promise-2)
* [FulfilledPromise](#fulfilledpromise)
* [RejectedPromise](#rejectedpromise)
* [LazyPromise](#lazypromise)
* [Functions](#functions)
* [resolve()](#resolve)
* [reject()](#reject)
* [all()](#all)
* [race()](#race)
* [any()](#any)
* [some()](#some)
* [map()](#map)
* [reduce()](#reduce)
* [PromisorInterface](#promisorinterface)
4. [Examples](#examples)
* [How to use Deferred](#how-to-use-deferred)
* [How promise forwarding works](#how-promise-forwarding-works)
* [Resolution forwarding](#resolution-forwarding)
* [Rejection forwarding](#rejection-forwarding)
* [Mixed resolution and rejection forwarding](#mixed-resolution-and-rejection-forwarding)
* [Progress event forwarding](#progress-event-forwarding)
* [done() vs. then()](#done-vs-then)
5. [Install](#install)
6. [Credits](#credits)
7. [License](#license)
Introduction
------------
Promise is a library implementing
[CommonJS Promises/A](http://wiki.commonjs.org/wiki/Promises/A) for PHP.
It also provides several other useful promise-related concepts, such as joining
multiple promises and mapping and reducing collections of promises.
If you've never heard about promises before,
[read this first](https://gist.github.com/3889970).
Concepts
--------
### Deferred
A **Deferred** represents a computation or unit of work that may not have
completed yet. Typically (but not always), that computation will be something
that executes asynchronously and completes at some point in the future.
### Promise
While a deferred represents the computation itself, a **Promise** represents
the result of that computation. Thus, each deferred has a promise that acts as
a placeholder for its actual result.
API
---
### Deferred
A deferred represents an operation whose resolution is pending. It has separate
promise and resolver parts.
```php
$deferred = new React\Promise\Deferred();
$promise = $deferred->promise();
$deferred->resolve(mixed $value = null);
$deferred->reject(mixed $reason = null);
$deferred->notify(mixed $update = null);
```
The `promise` method returns the promise of the deferred.
The `resolve` and `reject` methods control the state of the deferred.
The deprecated `notify` method is for progress notification.
The constructor of the `Deferred` accepts an optional `$canceller` argument.
See [Promise](#promise-2) for more information.
#### Deferred::promise()
```php
$promise = $deferred->promise();
```
Returns the promise of the deferred, which you can hand out to others while
keeping the authority to modify its state to yourself.
#### Deferred::resolve()
```php
$deferred->resolve(mixed $value = null);
```
Resolves the promise returned by `promise()`. All consumers are notified by
having `$onFulfilled` (which they registered via `$promise->then()`) called with
`$value`.
If `$value` itself is a promise, the promise will transition to the state of
this promise once it is resolved.
#### Deferred::reject()
```php
$deferred->reject(mixed $reason = null);
```
Rejects the promise returned by `promise()`, signalling that the deferred's
computation failed.
All consumers are notified by having `$onRejected` (which they registered via
`$promise->then()`) called with `$reason`.
If `$reason` itself is a promise, the promise will be rejected with the outcome
of this promise regardless whether it fulfills or rejects.
#### Deferred::notify()
> Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.
```php
$deferred->notify(mixed $update = null);
```
Triggers progress notifications, to indicate to consumers that the computation
is making progress toward its result.
All consumers are notified by having `$onProgress` (which they registered via
`$promise->then()`) called with `$update`.
### PromiseInterface
The promise interface provides the common interface for all promise
implementations.
A promise represents an eventual outcome, which is either fulfillment (success)
and an associated value, or rejection (failure) and an associated reason.
Once in the fulfilled or rejected state, a promise becomes immutable.
Neither its state nor its result (or error) can be modified.
#### Implementations
* [Promise](#promise-2)
* [FulfilledPromise](#fulfilledpromise) (deprecated)
* [RejectedPromise](#rejectedpromise) (deprecated)
* [LazyPromise](#lazypromise) (deprecated)
#### PromiseInterface::then()
```php
$transformedPromise = $promise->then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
```
Transforms a promise's value by applying a function to the promise's fulfillment
or rejection value. Returns a new promise for the transformed result.
The `then()` method registers new fulfilled, rejection and progress handlers
with a promise (all parameters are optional):
* `$onFulfilled` will be invoked once the promise is fulfilled and passed
the result as the first argument.
* `$onRejected` will be invoked once the promise is rejected and passed the
reason as the first argument.
* `$onProgress` (deprecated) will be invoked whenever the producer of the promise
triggers progress notifications and passed a single argument (whatever it
wants) to indicate progress.
It returns a new promise that will fulfill with the return value of either
`$onFulfilled` or `$onRejected`, whichever is called, or will reject with
the thrown exception if either throws.
A promise makes the following guarantees about handlers registered in
the same call to `then()`:
1. Only one of `$onFulfilled` or `$onRejected` will be called,
never both.
2. `$onFulfilled` and `$onRejected` will never be called more
than once.
3. `$onProgress` (deprecated) may be called multiple times.
#### See also
* [resolve()](#resolve) - Creating a resolved promise
* [reject()](#reject) - Creating a rejected promise
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
* [done() vs. then()](#done-vs-then)
### ExtendedPromiseInterface
The ExtendedPromiseInterface extends the PromiseInterface with useful shortcut
and utility methods which are not part of the Promises/A specification.
#### Implementations
* [Promise](#promise-1)
* [FulfilledPromise](#fulfilledpromise) (deprecated)
* [RejectedPromise](#rejectedpromise) (deprecated)
* [LazyPromise](#lazypromise) (deprecated)
#### ExtendedPromiseInterface::done()
```php
$promise->done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
```
Consumes the promise's ultimate value if the promise fulfills, or handles the
ultimate error.
It will cause a fatal error if either `$onFulfilled` or `$onRejected` throw or
return a rejected promise.
Since the purpose of `done()` is consumption rather than transformation,
`done()` always returns `null`.
#### See also
* [PromiseInterface::then()](#promiseinterfacethen)
* [done() vs. then()](#done-vs-then)
#### ExtendedPromiseInterface::otherwise()
```php
$promise->otherwise(callable $onRejected);
```
Registers a rejection handler for promise. It is a shortcut for:
```php
$promise->then(null, $onRejected);
```
Additionally, you can type hint the `$reason` argument of `$onRejected` to catch
only specific errors.
```php
$promise
->otherwise(function (\RuntimeException $reason) {
// Only catch \RuntimeException instances
// All other types of errors will propagate automatically
})
->otherwise(function ($reason) {
// Catch other errors
)};
```
#### ExtendedPromiseInterface::always()
```php
$newPromise = $promise->always(callable $onFulfilledOrRejected);
```
Allows you to execute "cleanup" type tasks in a promise chain.
It arranges for `$onFulfilledOrRejected` to be called, with no arguments,
when the promise is either fulfilled or rejected.
* If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully,
`$newPromise` will fulfill with the same value as `$promise`.
* If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a
rejected promise, `$newPromise` will reject with the thrown exception or
rejected promise's reason.
* If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully,
`$newPromise` will reject with the same reason as `$promise`.
* If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a
rejected promise, `$newPromise` will reject with the thrown exception or
rejected promise's reason.
`always()` behaves similarly to the synchronous finally statement. When combined
with `otherwise()`, `always()` allows you to write code that is similar to the familiar
synchronous catch/finally pair.
Consider the following synchronous code:
```php
try {
return doSomething();
} catch(\Exception $e) {
return handleError($e);
} finally {
cleanup();
}
```
Similar asynchronous code (with `doSomething()` that returns a promise) can be
written:
```php
return doSomething()
->otherwise('handleError')
->always('cleanup');
```
#### ExtendedPromiseInterface::progress()
> Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.
```php
$promise->progress(callable $onProgress);
```
Registers a handler for progress updates from promise. It is a shortcut for:
```php
$promise->then(null, null, $onProgress);
```
### CancellablePromiseInterface
A cancellable promise provides a mechanism for consumers to notify the creator
of the promise that they are not longer interested in the result of an
operation.
#### CancellablePromiseInterface::cancel()
``` php
$promise->cancel();
```
The `cancel()` method notifies the creator of the promise that there is no
further interest in the results of the operation.
Once a promise is settled (either fulfilled or rejected), calling `cancel()` on
a promise has no effect.
#### Implementations
* [Promise](#promise-1)
* [FulfilledPromise](#fulfilledpromise) (deprecated)
* [RejectedPromise](#rejectedpromise) (deprecated)
* [LazyPromise](#lazypromise) (deprecated)
### Promise
Creates a promise whose state is controlled by the functions passed to
`$resolver`.
```php
$resolver = function (callable $resolve, callable $reject, callable $notify) {
// Do some work, possibly asynchronously, and then
// resolve or reject. You can notify of progress events (deprecated)
// along the way if you want/need.
$resolve($awesomeResult);
// or throw new Exception('Promise rejected');
// or $resolve($anotherPromise);
// or $reject($nastyError);
// or $notify($progressNotification);
};
$canceller = function () {
// Cancel/abort any running operations like network connections, streams etc.
// Reject promise by throwing an exception
throw new Exception('Promise cancelled');
};
$promise = new React\Promise\Promise($resolver, $canceller);
```
The promise constructor receives a resolver function and an optional canceller
function which both will be called with 3 arguments:
* `$resolve($value)` - Primary function that seals the fate of the
returned promise. Accepts either a non-promise value, or another promise.
When called with a non-promise value, fulfills promise with that value.
When called with another promise, e.g. `$resolve($otherPromise)`, promise's
fate will be equivalent to that of `$otherPromise`.
* `$reject($reason)` - Function that rejects the promise. It is recommended to
just throw an exception instead of using `$reject()`.
* `$notify($update)` - Deprecated function that issues progress events for the promise.
If the resolver or canceller throw an exception, the promise will be rejected
with that thrown exception as the rejection reason.
The resolver function will be called immediately, the canceller function only
once all consumers called the `cancel()` method of the promise.
### FulfilledPromise
> Deprecated in v2.8.0: External usage of `FulfilledPromise` is deprecated, use `resolve()` instead.
Creates a already fulfilled promise.
```php
$promise = React\Promise\FulfilledPromise($value);
```
Note, that `$value` **cannot** be a promise. It's recommended to use
[resolve()](#resolve) for creating resolved promises.
### RejectedPromise
> Deprecated in v2.8.0: External usage of `RejectedPromise` is deprecated, use `reject()` instead.
Creates a already rejected promise.
```php
$promise = React\Promise\RejectedPromise($reason);
```
Note, that `$reason` **cannot** be a promise. It's recommended to use
[reject()](#reject) for creating rejected promises.
### LazyPromise
> Deprecated in v2.8.0: LazyPromise is deprecated and should not be used anymore.
Creates a promise which will be lazily initialized by `$factory` once a consumer
calls the `then()` method.
```php
$factory = function () {
$deferred = new React\Promise\Deferred();
// Do some heavy stuff here and resolve the deferred once completed
return $deferred->promise();
};
$promise = new React\Promise\LazyPromise($factory);
// $factory will only be executed once we call then()
$promise->then(function ($value) {
});
```
### Functions
Useful functions for creating, joining, mapping and reducing collections of
promises.
All functions working on promise collections (like `all()`, `race()`, `some()`
etc.) support cancellation. This means, if you call `cancel()` on the returned
promise, all promises in the collection are cancelled. If the collection itself
is a promise which resolves to an array, this promise is also cancelled.
#### resolve()
```php
$promise = React\Promise\resolve(mixed $promiseOrValue);
```
Creates a promise for the supplied `$promiseOrValue`.
If `$promiseOrValue` is a value, it will be the resolution value of the
returned promise.
If `$promiseOrValue` is a thenable (any object that provides a `then()` method),
a trusted promise that follows the state of the thenable is returned.
If `$promiseOrValue` is a promise, it will be returned as is.
Note: The promise returned is always a promise implementing
[ExtendedPromiseInterface](#extendedpromiseinterface). If you pass in a custom
promise which only implements [PromiseInterface](#promiseinterface), this
promise will be assimilated to a extended promise following `$promiseOrValue`.
#### reject()
```php
$promise = React\Promise\reject(mixed $promiseOrValue);
```
Creates a rejected promise for the supplied `$promiseOrValue`.
If `$promiseOrValue` is a value, it will be the rejection value of the
returned promise.
If `$promiseOrValue` is a promise, its completion value will be the rejected
value of the returned promise.
This can be useful in situations where you need to reject a promise without
throwing an exception. For example, it allows you to propagate a rejection with
the value of another promise.
#### all()
```php
$promise = React\Promise\all(array|React\Promise\PromiseInterface $promisesOrValues);
```
Returns a promise that will resolve only once all the items in
`$promisesOrValues` have resolved. The resolution value of the returned promise
will be an array containing the resolution values of each of the items in
`$promisesOrValues`.
#### race()
```php
$promise = React\Promise\race(array|React\Promise\PromiseInterface $promisesOrValues);
```
Initiates a competitive race that allows one winner. Returns a promise which is
resolved in the same way the first settled promise resolves.
#### any()
```php
$promise = React\Promise\any(array|React\Promise\PromiseInterface $promisesOrValues);
```
Returns a promise that will resolve when any one of the items in
`$promisesOrValues` resolves. The resolution value of the returned promise
will be the resolution value of the triggering item.
The returned promise will only reject if *all* items in `$promisesOrValues` are
rejected. The rejection value will be an array of all rejection reasons.
The returned promise will also reject with a `React\Promise\Exception\LengthException`
if `$promisesOrValues` contains 0 items.
#### some()
```php
$promise = React\Promise\some(array|React\Promise\PromiseInterface $promisesOrValues, integer $howMany);
```
Returns a promise that will resolve when `$howMany` of the supplied items in
`$promisesOrValues` resolve. The resolution value of the returned promise
will be an array of length `$howMany` containing the resolution values of the
triggering items.
The returned promise will reject if it becomes impossible for `$howMany` items
to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
reject). The rejection value will be an array of
`(count($promisesOrValues) - $howMany) + 1` rejection reasons.
The returned promise will also reject with a `React\Promise\Exception\LengthException`
if `$promisesOrValues` contains less items than `$howMany`.
#### map()
```php
$promise = React\Promise\map(array|React\Promise\PromiseInterface $promisesOrValues, callable $mapFunc);
```
Traditional map function, similar to `array_map()`, but allows input to contain
promises and/or values, and `$mapFunc` may return either a value or a promise.
The map function receives each item as argument, where item is a fully resolved
value of a promise or value in `$promisesOrValues`.
#### reduce()
```php
$promise = React\Promise\reduce(array|React\Promise\PromiseInterface $promisesOrValues, callable $reduceFunc , $initialValue = null);
```
Traditional reduce function, similar to `array_reduce()`, but input may contain
promises and/or values, and `$reduceFunc` may return either a value or a
promise, *and* `$initialValue` may be a promise or a value for the starting
value.
### PromisorInterface
The `React\Promise\PromisorInterface` provides a common interface for objects
that provide a promise. `React\Promise\Deferred` implements it, but since it
is part of the public API anyone can implement it.
Examples
--------
### How to use Deferred
```php
function getAwesomeResultPromise()
{
$deferred = new React\Promise\Deferred();
// Execute a Node.js-style function using the callback pattern
computeAwesomeResultAsynchronously(function ($error, $result) use ($deferred) {
if ($error) {
$deferred->reject($error);
} else {
$deferred->resolve($result);
}
});
// Return the promise
return $deferred->promise();
}
getAwesomeResultPromise()
->then(
function ($value) {
// Deferred resolved, do something with $value
},
function ($reason) {
// Deferred rejected, do something with $reason
},
function ($update) {
// Progress notification triggered, do something with $update
}
);
```
### How promise forwarding works
A few simple examples to show how the mechanics of Promises/A forwarding works.
These examples are contrived, of course, and in real usage, promise chains will
typically be spread across several function calls, or even several levels of
your application architecture.
#### Resolution forwarding
Resolved promises forward resolution values to the next promise.
The first promise, `$deferred->promise()`, will resolve with the value passed
to `$deferred->resolve()` below.
Each call to `then()` returns a new promise that will resolve with the return
value of the previous handler. This creates a promise "pipeline".
```php
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
// $x will be the value passed to $deferred->resolve() below
// and returns a *new promise* for $x + 1
return $x + 1;
})
->then(function ($x) {
// $x === 2
// This handler receives the return value of the
// previous handler.
return $x + 1;
})
->then(function ($x) {
// $x === 3
// This handler receives the return value of the
// previous handler.
return $x + 1;
})
->then(function ($x) {
// $x === 4
// This handler receives the return value of the
// previous handler.
echo 'Resolve ' . $x;
});
$deferred->resolve(1); // Prints "Resolve 4"
```
#### Rejection forwarding
Rejected promises behave similarly, and also work similarly to try/catch:
When you catch an exception, you must rethrow for it to propagate.
Similarly, when you handle a rejected promise, to propagate the rejection,
"rethrow" it by either returning a rejected promise, or actually throwing
(since promise translates thrown exceptions into rejections)
```php
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
throw new \Exception($x + 1);
})
->otherwise(function (\Exception $x) {
// Propagate the rejection
throw $x;
})
->otherwise(function (\Exception $x) {
// Can also propagate by returning another rejection
return React\Promise\reject(
new \Exception($x->getMessage() + 1)
);
})
->otherwise(function ($x) {
echo 'Reject ' . $x->getMessage(); // 3
});
$deferred->resolve(1); // Prints "Reject 3"
```
#### Mixed resolution and rejection forwarding
Just like try/catch, you can choose to propagate or not. Mixing resolutions and
rejections will still forward handler results in a predictable way.
```php
$deferred = new React\Promise\Deferred();
$deferred->promise()
->then(function ($x) {
return $x + 1;
})
->then(function ($x) {
throw new \Exception($x + 1);
})
->otherwise(function (\Exception $x) {
// Handle the rejection, and don't propagate.
// This is like catch without a rethrow
return $x->getMessage() + 1;
})
->then(function ($x) {
echo 'Mixed ' . $x; // 4
});
$deferred->resolve(1); // Prints "Mixed 4"
```
#### Progress event forwarding
> Deprecated in v2.6.0: Progress support is deprecated and should not be used anymore.
In the same way as resolution and rejection handlers, your progress handler
**MUST** return a progress event to be propagated to the next link in the chain.
If you return nothing, `null` will be propagated.
Also in the same way as resolutions and rejections, if you don't register a
progress handler, the update will be propagated through.
If your progress handler throws an exception, the exception will be propagated
to the next link in the chain. The best thing to do is to ensure your progress
handlers do not throw exceptions.
This gives you the opportunity to transform progress events at each step in the
chain so that they are meaningful to the next step. It also allows you to choose
not to transform them, and simply let them propagate untransformed, by not
registering a progress handler.
```php
$deferred = new React\Promise\Deferred();
$deferred->promise()
->progress(function ($update) {
return $update + 1;
})
->progress(function ($update) {
echo 'Progress ' . $update; // 2
});
$deferred->notify(1); // Prints "Progress 2"
```
### done() vs. then()
The golden rule is:
Either return your promise, or call done() on it.
At a first glance, `then()` and `done()` seem very similar. However, there are
important distinctions.
The intent of `then()` is to transform a promise's value and to pass or return
a new promise for the transformed value along to other parts of your code.
The intent of `done()` is to consume a promise's value, transferring
responsibility for the value to your code.
In addition to transforming a value, `then()` allows you to recover from, or
propagate intermediate errors. Any errors that are not handled will be caught
by the promise machinery and used to reject the promise returned by `then()`.
Calling `done()` transfers all responsibility for errors to your code. If an
error (either a thrown exception or returned rejection) escapes the
`$onFulfilled` or `$onRejected` callbacks you provide to done, it will be
rethrown in an uncatchable way causing a fatal error.
```php
function getJsonResult()
{
return queryApi()
->then(
// Transform API results to an object
function ($jsonResultString) {
return json_decode($jsonResultString);
},
// Transform API errors to an exception
function ($jsonErrorString) {
$object = json_decode($jsonErrorString);
throw new ApiErrorException($object->errorMessage);
}
);
}
// Here we provide no rejection handler. If the promise returned has been
// rejected, the ApiErrorException will be thrown
getJsonResult()
->done(
// Consume transformed object
function ($jsonResultObject) {
// Do something with $jsonResultObject
}
);
// Here we provide a rejection handler which will either throw while debugging
// or log the exception
getJsonResult()
->done(
function ($jsonResultObject) {
// Do something with $jsonResultObject
},
function (ApiErrorException $exception) {
if (isDebug()) {
throw $exception;
} else {
logException($exception);
}
}
);
```
Note that if a rejection value is not an instance of `\Exception`, it will be
wrapped in an exception of the type `React\Promise\UnhandledRejectionException`.
You can get the original rejection reason by calling `$exception->getReason()`.
Install
-------
The recommended way to install this library is [through Composer](https://getcomposer.org).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
This project follows [SemVer](https://semver.org/).
This will install the latest supported version:
```bash
$ composer require react/promise:^2.8
```
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.4 through current PHP 7+ and HHVM.
It's *highly recommended to use PHP 7+* for this project due to its vast
performance improvements.
Credits
-------
Promise is a port of [when.js](https://github.com/cujojs/when)
by [Brian Cavalier](https://github.com/briancavalier).
Also, large parts of the documentation have been ported from the when.js
[Wiki](https://github.com/cujojs/when/wiki) and the
[API docs](https://github.com/cujojs/when/blob/master/docs/api.md).
License
-------
Released under the [MIT](LICENSE) license.

29
vendor/react/promise/composer.json vendored Normal file
View file

@ -0,0 +1,29 @@
{
"name": "react/promise",
"description": "A lightweight implementation of CommonJS Promises/A for PHP",
"license": "MIT",
"authors": [
{"name": "Jan Sorgalla", "email": "jsorgalla@gmail.com"}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0 || ^6.5 || ^5.7 || ^4.8.36"
},
"autoload": {
"psr-4": {
"React\\Promise\\": "src/"
},
"files": ["src/functions_include.php"]
},
"autoload-dev": {
"psr-4": {
"React\\Promise\\": ["tests", "tests/fixtures"]
}
},
"keywords": [
"promise",
"promises"
]
}

27
vendor/react/promise/phpunit.xml.dist vendored Normal file
View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Promise Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/</directory>
<exclude>
<file>./src/functions_include.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>

View file

@ -0,0 +1,17 @@
<?php
namespace React\Promise;
interface CancellablePromiseInterface extends PromiseInterface
{
/**
* The `cancel()` method notifies the creator of the promise that there is no
* further interest in the results of the operation.
*
* Once a promise is settled (either fulfilled or rejected), calling `cancel()` on
* a promise has no effect.
*
* @return void
*/
public function cancel();
}

View file

@ -0,0 +1,55 @@
<?php
namespace React\Promise;
class CancellationQueue
{
private $started = false;
private $queue = [];
public function __invoke()
{
if ($this->started) {
return;
}
$this->started = true;
$this->drain();
}
public function enqueue($cancellable)
{
if (!\is_object($cancellable) || !\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
return;
}
$length = \array_push($this->queue, $cancellable);
if ($this->started && 1 === $length) {
$this->drain();
}
}
private function drain()
{
for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
$cancellable = $this->queue[$i];
$exception = null;
try {
$cancellable->cancel();
} catch (\Throwable $exception) {
} catch (\Exception $exception) {
}
unset($this->queue[$i]);
if ($exception) {
throw $exception;
}
}
$this->queue = [];
}
}

65
vendor/react/promise/src/Deferred.php vendored Normal file
View file

@ -0,0 +1,65 @@
<?php
namespace React\Promise;
class Deferred implements PromisorInterface
{
private $promise;
private $resolveCallback;
private $rejectCallback;
private $notifyCallback;
private $canceller;
public function __construct(callable $canceller = null)
{
$this->canceller = $canceller;
}
public function promise()
{
if (null === $this->promise) {
$this->promise = new Promise(function ($resolve, $reject, $notify) {
$this->resolveCallback = $resolve;
$this->rejectCallback = $reject;
$this->notifyCallback = $notify;
}, $this->canceller);
$this->canceller = null;
}
return $this->promise;
}
public function resolve($value = null)
{
$this->promise();
\call_user_func($this->resolveCallback, $value);
}
public function reject($reason = null)
{
$this->promise();
\call_user_func($this->rejectCallback, $reason);
}
/**
* @deprecated 2.6.0 Progress support is deprecated and should not be used anymore.
* @param mixed $update
*/
public function notify($update = null)
{
$this->promise();
\call_user_func($this->notifyCallback, $update);
}
/**
* @deprecated 2.2.0
* @see Deferred::notify()
*/
public function progress($update = null)
{
$this->notify($update);
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace React\Promise\Exception;
class LengthException extends \LengthException
{
}

View file

@ -0,0 +1,98 @@
<?php
namespace React\Promise;
interface ExtendedPromiseInterface extends PromiseInterface
{
/**
* Consumes the promise's ultimate value if the promise fulfills, or handles the
* ultimate error.
*
* It will cause a fatal error if either `$onFulfilled` or
* `$onRejected` throw or return a rejected promise.
*
* Since the purpose of `done()` is consumption rather than transformation,
* `done()` always returns `null`.
*
* @param callable|null $onFulfilled
* @param callable|null $onRejected
* @param callable|null $onProgress This argument is deprecated and should not be used anymore.
* @return void
*/
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
/**
* Registers a rejection handler for promise. It is a shortcut for:
*
* ```php
* $promise->then(null, $onRejected);
* ```
*
* Additionally, you can type hint the `$reason` argument of `$onRejected` to catch
* only specific errors.
*
* @param callable $onRejected
* @return ExtendedPromiseInterface
*/
public function otherwise(callable $onRejected);
/**
* Allows you to execute "cleanup" type tasks in a promise chain.
*
* It arranges for `$onFulfilledOrRejected` to be called, with no arguments,
* when the promise is either fulfilled or rejected.
*
* * If `$promise` fulfills, and `$onFulfilledOrRejected` returns successfully,
* `$newPromise` will fulfill with the same value as `$promise`.
* * If `$promise` fulfills, and `$onFulfilledOrRejected` throws or returns a
* rejected promise, `$newPromise` will reject with the thrown exception or
* rejected promise's reason.
* * If `$promise` rejects, and `$onFulfilledOrRejected` returns successfully,
* `$newPromise` will reject with the same reason as `$promise`.
* * If `$promise` rejects, and `$onFulfilledOrRejected` throws or returns a
* rejected promise, `$newPromise` will reject with the thrown exception or
* rejected promise's reason.
*
* `always()` behaves similarly to the synchronous finally statement. When combined
* with `otherwise()`, `always()` allows you to write code that is similar to the familiar
* synchronous catch/finally pair.
*
* Consider the following synchronous code:
*
* ```php
* try {
* return doSomething();
* } catch(\Exception $e) {
* return handleError($e);
* } finally {
* cleanup();
* }
* ```
*
* Similar asynchronous code (with `doSomething()` that returns a promise) can be
* written:
*
* ```php
* return doSomething()
* ->otherwise('handleError')
* ->always('cleanup');
* ```
*
* @param callable $onFulfilledOrRejected
* @return ExtendedPromiseInterface
*/
public function always(callable $onFulfilledOrRejected);
/**
* Registers a handler for progress updates from promise. It is a shortcut for:
*
* ```php
* $promise->then(null, null, $onProgress);
* ```
*
* @param callable $onProgress
* @return ExtendedPromiseInterface
* @deprecated 2.6.0 Progress support is deprecated and should not be used anymore.
*/
public function progress(callable $onProgress);
}

View file

@ -0,0 +1,71 @@
<?php
namespace React\Promise;
/**
* @deprecated 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.
*/
class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
private $value;
public function __construct($value = null)
{
if ($value instanceof PromiseInterface) {
throw new \InvalidArgumentException('You cannot create React\Promise\FulfilledPromise with a promise. Use React\Promise\resolve($promiseOrValue) instead.');
}
$this->value = $value;
}
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null === $onFulfilled) {
return $this;
}
try {
return resolve($onFulfilled($this->value));
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null === $onFulfilled) {
return;
}
$result = $onFulfilled($this->value);
if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
}
public function otherwise(callable $onRejected)
{
return $this;
}
public function always(callable $onFulfilledOrRejected)
{
return $this->then(function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
});
}
public function progress(callable $onProgress)
{
return $this;
}
public function cancel()
{
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace React\Promise;
/**
* @deprecated 2.8.0 LazyPromise is deprecated and should not be used anymore.
*/
class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
private $factory;
private $promise;
public function __construct(callable $factory)
{
$this->factory = $factory;
}
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return $this->promise()->then($onFulfilled, $onRejected, $onProgress);
}
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return $this->promise()->done($onFulfilled, $onRejected, $onProgress);
}
public function otherwise(callable $onRejected)
{
return $this->promise()->otherwise($onRejected);
}
public function always(callable $onFulfilledOrRejected)
{
return $this->promise()->always($onFulfilledOrRejected);
}
public function progress(callable $onProgress)
{
return $this->promise()->progress($onProgress);
}
public function cancel()
{
return $this->promise()->cancel();
}
/**
* @internal
* @see Promise::settle()
*/
public function promise()
{
if (null === $this->promise) {
try {
$this->promise = resolve(\call_user_func($this->factory));
} catch (\Throwable $exception) {
$this->promise = new RejectedPromise($exception);
} catch (\Exception $exception) {
$this->promise = new RejectedPromise($exception);
}
}
return $this->promise;
}
}

256
vendor/react/promise/src/Promise.php vendored Normal file
View file

@ -0,0 +1,256 @@
<?php
namespace React\Promise;
class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
private $canceller;
private $result;
private $handlers = [];
private $progressHandlers = [];
private $requiredCancelRequests = 0;
private $cancelRequests = 0;
public function __construct(callable $resolver, callable $canceller = null)
{
$this->canceller = $canceller;
// Explicitly overwrite arguments with null values before invoking
// resolver function. This ensure that these arguments do not show up
// in the stack trace in PHP 7+ only.
$cb = $resolver;
$resolver = $canceller = null;
$this->call($cb);
}
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return $this->result->then($onFulfilled, $onRejected, $onProgress);
}
if (null === $this->canceller) {
return new static($this->resolver($onFulfilled, $onRejected, $onProgress));
}
// This promise has a canceller, so we create a new child promise which
// has a canceller that invokes the parent canceller if all other
// followers are also cancelled. We keep a reference to this promise
// instance for the static canceller function and clear this to avoid
// keeping a cyclic reference between parent and follower.
$parent = $this;
++$parent->requiredCancelRequests;
return new static(
$this->resolver($onFulfilled, $onRejected, $onProgress),
static function () use (&$parent) {
if (++$parent->cancelRequests >= $parent->requiredCancelRequests) {
$parent->cancel();
}
$parent = null;
}
);
}
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return $this->result->done($onFulfilled, $onRejected, $onProgress);
}
$this->handlers[] = static function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
$promise
->done($onFulfilled, $onRejected);
};
if ($onProgress) {
$this->progressHandlers[] = $onProgress;
}
}
public function otherwise(callable $onRejected)
{
return $this->then(null, static function ($reason) use ($onRejected) {
if (!_checkTypehint($onRejected, $reason)) {
return new RejectedPromise($reason);
}
return $onRejected($reason);
});
}
public function always(callable $onFulfilledOrRejected)
{
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
}, static function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
});
}
public function progress(callable $onProgress)
{
return $this->then(null, null, $onProgress);
}
public function cancel()
{
if (null === $this->canceller || null !== $this->result) {
return;
}
$canceller = $this->canceller;
$this->canceller = null;
$this->call($canceller);
}
private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) {
if ($onProgress) {
$progressHandler = static function ($update) use ($notify, $onProgress) {
try {
$notify($onProgress($update));
} catch (\Throwable $e) {
$notify($e);
} catch (\Exception $e) {
$notify($e);
}
};
} else {
$progressHandler = $notify;
}
$this->handlers[] = static function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) {
$promise
->then($onFulfilled, $onRejected)
->done($resolve, $reject, $progressHandler);
};
$this->progressHandlers[] = $progressHandler;
};
}
private function reject($reason = null)
{
if (null !== $this->result) {
return;
}
$this->settle(reject($reason));
}
private function settle(ExtendedPromiseInterface $promise)
{
$promise = $this->unwrap($promise);
if ($promise === $this) {
$promise = new RejectedPromise(
new \LogicException('Cannot resolve a promise with itself.')
);
}
$handlers = $this->handlers;
$this->progressHandlers = $this->handlers = [];
$this->result = $promise;
$this->canceller = null;
foreach ($handlers as $handler) {
$handler($promise);
}
}
private function unwrap($promise)
{
$promise = $this->extract($promise);
while ($promise instanceof self && null !== $promise->result) {
$promise = $this->extract($promise->result);
}
return $promise;
}
private function extract($promise)
{
if ($promise instanceof LazyPromise) {
$promise = $promise->promise();
}
return $promise;
}
private function call(callable $cb)
{
// Explicitly overwrite argument with null value. This ensure that this
// argument does not show up in the stack trace in PHP 7+ only.
$callback = $cb;
$cb = null;
// Use reflection to inspect number of arguments expected by this callback.
// We did some careful benchmarking here: Using reflection to avoid unneeded
// function arguments is actually faster than blindly passing them.
// Also, this helps avoiding unnecessary function arguments in the call stack
// if the callback creates an Exception (creating garbage cycles).
if (\is_array($callback)) {
$ref = new \ReflectionMethod($callback[0], $callback[1]);
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
$ref = new \ReflectionMethod($callback, '__invoke');
} else {
$ref = new \ReflectionFunction($callback);
}
$args = $ref->getNumberOfParameters();
try {
if ($args === 0) {
$callback();
} else {
// Keep references to this promise instance for the static resolve/reject functions.
// By using static callbacks that are not bound to this instance
// and passing the target promise instance by reference, we can
// still execute its resolving logic and still clear this
// reference when settling the promise. This helps avoiding
// garbage cycles if any callback creates an Exception.
// These assumptions are covered by the test suite, so if you ever feel like
// refactoring this, go ahead, any alternative suggestions are welcome!
$target =& $this;
$progressHandlers =& $this->progressHandlers;
$callback(
static function ($value = null) use (&$target) {
if ($target !== null) {
$target->settle(resolve($value));
$target = null;
}
},
static function ($reason = null) use (&$target) {
if ($target !== null) {
$target->reject($reason);
$target = null;
}
},
static function ($update = null) use (&$progressHandlers) {
foreach ($progressHandlers as $handler) {
$handler($update);
}
}
);
}
} catch (\Throwable $e) {
$target = null;
$this->reject($e);
} catch (\Exception $e) {
$target = null;
$this->reject($e);
}
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace React\Promise;
interface PromiseInterface
{
/**
* Transforms a promise's value by applying a function to the promise's fulfillment
* or rejection value. Returns a new promise for the transformed result.
*
* The `then()` method registers new fulfilled and rejection handlers with a promise
* (all parameters are optional):
*
* * `$onFulfilled` will be invoked once the promise is fulfilled and passed
* the result as the first argument.
* * `$onRejected` will be invoked once the promise is rejected and passed the
* reason as the first argument.
* * `$onProgress` (deprecated) will be invoked whenever the producer of the promise
* triggers progress notifications and passed a single argument (whatever it
* wants) to indicate progress.
*
* It returns a new promise that will fulfill with the return value of either
* `$onFulfilled` or `$onRejected`, whichever is called, or will reject with
* the thrown exception if either throws.
*
* A promise makes the following guarantees about handlers registered in
* the same call to `then()`:
*
* 1. Only one of `$onFulfilled` or `$onRejected` will be called,
* never both.
* 2. `$onFulfilled` and `$onRejected` will never be called more
* than once.
* 3. `$onProgress` (deprecated) may be called multiple times.
*
* @param callable|null $onFulfilled
* @param callable|null $onRejected
* @param callable|null $onProgress This argument is deprecated and should not be used anymore.
* @return PromiseInterface
*/
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
}

View file

@ -0,0 +1,13 @@
<?php
namespace React\Promise;
interface PromisorInterface
{
/**
* Returns the promise of the deferred.
*
* @return PromiseInterface
*/
public function promise();
}

View file

@ -0,0 +1,79 @@
<?php
namespace React\Promise;
/**
* @deprecated 2.8.0 External usage of RejectedPromise is deprecated, use `reject()` instead.
*/
class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
private $reason;
public function __construct($reason = null)
{
if ($reason instanceof PromiseInterface) {
throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.');
}
$this->reason = $reason;
}
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null === $onRejected) {
return $this;
}
try {
return resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null === $onRejected) {
throw UnhandledRejectionException::resolve($this->reason);
}
$result = $onRejected($this->reason);
if ($result instanceof self) {
throw UnhandledRejectionException::resolve($result->reason);
}
if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
}
public function otherwise(callable $onRejected)
{
if (!_checkTypehint($onRejected, $this->reason)) {
return $this;
}
return $this->then(null, $onRejected);
}
public function always(callable $onFulfilledOrRejected)
{
return $this->then(null, function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
});
}
public function progress(callable $onProgress)
{
return $this;
}
public function cancel()
{
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace React\Promise;
class UnhandledRejectionException extends \RuntimeException
{
private $reason;
public static function resolve($reason)
{
if ($reason instanceof \Exception || $reason instanceof \Throwable) {
return $reason;
}
return new static($reason);
}
public function __construct($reason)
{
$this->reason = $reason;
$message = \sprintf('Unhandled Rejection: %s', \json_encode($reason));
parent::__construct($message, 0);
}
public function getReason()
{
return $this->reason;
}
}

351
vendor/react/promise/src/functions.php vendored Normal file
View file

@ -0,0 +1,351 @@
<?php
namespace React\Promise;
/**
* Creates a promise for the supplied `$promiseOrValue`.
*
* If `$promiseOrValue` is a value, it will be the resolution value of the
* returned promise.
*
* If `$promiseOrValue` is a thenable (any object that provides a `then()` method),
* a trusted promise that follows the state of the thenable is returned.
*
* If `$promiseOrValue` is a promise, it will be returned as is.
*
* @param mixed $promiseOrValue
* @return PromiseInterface
*/
function resolve($promiseOrValue = null)
{
if ($promiseOrValue instanceof ExtendedPromiseInterface) {
return $promiseOrValue;
}
// Check is_object() first to avoid method_exists() triggering
// class autoloaders if $promiseOrValue is a string.
if (\is_object($promiseOrValue) && \method_exists($promiseOrValue, 'then')) {
$canceller = null;
if (\method_exists($promiseOrValue, 'cancel')) {
$canceller = [$promiseOrValue, 'cancel'];
}
return new Promise(function ($resolve, $reject, $notify) use ($promiseOrValue) {
$promiseOrValue->then($resolve, $reject, $notify);
}, $canceller);
}
return new FulfilledPromise($promiseOrValue);
}
/**
* Creates a rejected promise for the supplied `$promiseOrValue`.
*
* If `$promiseOrValue` is a value, it will be the rejection value of the
* returned promise.
*
* If `$promiseOrValue` is a promise, its completion value will be the rejected
* value of the returned promise.
*
* This can be useful in situations where you need to reject a promise without
* throwing an exception. For example, it allows you to propagate a rejection with
* the value of another promise.
*
* @param mixed $promiseOrValue
* @return PromiseInterface
*/
function reject($promiseOrValue = null)
{
if ($promiseOrValue instanceof PromiseInterface) {
return resolve($promiseOrValue)->then(function ($value) {
return new RejectedPromise($value);
});
}
return new RejectedPromise($promiseOrValue);
}
/**
* Returns a promise that will resolve only once all the items in
* `$promisesOrValues` have resolved. The resolution value of the returned promise
* will be an array containing the resolution values of each of the items in
* `$promisesOrValues`.
*
* @param array $promisesOrValues
* @return PromiseInterface
*/
function all($promisesOrValues)
{
return map($promisesOrValues, function ($val) {
return $val;
});
}
/**
* Initiates a competitive race that allows one winner. Returns a promise which is
* resolved in the same way the first settled promise resolves.
*
* The returned promise will become **infinitely pending** if `$promisesOrValues`
* contains 0 items.
*
* @param array $promisesOrValues
* @return PromiseInterface
*/
function race($promisesOrValues)
{
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($promisesOrValues);
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $cancellationQueue) {
resolve($promisesOrValues)
->done(function ($array) use ($cancellationQueue, $resolve, $reject, $notify) {
if (!is_array($array) || !$array) {
$resolve();
return;
}
foreach ($array as $promiseOrValue) {
$cancellationQueue->enqueue($promiseOrValue);
resolve($promiseOrValue)
->done($resolve, $reject, $notify);
}
}, $reject, $notify);
}, $cancellationQueue);
}
/**
* Returns a promise that will resolve when any one of the items in
* `$promisesOrValues` resolves. The resolution value of the returned promise
* will be the resolution value of the triggering item.
*
* The returned promise will only reject if *all* items in `$promisesOrValues` are
* rejected. The rejection value will be an array of all rejection reasons.
*
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
* if `$promisesOrValues` contains 0 items.
*
* @param array $promisesOrValues
* @return PromiseInterface
*/
function any($promisesOrValues)
{
return some($promisesOrValues, 1)
->then(function ($val) {
return \array_shift($val);
});
}
/**
* Returns a promise that will resolve when `$howMany` of the supplied items in
* `$promisesOrValues` resolve. The resolution value of the returned promise
* will be an array of length `$howMany` containing the resolution values of the
* triggering items.
*
* The returned promise will reject if it becomes impossible for `$howMany` items
* to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
* reject). The rejection value will be an array of
* `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
*
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
* if `$promisesOrValues` contains less items than `$howMany`.
*
* @param array $promisesOrValues
* @param int $howMany
* @return PromiseInterface
*/
function some($promisesOrValues, $howMany)
{
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($promisesOrValues);
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $howMany, $cancellationQueue) {
resolve($promisesOrValues)
->done(function ($array) use ($howMany, $cancellationQueue, $resolve, $reject, $notify) {
if (!\is_array($array) || $howMany < 1) {
$resolve([]);
return;
}
$len = \count($array);
if ($len < $howMany) {
throw new Exception\LengthException(
\sprintf(
'Input array must contain at least %d item%s but contains only %s item%s.',
$howMany,
1 === $howMany ? '' : 's',
$len,
1 === $len ? '' : 's'
)
);
}
$toResolve = $howMany;
$toReject = ($len - $toResolve) + 1;
$values = [];
$reasons = [];
foreach ($array as $i => $promiseOrValue) {
$fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) {
if ($toResolve < 1 || $toReject < 1) {
return;
}
$values[$i] = $val;
if (0 === --$toResolve) {
$resolve($values);
}
};
$rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) {
if ($toResolve < 1 || $toReject < 1) {
return;
}
$reasons[$i] = $reason;
if (0 === --$toReject) {
$reject($reasons);
}
};
$cancellationQueue->enqueue($promiseOrValue);
resolve($promiseOrValue)
->done($fulfiller, $rejecter, $notify);
}
}, $reject, $notify);
}, $cancellationQueue);
}
/**
* Traditional map function, similar to `array_map()`, but allows input to contain
* promises and/or values, and `$mapFunc` may return either a value or a promise.
*
* The map function receives each item as argument, where item is a fully resolved
* value of a promise or value in `$promisesOrValues`.
*
* @param array $promisesOrValues
* @param callable $mapFunc
* @return PromiseInterface
*/
function map($promisesOrValues, callable $mapFunc)
{
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($promisesOrValues);
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $mapFunc, $cancellationQueue) {
resolve($promisesOrValues)
->done(function ($array) use ($mapFunc, $cancellationQueue, $resolve, $reject, $notify) {
if (!\is_array($array) || !$array) {
$resolve([]);
return;
}
$toResolve = \count($array);
$values = [];
foreach ($array as $i => $promiseOrValue) {
$cancellationQueue->enqueue($promiseOrValue);
$values[$i] = null;
resolve($promiseOrValue)
->then($mapFunc)
->done(
function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
$values[$i] = $mapped;
if (0 === --$toResolve) {
$resolve($values);
}
},
$reject,
$notify
);
}
}, $reject, $notify);
}, $cancellationQueue);
}
/**
* Traditional reduce function, similar to `array_reduce()`, but input may contain
* promises and/or values, and `$reduceFunc` may return either a value or a
* promise, *and* `$initialValue` may be a promise or a value for the starting
* value.
*
* @param array $promisesOrValues
* @param callable $reduceFunc
* @param mixed $initialValue
* @return PromiseInterface
*/
function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
{
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($promisesOrValues);
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $reduceFunc, $initialValue, $cancellationQueue) {
resolve($promisesOrValues)
->done(function ($array) use ($reduceFunc, $initialValue, $cancellationQueue, $resolve, $reject, $notify) {
if (!\is_array($array)) {
$array = [];
}
$total = \count($array);
$i = 0;
// Wrap the supplied $reduceFunc with one that handles promises and then
// delegates to the supplied.
$wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $cancellationQueue, $total, &$i) {
$cancellationQueue->enqueue($val);
return $current
->then(function ($c) use ($reduceFunc, $total, &$i, $val) {
return resolve($val)
->then(function ($value) use ($reduceFunc, $total, &$i, $c) {
return $reduceFunc($c, $value, $i++, $total);
});
});
};
$cancellationQueue->enqueue($initialValue);
\array_reduce($array, $wrappedReduceFunc, resolve($initialValue))
->done($resolve, $reject, $notify);
}, $reject, $notify);
}, $cancellationQueue);
}
/**
* @internal
*/
function _checkTypehint(callable $callback, $object)
{
if (!\is_object($object)) {
return true;
}
if (\is_array($callback)) {
$callbackReflection = new \ReflectionMethod($callback[0], $callback[1]);
} elseif (\is_object($callback) && !$callback instanceof \Closure) {
$callbackReflection = new \ReflectionMethod($callback, '__invoke');
} else {
$callbackReflection = new \ReflectionFunction($callback);
}
$parameters = $callbackReflection->getParameters();
if (!isset($parameters[0])) {
return true;
}
$expectedException = $parameters[0];
if (!$expectedException->getClass()) {
return true;
}
return $expectedException->getClass()->isInstance($object);
}

View file

@ -0,0 +1,5 @@
<?php
if (!\function_exists('React\Promise\resolve')) {
require __DIR__.'/functions.php';
}

View file

@ -0,0 +1,100 @@
<?php
namespace React\Promise;
class CancellationQueueTest extends TestCase
{
/** @test */
public function acceptsSimpleCancellableThenable()
{
$p = new SimpleTestCancellableThenable();
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($p);
$cancellationQueue();
$this->assertTrue($p->cancelCalled);
}
/** @test */
public function ignoresSimpleCancellable()
{
$p = new SimpleTestCancellable();
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($p);
$cancellationQueue();
$this->assertFalse($p->cancelCalled);
}
/** @test */
public function callsCancelOnPromisesEnqueuedBeforeStart()
{
$d1 = $this->getCancellableDeferred();
$d2 = $this->getCancellableDeferred();
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($d1->promise());
$cancellationQueue->enqueue($d2->promise());
$cancellationQueue();
}
/** @test */
public function callsCancelOnPromisesEnqueuedAfterStart()
{
$d1 = $this->getCancellableDeferred();
$d2 = $this->getCancellableDeferred();
$cancellationQueue = new CancellationQueue();
$cancellationQueue();
$cancellationQueue->enqueue($d2->promise());
$cancellationQueue->enqueue($d1->promise());
}
/** @test */
public function doesNotCallCancelTwiceWhenStartedTwice()
{
$d = $this->getCancellableDeferred();
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($d->promise());
$cancellationQueue();
$cancellationQueue();
}
/** @test */
public function rethrowsExceptionsThrownFromCancel()
{
$this->setExpectedException('\Exception', 'test');
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel')
->will($this->throwException(new \Exception('test')));
$cancellationQueue = new CancellationQueue();
$cancellationQueue->enqueue($mock);
$cancellationQueue();
}
private function getCancellableDeferred()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');
return new Deferred($mock);
}
}

View file

@ -0,0 +1,116 @@
<?php
namespace React\Promise;
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
class DeferredTest extends TestCase
{
use PromiseTest\FullTestTrait;
public function getPromiseTestAdapter(callable $canceller = null)
{
$d = new Deferred($canceller);
return new CallbackPromiseAdapter([
'promise' => [$d, 'promise'],
'resolve' => [$d, 'resolve'],
'reject' => [$d, 'reject'],
'notify' => [$d, 'progress'],
'settle' => [$d, 'resolve'],
]);
}
/** @test */
public function progressIsAnAliasForNotify()
{
$deferred = new Deferred();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
$deferred->promise()
->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
$deferred->progress($sentinel);
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException()
{
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$deferred = new Deferred(function ($resolve, $reject) {
$reject(new \Exception('foo'));
});
$deferred->promise()->cancel();
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException()
{
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$deferred = new Deferred(function ($resolve, $reject) {
$reject(new \Exception('foo'));
});
$deferred->promise()->then()->cancel();
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndExplicitlyRejectWithException()
{
gc_collect_cycles();
$deferred = new Deferred(function () use (&$deferred) { });
$deferred->reject(new \Exception('foo'));
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferred()
{
gc_collect_cycles();
$deferred = new Deferred();
$deferred->promise();
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithUnusedCanceller()
{
gc_collect_cycles();
$deferred = new Deferred(function () { });
$deferred->promise();
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithNoopCanceller()
{
gc_collect_cycles();
$deferred = new Deferred(function () { });
$deferred->promise()->cancel();
unset($deferred);
$this->assertSame(0, gc_collect_cycles());
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace React\Promise;
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
class FulfilledPromiseTest extends TestCase
{
use PromiseTest\PromiseSettledTestTrait,
PromiseTest\PromiseFulfilledTestTrait;
public function getPromiseTestAdapter(callable $canceller = null)
{
$promise = null;
return new CallbackPromiseAdapter([
'promise' => function () use (&$promise) {
if (!$promise) {
throw new \LogicException('FulfilledPromise must be resolved before obtaining the promise');
}
return $promise;
},
'resolve' => function ($value = null) use (&$promise) {
if (!$promise) {
$promise = new FulfilledPromise($value);
}
},
'reject' => function () {
throw new \LogicException('You cannot call reject() for React\Promise\FulfilledPromise');
},
'notify' => function () {
// no-op
},
'settle' => function ($value = null) use (&$promise) {
if (!$promise) {
$promise = new FulfilledPromise($value);
}
},
]);
}
/** @test */
public function shouldThrowExceptionIfConstructedWithAPromise()
{
$this->setExpectedException('\InvalidArgumentException');
return new FulfilledPromise(new FulfilledPromise());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithAlwaysFollowers()
{
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$promise = new FulfilledPromise(1);
$promise->always(function () {
throw new \RuntimeException();
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithThenFollowers()
{
gc_collect_cycles();
$promise = new FulfilledPromise(1);
$promise = $promise->then(function () {
throw new \RuntimeException();
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
}

View file

@ -0,0 +1,114 @@
<?php
namespace React\Promise;
class FunctionAllTest extends TestCase
{
/** @test */
public function shouldResolveEmptyInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
all([])
->then($mock);
}
/** @test */
public function shouldResolveValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2, 3]));
all([1, 2, 3])
->then($mock);
}
/** @test */
public function shouldResolvePromisesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2, 3]));
all([resolve(1), resolve(2), resolve(3)])
->then($mock);
}
/** @test */
public function shouldResolveSparseArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([null, 1, null, 1, 1]));
all([null, 1, null, 1, 1])
->then($mock);
}
/** @test */
public function shouldRejectIfAnyInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
all([resolve(1), reject(2), resolve(3)])
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2, 3]));
all(resolve([1, 2, 3]))
->then($mock);
}
/** @test */
public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
all(resolve(1))
->then($mock);
}
/** @test */
public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2, 3]));
$deferred = new Deferred();
all([resolve(1), $deferred->promise(), resolve(3)])
->then($mock);
$deferred->resolve(2);
}
}

View file

@ -0,0 +1,204 @@
<?php
namespace React\Promise;
use React\Promise\Exception\LengthException;
class FunctionAnyTest extends TestCase
{
/** @test */
public function shouldRejectWithLengthExceptionWithEmptyInputArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(
$this->callback(function($exception){
return $exception instanceof LengthException &&
'Input array must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
})
);
any([])
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldResolveToNullWithNonArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
any(null)
->then($mock);
}
/** @test */
public function shouldResolveWithAnInputValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
any([1, 2, 3])
->then($mock);
}
/** @test */
public function shouldResolveWithAPromisedInputValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
any([resolve(1), resolve(2), resolve(3)])
->then($mock);
}
/** @test */
public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([0 => 1, 1 => 2, 2 => 3]));
any([reject(1), reject(2), reject(3)])
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldResolveWhenFirstInputPromiseResolves()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
any([resolve(1), reject(2), reject(3)])
->then($mock);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
any(resolve([1, 2, 3]))
->then($mock);
}
/** @test */
public function shouldResolveToNullArrayWhenInputPromiseDoesNotResolveToArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
any(resolve(1))
->then($mock);
}
/** @test */
public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$d1 = new Deferred();
$d2 = new Deferred();
any(['abc' => $d1->promise(), 1 => $d2->promise()])
->then($mock);
$d2->resolve(2);
$d1->resolve(1);
}
/** @test */
public function shouldRejectWhenInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
any(reject())
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldCancelInputPromise()
{
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel');
any($mock)->cancel();
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->once())
->method('cancel');
any([$mock1, $mock2])->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
$deferred = New Deferred($mock);
$deferred->resolve();
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->never())
->method('cancel');
some([$deferred->promise(), $mock2], 1)->cancel();
}
}

View file

@ -0,0 +1,118 @@
<?php
namespace React\Promise;
class FunctionCheckTypehintTest extends TestCase
{
/** @test */
public function shouldAcceptClosureCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint(function (\InvalidArgumentException $e) {
}, new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(function (\InvalidArgumentException $e) {
}, new \Exception()));
}
/** @test */
public function shouldAcceptFunctionStringCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint('React\Promise\testCallbackWithTypehint', new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint('React\Promise\testCallbackWithTypehint', new \Exception()));
}
/** @test */
public function shouldAcceptInvokableObjectCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint(new TestCallbackWithTypehintClass(), new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(new TestCallbackWithTypehintClass(), new \Exception()));
}
/** @test */
public function shouldAcceptObjectMethodCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint([new TestCallbackWithTypehintClass(), 'testCallback'], new \Exception()));
}
/** @test */
public function shouldAcceptStaticClassCallbackWithTypehint()
{
$this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
$this->assertfalse(_checkTypehint(['React\Promise\TestCallbackWithTypehintClass', 'testCallbackStatic'], new \Exception()));
}
/** @test */
public function shouldAcceptClosureCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint(function (\InvalidArgumentException $e) {
}, new \InvalidArgumentException()));
}
/** @test */
public function shouldAcceptFunctionStringCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint('React\Promise\testCallbackWithoutTypehint', new \InvalidArgumentException()));
}
/** @test */
public function shouldAcceptInvokableObjectCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint(new TestCallbackWithoutTypehintClass(), new \InvalidArgumentException()));
}
/** @test */
public function shouldAcceptObjectMethodCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint([new TestCallbackWithoutTypehintClass(), 'testCallback'], new \InvalidArgumentException()));
}
/** @test */
public function shouldAcceptStaticClassCallbackWithoutTypehint()
{
$this->assertTrue(_checkTypehint(['React\Promise\TestCallbackWithoutTypehintClass', 'testCallbackStatic'], new \InvalidArgumentException()));
}
}
function testCallbackWithTypehint(\InvalidArgumentException $e)
{
}
function testCallbackWithoutTypehint()
{
}
class TestCallbackWithTypehintClass
{
public function __invoke(\InvalidArgumentException $e)
{
}
public function testCallback(\InvalidArgumentException $e)
{
}
public static function testCallbackStatic(\InvalidArgumentException $e)
{
}
}
class TestCallbackWithoutTypehintClass
{
public function __invoke()
{
}
public function testCallback()
{
}
public static function testCallbackStatic()
{
}
}

View file

@ -0,0 +1,198 @@
<?php
namespace React\Promise;
class FunctionMapTest extends TestCase
{
protected function mapper()
{
return function ($val) {
return $val * 2;
};
}
protected function promiseMapper()
{
return function ($val) {
return resolve($val * 2);
};
}
/** @test */
public function shouldMapInputValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
map(
[1, 2, 3],
$this->mapper()
)->then($mock);
}
/** @test */
public function shouldMapInputPromisesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
map(
[resolve(1), resolve(2), resolve(3)],
$this->mapper()
)->then($mock);
}
/** @test */
public function shouldMapMixedInputArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
map(
[1, resolve(2), 3],
$this->mapper()
)->then($mock);
}
/** @test */
public function shouldMapInputWhenMapperReturnsAPromise()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
map(
[1, 2, 3],
$this->promiseMapper()
)->then($mock);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
map(
resolve([1, resolve(2), 3]),
$this->mapper()
)->then($mock);
}
/** @test */
public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
map(
resolve(1),
$this->mapper()
)->then($mock);
}
/** @test */
public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([2, 4, 6]));
$deferred = new Deferred();
map(
[resolve(1), $deferred->promise(), resolve(3)],
$this->mapper()
)->then($mock);
$deferred->resolve(2);
}
/** @test */
public function shouldRejectWhenInputContainsRejection()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
map(
[resolve(1), reject(2), resolve(3)],
$this->mapper()
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldRejectWhenInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
map(
reject(),
$this->mapper()
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldCancelInputPromise()
{
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel');
map(
$mock,
$this->mapper()
)->cancel();
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->once())
->method('cancel');
map(
[$mock1, $mock2],
$this->mapper()
)->cancel();
}
}

View file

@ -0,0 +1,211 @@
<?php
namespace React\Promise;
class FunctionRaceTest extends TestCase
{
/** @test */
public function shouldResolveEmptyInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
race(
[]
)->then($mock);
}
/** @test */
public function shouldResolveValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
race(
[1, 2, 3]
)->then($mock);
}
/** @test */
public function shouldResolvePromisesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$d1 = new Deferred();
$d2 = new Deferred();
$d3 = new Deferred();
race(
[$d1->promise(), $d2->promise(), $d3->promise()]
)->then($mock);
$d2->resolve(2);
$d1->resolve(1);
$d3->resolve(3);
}
/** @test */
public function shouldResolveSparseArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
race(
[null, 1, null, 2, 3]
)->then($mock);
}
/** @test */
public function shouldRejectIfFirstSettledPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$d1 = new Deferred();
$d2 = new Deferred();
$d3 = new Deferred();
race(
[$d1->promise(), $d2->promise(), $d3->promise()]
)->then($this->expectCallableNever(), $mock);
$d2->reject(2);
$d1->resolve(1);
$d3->resolve(3);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
race(
resolve([1, 2, 3])
)->then($mock);
}
/** @test */
public function shouldResolveToNullWhenInputPromiseDoesNotResolveToArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
race(
resolve(1)
)->then($mock);
}
/** @test */
public function shouldRejectWhenInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
race(
reject()
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldCancelInputPromise()
{
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel');
race($mock)->cancel();
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->once())
->method('cancel');
race([$mock1, $mock2])->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
$deferred = New Deferred($mock);
$deferred->resolve();
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->never())
->method('cancel');
race([$deferred->promise(), $mock2])->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
$deferred = New Deferred($mock);
$deferred->reject();
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->never())
->method('cancel');
race([$deferred->promise(), $mock2])->cancel();
}
}

View file

@ -0,0 +1,347 @@
<?php
namespace React\Promise;
class FunctionReduceTest extends TestCase
{
protected function plus()
{
return function ($sum, $val) {
return $sum + $val;
};
}
protected function append()
{
return function ($sum, $val) {
return $sum . $val;
};
}
/** @test */
public function shouldReduceValuesWithoutInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(6));
reduce(
[1, 2, 3],
$this->plus()
)->then($mock);
}
/** @test */
public function shouldReduceValuesWithInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(7));
reduce(
[1, 2, 3],
$this->plus(),
1
)->then($mock);
}
/** @test */
public function shouldReduceValuesWithInitialPromise()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(7));
reduce(
[1, 2, 3],
$this->plus(),
resolve(1)
)->then($mock);
}
/** @test */
public function shouldReducePromisedValuesWithoutInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(6));
reduce(
[resolve(1), resolve(2), resolve(3)],
$this->plus()
)->then($mock);
}
/** @test */
public function shouldReducePromisedValuesWithInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(7));
reduce(
[resolve(1), resolve(2), resolve(3)],
$this->plus(),
1
)->then($mock);
}
/** @test */
public function shouldReducePromisedValuesWithInitialPromise()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(7));
reduce(
[resolve(1), resolve(2), resolve(3)],
$this->plus(),
resolve(1)
)->then($mock);
}
/** @test */
public function shouldReduceEmptyInputWithInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
reduce(
[],
$this->plus(),
1
)->then($mock);
}
/** @test */
public function shouldReduceEmptyInputWithInitialPromise()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
reduce(
[],
$this->plus(),
resolve(1)
)->then($mock);
}
/** @test */
public function shouldRejectWhenInputContainsRejection()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
reduce(
[resolve(1), reject(2), resolve(3)],
$this->plus(),
resolve(1)
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldResolveWithNullWhenInputIsEmptyAndNoInitialValueOrPromiseProvided()
{
// Note: this is different from when.js's behavior!
// In when.reduce(), this rejects with a TypeError exception (following
// JavaScript's [].reduce behavior.
// We're following PHP's array_reduce behavior and resolve with NULL.
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
reduce(
[],
$this->plus()
)->then($mock);
}
/** @test */
public function shouldAllowSparseArrayInputWithoutInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(3));
reduce(
[null, null, 1, null, 1, 1],
$this->plus()
)->then($mock);
}
/** @test */
public function shouldAllowSparseArrayInputWithInitialValue()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(4));
reduce(
[null, null, 1, null, 1, 1],
$this->plus(),
1
)->then($mock);
}
/** @test */
public function shouldReduceInInputOrder()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo('123'));
reduce(
[1, 2, 3],
$this->append(),
''
)->then($mock);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo('123'));
reduce(
resolve([1, 2, 3]),
$this->append(),
''
)->then($mock);
}
/** @test */
public function shouldResolveToInitialValueWhenInputPromiseDoesNotResolveToAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
reduce(
resolve(1),
$this->plus(),
1
)->then($mock);
}
/** @test */
public function shouldProvideCorrectBasisValue()
{
$insertIntoArray = function ($arr, $val, $i) {
$arr[$i] = $val;
return $arr;
};
$d1 = new Deferred();
$d2 = new Deferred();
$d3 = new Deferred();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2, 3]));
reduce(
[$d1->promise(), $d2->promise(), $d3->promise()],
$insertIntoArray,
[]
)->then($mock);
$d3->resolve(3);
$d1->resolve(1);
$d2->resolve(2);
}
/** @test */
public function shouldRejectWhenInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
reduce(
reject(),
$this->plus(),
1
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldCancelInputPromise()
{
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel');
reduce(
$mock,
$this->plus(),
1
)->cancel();
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->once())
->method('cancel');
reduce(
[$mock1, $mock2],
$this->plus(),
1
)->cancel();
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace React\Promise;
class FunctionRejectTest extends TestCase
{
/** @test */
public function shouldRejectAnImmediateValue()
{
$expected = 123;
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
reject($expected)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function shouldRejectAFulfilledPromise()
{
$expected = 123;
$resolved = new FulfilledPromise($expected);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
reject($resolved)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function shouldRejectARejectedPromise()
{
$expected = 123;
$resolved = new RejectedPromise($expected);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
reject($resolved)
->then(
$this->expectCallableNever(),
$mock
);
}
}

View file

@ -0,0 +1,171 @@
<?php
namespace React\Promise;
class FunctionResolveTest extends TestCase
{
/** @test */
public function shouldResolveAnImmediateValue()
{
$expected = 123;
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
resolve($expected)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldResolveAFulfilledPromise()
{
$expected = 123;
$resolved = new FulfilledPromise($expected);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
resolve($resolved)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldResolveAThenable()
{
$thenable = new SimpleFulfilledTestThenable();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo('foo'));
resolve($thenable)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldResolveACancellableThenable()
{
$thenable = new SimpleTestCancellableThenable();
$promise = resolve($thenable);
$promise->cancel();
$this->assertTrue($thenable->cancelCalled);
}
/** @test */
public function shouldRejectARejectedPromise()
{
$expected = 123;
$resolved = new RejectedPromise($expected);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($expected));
resolve($resolved)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function shouldSupportDeepNestingInPromiseChains()
{
$d = new Deferred();
$d->resolve(false);
$result = resolve(resolve($d->promise()->then(function ($val) {
$d = new Deferred();
$d->resolve($val);
$identity = function ($val) {
return $val;
};
return resolve($d->promise()->then($identity))->then(
function ($val) {
return !$val;
}
);
})));
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(true));
$result->then($mock);
}
/** @test */
public function shouldSupportVeryDeepNestedPromises()
{
$deferreds = [];
// @TODO Increase count once global-queue is merged
for ($i = 0; $i < 10; $i++) {
$deferreds[] = $d = new Deferred();
$p = $d->promise();
$last = $p;
for ($j = 0; $j < 10; $j++) {
$last = $last->then(function($result) {
return $result;
});
}
}
$p = null;
foreach ($deferreds as $d) {
if ($p) {
$d->resolve($p);
}
$p = $d->promise();
}
$deferreds[0]->resolve(true);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(true));
$deferreds[0]->promise()->then($mock);
}
/** @test */
public function returnsExtendePromiseForSimplePromise()
{
$promise = $this
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$this->assertInstanceOf('React\Promise\ExtendedPromiseInterface', resolve($promise));
}
}

View file

@ -0,0 +1,258 @@
<?php
namespace React\Promise;
use React\Promise\Exception\LengthException;
class FunctionSomeTest extends TestCase
{
/** @test */
public function shouldRejectWithLengthExceptionWithEmptyInputArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(
$this->callback(function($exception){
return $exception instanceof LengthException &&
'Input array must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
})
);
some(
[],
1
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldRejectWithLengthExceptionWithInputArrayContainingNotEnoughItems()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(
$this->callback(function($exception){
return $exception instanceof LengthException &&
'Input array must contain at least 4 items but contains only 3 items.' === $exception->getMessage();
})
);
some(
[1, 2, 3],
4
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldResolveToEmptyArrayWithNonArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
some(
null,
1
)->then($mock);
}
/** @test */
public function shouldResolveValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2]));
some(
[1, 2, 3],
2
)->then($mock);
}
/** @test */
public function shouldResolvePromisesArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2]));
some(
[resolve(1), resolve(2), resolve(3)],
2
)->then($mock);
}
/** @test */
public function shouldResolveSparseArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([null, 1]));
some(
[null, 1, null, 2, 3],
2
)->then($mock);
}
/** @test */
public function shouldRejectIfAnyInputPromiseRejectsBeforeDesiredNumberOfInputsAreResolved()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1 => 2, 2 => 3]));
some(
[resolve(1), reject(2), reject(3)],
2
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldAcceptAPromiseForAnArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([1, 2]));
some(
resolve([1, 2, 3]),
2
)->then($mock);
}
/** @test */
public function shouldResolveWithEmptyArrayIfHowManyIsLessThanOne()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
some(
[1],
0
)->then($mock);
}
/** @test */
public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo([]));
some(
resolve(1),
1
)->then($mock);
}
/** @test */
public function shouldRejectWhenInputPromiseRejects()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));
some(
reject(),
1
)->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldCancelInputPromise()
{
$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock
->expects($this->once())
->method('cancel');
some($mock, 1)->cancel();
}
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->once())
->method('cancel');
some([$mock1, $mock2], 1)->cancel();
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
$deferred = New Deferred($mock);
$deferred->resolve();
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->never())
->method('cancel');
some([$deferred->promise(), $mock2], 1);
}
/** @test */
public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
$deferred = New Deferred($mock);
$deferred->reject();
$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMock();
$mock2
->expects($this->never())
->method('cancel');
some([$deferred->promise(), $mock2], 2);
}
}

View file

@ -0,0 +1,107 @@
<?php
namespace React\Promise;
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
class LazyPromiseTest extends TestCase
{
use PromiseTest\FullTestTrait;
public function getPromiseTestAdapter(callable $canceller = null)
{
$d = new Deferred($canceller);
$factory = function () use ($d) {
return $d->promise();
};
return new CallbackPromiseAdapter([
'promise' => function () use ($factory) {
return new LazyPromise($factory);
},
'resolve' => [$d, 'resolve'],
'reject' => [$d, 'reject'],
'notify' => [$d, 'progress'],
'settle' => [$d, 'resolve'],
]);
}
/** @test */
public function shouldNotCallFactoryIfThenIsNotInvoked()
{
$factory = $this->createCallableMock();
$factory
->expects($this->never())
->method('__invoke');
new LazyPromise($factory);
}
/** @test */
public function shouldCallFactoryIfThenIsInvoked()
{
$factory = $this->createCallableMock();
$factory
->expects($this->once())
->method('__invoke');
$p = new LazyPromise($factory);
$p->then();
}
/** @test */
public function shouldReturnPromiseFromFactory()
{
$factory = $this->createCallableMock();
$factory
->expects($this->once())
->method('__invoke')
->will($this->returnValue(new FulfilledPromise(1)));
$onFulfilled = $this->createCallableMock();
$onFulfilled
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$p = new LazyPromise($factory);
$p->then($onFulfilled);
}
/** @test */
public function shouldReturnPromiseIfFactoryReturnsNull()
{
$factory = $this->createCallableMock();
$factory
->expects($this->once())
->method('__invoke')
->will($this->returnValue(null));
$p = new LazyPromise($factory);
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $p->then());
}
/** @test */
public function shouldReturnRejectedPromiseIfFactoryThrowsException()
{
$exception = new \Exception();
$factory = $this->createCallableMock();
$factory
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$onRejected = $this->createCallableMock();
$onRejected
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$p = new LazyPromise($factory);
$p->then($this->expectCallableNever(), $onRejected);
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace React\Promise\PromiseAdapter;
use React\Promise;
class CallbackPromiseAdapter implements PromiseAdapterInterface
{
private $callbacks;
public function __construct(array $callbacks)
{
$this->callbacks = $callbacks;
}
public function promise()
{
return call_user_func_array($this->callbacks['promise'], func_get_args());
}
public function resolve()
{
return call_user_func_array($this->callbacks['resolve'], func_get_args());
}
public function reject()
{
return call_user_func_array($this->callbacks['reject'], func_get_args());
}
public function notify()
{
return call_user_func_array($this->callbacks['notify'], func_get_args());
}
public function settle()
{
return call_user_func_array($this->callbacks['settle'], func_get_args());
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace React\Promise\PromiseAdapter;
use React\Promise;
interface PromiseAdapterInterface
{
public function promise();
public function resolve();
public function reject();
public function notify();
public function settle();
}

View file

@ -0,0 +1,295 @@
<?php
namespace React\Promise;
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
class PromiseTest extends TestCase
{
use PromiseTest\FullTestTrait;
public function getPromiseTestAdapter(callable $canceller = null)
{
$resolveCallback = $rejectCallback = $progressCallback = null;
$promise = new Promise(function ($resolve, $reject, $progress) use (&$resolveCallback, &$rejectCallback, &$progressCallback) {
$resolveCallback = $resolve;
$rejectCallback = $reject;
$progressCallback = $progress;
}, $canceller);
return new CallbackPromiseAdapter([
'promise' => function () use ($promise) {
return $promise;
},
'resolve' => $resolveCallback,
'reject' => $rejectCallback,
'notify' => $progressCallback,
'settle' => $resolveCallback,
]);
}
/** @test */
public function shouldRejectIfResolverThrowsException()
{
$exception = new \Exception('foo');
$promise = new Promise(function () use ($exception) {
throw $exception;
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$promise
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldResolveWithoutCreatingGarbageCyclesIfResolverResolvesWithException()
{
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$promise = new Promise(function ($resolve) {
$resolve(new \Exception('foo'));
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptionWithoutResolver()
{
gc_collect_cycles();
$promise = new Promise(function () {
throw new \Exception('foo');
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverRejectsWithException()
{
gc_collect_cycles();
$promise = new Promise(function ($resolve, $reject) {
$reject(new \Exception('foo'));
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException()
{
gc_collect_cycles();
$promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) {
$reject(new \Exception('foo'));
});
$promise->cancel();
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException()
{
gc_collect_cycles();
$promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) {
$reject(new \Exception('foo'));
});
$promise->then()->then()->then()->cancel();
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException()
{
gc_collect_cycles();
$promise = new Promise(function ($resolve, $reject) {
throw new \Exception('foo');
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/**
* Test that checks number of garbage cycles after throwing from a canceller
* that explicitly uses a reference to the promise. This is rather synthetic,
* actual use cases often have implicit (hidden) references which ought not
* to be stored in the stack trace.
*
* Reassigned arguments only show up in the stack trace in PHP 7, so we can't
* avoid this on legacy PHP. As an alternative, consider explicitly unsetting
* any references before throwing.
*
* @test
* @requires PHP 7
*/
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException()
{
gc_collect_cycles();
$promise = new Promise(function () {}, function () use (&$promise) {
throw new \Exception('foo');
});
$promise->cancel();
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/**
* @test
* @requires PHP 7
* @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
*/
public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException()
{
gc_collect_cycles();
$promise = new Promise(function () use (&$promise) {
throw new \Exception('foo');
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/**
* @test
* @requires PHP 7
* @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
*/
public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndResolverThrowsException()
{
gc_collect_cycles();
$promise = new Promise(function () {
throw new \Exception('foo');
}, function () use (&$promise) { });
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldIgnoreNotifyAfterReject()
{
$promise = new Promise(function () { }, function ($resolve, $reject, $notify) {
$reject(new \Exception('foo'));
$notify(42);
});
$promise->then(null, null, $this->expectCallableNever());
$promise->cancel();
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromise()
{
gc_collect_cycles();
$promise = new Promise(function () { });
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithThenFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->then()->then()->then();
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithDoneFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->done();
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithOtherwiseFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->otherwise(function () { });
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithAlwaysFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->always(function () { });
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithProgressFollowers()
{
gc_collect_cycles();
$promise = new Promise(function () { });
$promise->then(null, null, function () { });
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldFulfillIfFullfilledWithSimplePromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo('foo'));
$adapter->promise()
->then($mock);
$adapter->resolve(new SimpleFulfilledTestPromise());
}
/** @test */
public function shouldRejectIfRejectedWithSimplePromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo('foo'));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->resolve(new SimpleRejectedTestPromise());
}
}

View file

@ -0,0 +1,246 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
trait CancelTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function cancelShouldCallCancellerWithResolverArguments()
{
$args = null;
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject, $notify) use (&$args) {
$args = func_get_args();
});
$adapter->promise()->cancel();
$this->assertCount(3, $args);
$this->assertTrue(is_callable($args[0]));
$this->assertTrue(is_callable($args[1]));
$this->assertTrue(is_callable($args[2]));
}
/** @test */
public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed()
{
$args = null;
$adapter = $this->getPromiseTestAdapter(function () use (&$args) {
$args = func_num_args();
});
$adapter->promise()->cancel();
$this->assertSame(0, $args);
}
/** @test */
public function cancelShouldFulfillPromiseIfCancellerFulfills()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve) {
$resolve(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock, $this->expectCallableNever());
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldRejectPromiseIfCancellerRejects()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) {
$reject(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows()
{
$e = new \Exception();
$adapter = $this->getPromiseTestAdapter(function () use ($e) {
throw $e;
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($e));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldProgressPromiseIfCancellerNotifies()
{
$adapter = $this->getPromiseTestAdapter(function ($resolve, $reject, $progress) {
$progress(1);
});
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnCallback(function ($resolve) {
$resolve();
}));
$adapter = $this->getPromiseTestAdapter($mock);
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldHaveNoEffectIfCancellerDoesNothing()
{
$adapter = $this->getPromiseTestAdapter(function () {});
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever());
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldCallCancellerFromDeepNestedPromiseChain()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');
$adapter = $this->getPromiseTestAdapter($mock);
$promise = $adapter->promise()
->then(function () {
return new Promise\Promise(function () {});
})
->then(function () {
$d = new Promise\Deferred();
return $d->promise();
})
->then(function () {
return new Promise\Promise(function () {});
});
$promise->cancel();
}
/** @test */
public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$child1 = $adapter->promise()
->then()
->then();
$adapter->promise()
->then();
$child1->cancel();
}
/** @test */
public function cancelShouldTriggerCancellerWhenAllChildrenCancel()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$child1 = $adapter->promise()
->then()
->then();
$child2 = $adapter->promise()
->then();
$child1->cancel();
$child2->cancel();
}
/** @test */
public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$child1 = $adapter->promise()
->then()
->then();
$child2 = $adapter->promise()
->then();
$child1->cancel();
$child1->cancel();
}
/** @test */
public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$adapter->promise()->cancel();
$adapter->promise()->cancel();
}
/** @test */
public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
$adapter->promise()
->then()
->then();
$adapter->promise()
->then();
$adapter->promise()->cancel();
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace React\Promise\PromiseTest;
trait FullTestTrait
{
use PromisePendingTestTrait,
PromiseSettledTestTrait,
PromiseFulfilledTestTrait,
PromiseRejectedTestTrait,
ResolveTestTrait,
RejectTestTrait,
NotifyTestTrait,
CancelTestTrait;
}

View file

@ -0,0 +1,336 @@
<?php
namespace React\Promise\PromiseTest;
trait NotifyTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function notifyShouldProgress()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
$adapter->notify($sentinel);
}
/** @test */
public function notifyShouldPropagateProgressToDownstreamPromises()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnArgument(0));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify($sentinel);
}
/** @test */
public function notifyShouldPropagateTransformedProgressToDownstreamPromises()
{
$adapter = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnValue($sentinel));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify(1);
}
/** @test */
public function notifyShouldPropagateCaughtExceptionValueAsProgress()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->notify(1);
}
/** @test */
public function notifyShouldForwardProgressEventsWhenIntermediaryCallbackTiedToAResolvedPromiseReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$promise2 = $adapter2->promise();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
// resolve BEFORE attaching progress handler
$adapter->resolve();
$adapter->promise()
->then(function () use ($promise2) {
return $promise2;
})
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
);
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldForwardProgressEventsWhenIntermediaryCallbackTiedToAnUnresolvedPromiseReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$promise2 = $adapter2->promise();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(function () use ($promise2) {
return $promise2;
})
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
);
// resolve AFTER attaching progress handler
$adapter->resolve();
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldForwardProgressWhenResolvedWithAnotherPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$sentinel = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->returnValue($sentinel));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($sentinel);
$adapter->promise()
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$this->expectCallableNever(),
$mock2
);
$adapter->resolve($adapter2->promise());
$adapter2->notify($sentinel);
}
/** @test */
public function notifyShouldAllowResolveAfterProgress()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->at(0))
->method('__invoke')
->with($this->identicalTo(1));
$mock
->expects($this->at(1))
->method('__invoke')
->with($this->identicalTo(2));
$adapter->promise()
->then(
$mock,
$this->expectCallableNever(),
$mock
);
$adapter->notify(1);
$adapter->resolve(2);
}
/** @test */
public function notifyShouldAllowRejectAfterProgress()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->at(0))
->method('__invoke')
->with($this->identicalTo(1));
$mock
->expects($this->at(1))
->method('__invoke')
->with($this->identicalTo(2));
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock,
$mock
);
$adapter->notify(1);
$adapter->reject(2);
}
/** @test */
public function notifyShouldReturnSilentlyOnProgressWhenAlreadyRejected()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(1);
$this->assertNull($adapter->notify());
}
/** @test */
public function notifyShouldInvokeProgressHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()->progress($mock);
$adapter->notify(1);
}
/** @test */
public function notifyShouldInvokeProgressHandlerFromDone()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done(null, null, $mock));
$adapter->notify(1);
}
/** @test */
public function notifyShouldThrowExceptionThrownProgressHandlerFromDone()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, null, function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->notify(1);
}
}

View file

@ -0,0 +1,351 @@
<?php
namespace React\Promise\PromiseTest;
trait PromiseFulfilledTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function fulfilledPromiseShouldBeImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$adapter->resolve(2);
$adapter->promise()
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function fulfilledPromiseShouldInvokeNewlyAddedCallback()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve(1);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock, $this->expectCallableNever());
}
/** @test */
public function thenShouldForwardResultWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$adapter->promise()
->then(
null,
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldForwardCallbackResultToNextCallback()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return $val + 1;
},
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldForwardPromisedCallbackResultValueToNextCallback()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return \React\Promise\resolve($val + 1);
},
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackReturnsARejection()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->resolve(1);
$adapter->promise()
->then(
function ($val) {
return \React\Promise\reject($val + 1);
},
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function thenShouldSwitchFromCallbacksToErrbacksWhenCallbackThrows()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->then(
$mock,
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock2
);
}
/** @test */
public function cancelShouldReturnNullForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->resolve();
$adapter->promise()->cancel();
}
/** @test */
public function doneShouldInvokeFulfillmentHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done($mock));
}
/** @test */
public function doneShouldThrowExceptionThrownFulfillmentHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done(function () {
throw new \Exception('UnhandledRejectionException');
}));
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenFulfillmentHandlerRejectsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->resolve(1);
$this->assertNull($adapter->promise()->done(function () {
return \React\Promise\reject();
}));
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->resolve(1);
$adapter->promise()->otherwise($this->expectCallableNever());
}
/** @test */
public function alwaysShouldNotSuppressValueForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {})
->then($mock);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromiseForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {
return 1;
})
->then($mock);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromiseForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->resolve($value);
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then($mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->resolve(1);
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace React\Promise\PromiseTest;
trait PromisePendingTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function thenShouldReturnAPromiseForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then());
}
/** @test */
public function thenShouldReturnAllowNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null));
}
/** @test */
public function cancelShouldReturnNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function doneShouldReturnNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function doneShouldReturnAllowNullForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done(null, null, null));
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$adapter->promise()->otherwise($this->expectCallableNever());
}
/** @test */
public function alwaysShouldReturnAPromiseForPendingPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {}));
}
}

View file

@ -0,0 +1,512 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise\Deferred;
use React\Promise\UnhandledRejectionException;
trait PromiseRejectedTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function rejectedPromiseShouldBeImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$adapter->reject(2);
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function rejectedPromiseShouldInvokeNewlyAddedCallback()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(1);
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
}
/** @test */
public function shouldForwardUndefinedRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(null);
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function () {
// Presence of rejection handler is enough to switch back
// to resolve mode, even though it returns undefined.
// The ONLY way to propagate a rejection is to re-throw or
// return a rejected promise;
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldSwitchFromErrbacksToCallbacksWhenErrbackDoesNotExplicitlyPropagate()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return $val + 1;
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldSwitchFromErrbacksToCallbacksWhenErrbackReturnsAResolution()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return \React\Promise\resolve($val + 1);
}
)
->then(
$mock,
$this->expectCallableNever()
);
}
/** @test */
public function shouldPropagateRejectionsWhenErrbackThrows()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->will($this->throwException($exception));
$mock2 = $this->createCallableMock();
$mock2
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
)
->then(
$this->expectCallableNever(),
$mock2
);
}
/** @test */
public function shouldPropagateRejectionsWhenErrbackReturnsARejection()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(2));
$adapter->reject(1);
$adapter->promise()
->then(
$this->expectCallableNever(),
function ($val) {
return \React\Promise\reject($val + 1);
}
)
->then(
$this->expectCallableNever(),
$mock
);
}
/** @test */
public function doneShouldInvokeRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, $mock));
}
/** @test */
public function doneShouldThrowExceptionThrownByRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
throw new \Exception('UnhandledRejectionException');
}));
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectedWithNonExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function unhandledRejectionExceptionThrownByDoneHoldsRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$expected = new \stdClass();
$adapter->reject($expected);
try {
$adapter->promise()->done();
} catch (UnhandledRejectionException $e) {
$this->assertSame($expected, $e->getReason());
return;
}
$this->fail();
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRejectsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject();
}));
}
/** @test */
public function doneShouldThrowRejectionExceptionWhenRejectionHandlerRejectsWithExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(1);
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject(new \Exception('UnhandledRejectionException'));
}));
}
/** @test */
public function doneShouldThrowExceptionProvidedAsRejectionValueForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$adapter->reject(new \Exception('UnhandledRejectionException'));
$this->assertNull($adapter->promise()->done());
}
/** @test */
public function doneShouldThrowWithDeepNestingPromiseChainsForRejectedPromise()
{
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$exception = new \Exception('UnhandledRejectionException');
$d = new Deferred();
$d->resolve();
$result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) {
$d = new Deferred();
$d->resolve();
return \React\Promise\resolve($d->promise()->then(function () {}))->then(
function () use ($exception) {
throw $exception;
}
);
})));
$result->done();
}
/** @test */
public function doneShouldRecoverWhenRejectionHandlerCatchesExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject(new \Exception('UnhandledRejectionException'));
$this->assertNull($adapter->promise()->done(null, function (\Exception $e) {
}));
}
/** @test */
public function otherwiseShouldInvokeRejectionHandlerForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->reject(1);
$adapter->promise()->otherwise($mock);
}
/** @test */
public function otherwiseShouldInvokeNonTypeHintedRejectionHandlerIfReasonIsAnExceptionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->otherwise(function ($reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function otherwiseShouldInvokeRejectionHandlerIfReasonMatchesTypehintForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \InvalidArgumentException();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->otherwise(function (\InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function otherwiseShouldNotInvokeRejectionHandlerIfReaonsDoesNotMatchTypehintForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->expectCallableNever();
$adapter->reject($exception);
$adapter->promise()
->otherwise(function (\InvalidArgumentException $reason) use ($mock) {
$mock($reason);
});
}
/** @test */
public function alwaysShouldNotSuppressRejectionForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {})
->then(null, $mock);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromiseForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {
return 1;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromiseForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->reject($exception);
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception1 = new \Exception();
$exception2 = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception2));
$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
throw $exception2;
})
->then(null, $mock);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception1 = new \Exception();
$exception2 = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception2));
$adapter->reject($exception1);
$adapter->promise()
->always(function () use ($exception2) {
return \React\Promise\reject($exception2);
})
->then(null, $mock);
}
/** @test */
public function cancelShouldReturnNullForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->reject();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->reject();
$adapter->promise()->cancel();
}
}

View file

@ -0,0 +1,86 @@
<?php
namespace React\Promise\PromiseTest;
trait PromiseSettledTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function thenShouldReturnAPromiseForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then());
}
/** @test */
public function thenShouldReturnAllowNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->then(null, null, null));
}
/** @test */
public function cancelShouldReturnNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->cancel());
}
/** @test */
public function cancelShouldHaveNoEffectForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
$adapter->settle();
$adapter->promise()->cancel();
}
/** @test */
public function doneShouldReturnNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->done(null, function () {}));
}
/** @test */
public function doneShouldReturnAllowNullForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertNull($adapter->promise()->done(null, function () {}, null));
}
/** @test */
public function progressShouldNotInvokeProgressHandlerForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$adapter->promise()->progress($this->expectCallableNever());
$adapter->notify();
}
/** @test */
public function alwaysShouldReturnAPromiseForSettledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$adapter->settle();
$this->assertInstanceOf('React\\Promise\\PromiseInterface', $adapter->promise()->always(function () {}));
}
}

View file

@ -0,0 +1,368 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
use React\Promise\Deferred;
trait RejectTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function rejectShouldRejectWithAnImmediateValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(1);
}
/** @test */
public function rejectShouldRejectWithFulfilledPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(Promise\resolve(1));
}
/** @test */
public function rejectShouldRejectWithRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->reject(Promise\reject(1));
}
/** @test */
public function rejectShouldForwardReasonWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(
$this->expectCallableNever()
)
->then(
$this->expectCallableNever(),
$mock
);
$adapter->reject(1);
}
/** @test */
public function rejectShouldMakePromiseImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(null, function ($value) use ($adapter) {
$adapter->reject(3);
return Promise\reject($value);
})
->then(
$this->expectCallableNever(),
$mock
);
$adapter->reject(1);
$adapter->reject(2);
}
/** @test */
public function notifyShouldInvokeOtherwiseHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->otherwise($mock);
$adapter->reject(1);
}
/** @test */
public function doneShouldInvokeRejectionHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done(null, $mock));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowExceptionThrownByRejectionHandler()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectedWithNonException()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done());
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRejects()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject();
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowRejectionExceptionWhenRejectionHandlerRejectsWithException()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(null, function () {
return \React\Promise\reject(new \Exception('UnhandledRejectionException'));
}));
$adapter->reject(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenRejectionHandlerRetunsPendingPromiseWhichRejectsLater()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$d = new Deferred();
$promise = $d->promise();
$this->assertNull($adapter->promise()->done(null, function () use ($promise) {
return $promise;
}));
$adapter->reject(1);
$d->reject(1);
}
/** @test */
public function doneShouldThrowExceptionProvidedAsRejectionValue()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done());
$adapter->reject(new \Exception('UnhandledRejectionException'));
}
/** @test */
public function doneShouldThrowWithDeepNestingPromiseChains()
{
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$exception = new \Exception('UnhandledRejectionException');
$d = new Deferred();
$result = \React\Promise\resolve(\React\Promise\resolve($d->promise()->then(function () use ($exception) {
$d = new Deferred();
$d->resolve();
return \React\Promise\resolve($d->promise()->then(function () {}))->then(
function () use ($exception) {
throw $exception;
}
);
})));
$result->done();
$d->resolve();
}
/** @test */
public function doneShouldRecoverWhenRejectionHandlerCatchesException()
{
$adapter = $this->getPromiseTestAdapter();
$this->assertNull($adapter->promise()->done(null, function (\Exception $e) {
}));
$adapter->reject(new \Exception('UnhandledRejectionException'));
}
/** @test */
public function alwaysShouldNotSuppressRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsANonPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {
return 1;
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldNotSuppressRejectionWhenHandlerReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
$adapter->reject($exception);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForRejection()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
$adapter->reject($exception);
}
}

View file

@ -0,0 +1,312 @@
<?php
namespace React\Promise\PromiseTest;
use React\Promise;
trait ResolveTestTrait
{
/**
* @return \React\Promise\PromiseAdapter\PromiseAdapterInterface
*/
abstract public function getPromiseTestAdapter(callable $canceller = null);
/** @test */
public function resolveShouldResolve()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock);
$adapter->resolve(1);
}
/** @test */
public function resolveShouldResolveWithPromisedValue()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($mock);
$adapter->resolve(Promise\resolve(1));
}
/** @test */
public function resolveShouldRejectWhenResolvedWithRejectedPromise()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then($this->expectCallableNever(), $mock);
$adapter->resolve(Promise\reject(1));
}
/** @test */
public function resolveShouldForwardValueWhenCallbackIsNull()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(
null,
$this->expectCallableNever()
)
->then(
$mock,
$this->expectCallableNever()
);
$adapter->resolve(1);
}
/** @test */
public function resolveShouldMakePromiseImmutable()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$adapter->promise()
->then(function ($value) use ($adapter) {
$adapter->resolve(3);
return $value;
})
->then(
$mock,
$this->expectCallableNever()
);
$adapter->resolve(1);
$adapter->resolve(2);
}
/**
* @test
*/
public function resolveShouldRejectWhenResolvedWithItself()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(new \LogicException('Cannot resolve a promise with itself.'));
$adapter->promise()
->then(
$this->expectCallableNever(),
$mock
);
$adapter->resolve($adapter->promise());
}
/**
* @test
*/
public function resolveShouldRejectWhenResolvedWithAPromiseWhichFollowsItself()
{
$adapter1 = $this->getPromiseTestAdapter();
$adapter2 = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with(new \LogicException('Cannot resolve a promise with itself.'));
$promise1 = $adapter1->promise();
$promise2 = $adapter2->promise();
$promise2->then(
$this->expectCallableNever(),
$mock
);
$adapter1->resolve($promise2);
$adapter2->resolve($promise1);
}
/** @test */
public function doneShouldInvokeFulfillmentHandler()
{
$adapter = $this->getPromiseTestAdapter();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(1));
$this->assertNull($adapter->promise()->done($mock));
$adapter->resolve(1);
}
/** @test */
public function doneShouldThrowExceptionThrownFulfillmentHandler()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('\Exception', 'UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(function () {
throw new \Exception('UnhandledRejectionException');
}));
$adapter->resolve(1);
}
/** @test */
public function doneShouldThrowUnhandledRejectionExceptionWhenFulfillmentHandlerRejects()
{
$adapter = $this->getPromiseTestAdapter();
$this->setExpectedException('React\\Promise\\UnhandledRejectionException');
$this->assertNull($adapter->promise()->done(function () {
return \React\Promise\reject();
}));
$adapter->resolve(1);
}
/** @test */
public function alwaysShouldNotSuppressValue()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsANonPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {
return 1;
})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldNotSuppressValueWhenHandlerReturnsAPromise()
{
$adapter = $this->getPromiseTestAdapter();
$value = new \stdClass();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($value));
$adapter->promise()
->always(function () {
return \React\Promise\resolve(1);
})
->then($mock);
$adapter->resolve($value);
}
/** @test */
public function alwaysShouldRejectWhenHandlerThrowsForFulfillment()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
throw $exception;
})
->then(null, $mock);
$adapter->resolve(1);
}
/** @test */
public function alwaysShouldRejectWhenHandlerRejectsForFulfillment()
{
$adapter = $this->getPromiseTestAdapter();
$exception = new \Exception();
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo($exception));
$adapter->promise()
->always(function () use ($exception) {
return \React\Promise\reject($exception);
})
->then(null, $mock);
$adapter->resolve(1);
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace React\Promise;
use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
class RejectedPromiseTest extends TestCase
{
use PromiseTest\PromiseSettledTestTrait,
PromiseTest\PromiseRejectedTestTrait;
public function getPromiseTestAdapter(callable $canceller = null)
{
$promise = null;
return new CallbackPromiseAdapter([
'promise' => function () use (&$promise) {
if (!$promise) {
throw new \LogicException('RejectedPromise must be rejected before obtaining the promise');
}
return $promise;
},
'resolve' => function () {
throw new \LogicException('You cannot call resolve() for React\Promise\RejectedPromise');
},
'reject' => function ($reason = null) use (&$promise) {
if (!$promise) {
$promise = new RejectedPromise($reason);
}
},
'notify' => function () {
// no-op
},
'settle' => function ($reason = null) use (&$promise) {
if (!$promise) {
$promise = new RejectedPromise($reason);
}
},
]);
}
/** @test */
public function shouldThrowExceptionIfConstructedWithAPromise()
{
$this->setExpectedException('\InvalidArgumentException');
return new RejectedPromise(new RejectedPromise());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithAlwaysFollowers()
{
gc_collect_cycles();
$promise = new RejectedPromise(1);
$promise->always(function () {
throw new \RuntimeException();
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
/** @test */
public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithThenFollowers()
{
gc_collect_cycles();
$promise = new RejectedPromise(1);
$promise = $promise->then(null, function () {
throw new \RuntimeException();
});
unset($promise);
$this->assertSame(0, gc_collect_cycles());
}
}

58
vendor/react/promise/tests/TestCase.php vendored Normal file
View file

@ -0,0 +1,58 @@
<?php
namespace React\Promise;
class TestCase extends \PHPUnit\Framework\TestCase
{
public function expectCallableExactly($amount)
{
$mock = $this->createCallableMock();
$mock
->expects($this->exactly($amount))
->method('__invoke');
return $mock;
}
public function expectCallableOnce()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');
return $mock;
}
public function expectCallableNever()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
return $mock;
}
public function createCallableMock()
{
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit 4
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace React\Promise;
class SimpleFulfilledTestPromise implements PromiseInterface
{
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
try {
if ($onFulfilled) {
$onFulfilled('foo');
}
return new self();
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace React\Promise;
class SimpleFulfilledTestThenable
{
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
try {
if ($onFulfilled) {
$onFulfilled('foo');
}
return new self();
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace React\Promise;
class SimpleRejectedTestPromise implements PromiseInterface
{
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
try {
if ($onRejected) {
$onRejected('foo');
}
return new self();
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace React\Promise;
class SimpleTestCancellable
{
public $cancelCalled = false;
public function cancel()
{
$this->cancelCalled = true;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace React\Promise;
class SimpleTestCancellableThenable
{
public $cancelCalled = false;
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return new self();
}
public function cancel()
{
$this->cancelCalled = true;
}
}