From a927b3cb063cebf5d222b6e44b3aac2842ce6dab Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Wed, 15 Mar 2017 11:12:49 -0500 Subject: [PATCH] Separate functions into Promise and Stream namespaces --- lib/Coroutine.php | 8 +- lib/Internal/Placeholder.php | 2 +- lib/Internal/Producer.php | 2 +- lib/LazyPromise.php | 2 +- lib/Loop/Driver.php | 2 +- lib/Loop/EvDriver.php | 2 +- lib/Loop/EventDriver.php | 2 +- lib/Loop/NativeDriver.php | 2 +- lib/Loop/UvDriver.php | 2 +- lib/functions.php | 1367 +++++++++-------- test/AdaptTest.php | 11 +- test/AllTest.php | 12 +- test/AnyTest.php | 16 +- test/CaptureTest.php | 15 +- test/ConcatTest.php | 11 +- test/CoroutineTest.php | 13 +- test/FilterTest.php | 9 +- test/FirstTest.php | 13 +- test/IntervalTest.php | 13 +- test/MergeTest.php | 11 +- test/PipeTest.php | 13 +- test/ProducerTest.php | 2 +- test/{MapTest.php => PromiseMapTest.php} | 29 +- test/RethrowTest.php | 10 +- test/SomeTest.php | 20 +- ...eamTest.php => StreamFromIterableTest.php} | 16 +- test/{EachTest.php => StreamMapTest.php} | 12 +- test/TimeoutTest.php | 17 +- test/WaitTest.php | 16 +- 29 files changed, 832 insertions(+), 818 deletions(-) rename test/{MapTest.php => PromiseMapTest.php} (85%) rename test/{StreamTest.php => StreamFromIterableTest.php} (80%) rename test/{EachTest.php => StreamMapTest.php} (89%) diff --git a/lib/Coroutine.php b/lib/Coroutine.php index 9e9719b..a44bfb6 100644 --- a/lib/Coroutine.php +++ b/lib/Coroutine.php @@ -65,7 +65,7 @@ final class Coroutine implements Promise { if (\is_array($yielded)) { try { - $yielded = all($yielded); + $yielded = Promise\all($yielded); } catch (UnionTypeError $e) { throw new InvalidYieldError( $this->generator, @@ -77,7 +77,7 @@ final class Coroutine implements Promise { ); } } else if ($yielded instanceof ReactPromise) { - $yielded = adapt($yielded); + $yielded = Promise\adapt($yielded); } else { throw new InvalidYieldError( $this->generator, @@ -109,7 +109,7 @@ final class Coroutine implements Promise { if (\is_array($yielded)) { try { - $yielded = all($yielded); + $yielded = Promise\all($yielded); } catch (UnionTypeError $e) { throw new InvalidYieldError( $this->generator, @@ -121,7 +121,7 @@ final class Coroutine implements Promise { ); } } else if ($yielded instanceof ReactPromise) { - $yielded = adapt($yielded); + $yielded = Promise\adapt($yielded); } else { throw new InvalidYieldError( $this->generator, diff --git a/lib/Internal/Placeholder.php b/lib/Internal/Placeholder.php index 0c22f7b..c24837e 100644 --- a/lib/Internal/Placeholder.php +++ b/lib/Internal/Placeholder.php @@ -6,7 +6,7 @@ use Amp\Failure; use Amp\Loop; use Amp\Promise; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\adapt; +use function Amp\Promise\adapt; /** * Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of diff --git a/lib/Internal/Producer.php b/lib/Internal/Producer.php index 7246f17..391df4c 100644 --- a/lib/Internal/Producer.php +++ b/lib/Internal/Producer.php @@ -7,7 +7,7 @@ use Amp\Loop; use Amp\Promise; use Amp\Success; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\adapt; +use function Amp\Promise\adapt; /** * Trait used by Stream implementations. Do not use this trait in your code, instead compose your class from one of diff --git a/lib/LazyPromise.php b/lib/LazyPromise.php index 9fde881..21afa3e 100644 --- a/lib/LazyPromise.php +++ b/lib/LazyPromise.php @@ -35,7 +35,7 @@ class LazyPromise implements Promise { $this->promise = $provider(); if ($this->promise instanceof ReactPromise) { - $this->promise = adapt($this->promise); + $this->promise = Promise\adapt($this->promise); } if (!$this->promise instanceof Promise) { diff --git a/lib/Loop/Driver.php b/lib/Loop/Driver.php index 380a8fd..a422202 100644 --- a/lib/Loop/Driver.php +++ b/lib/Loop/Driver.php @@ -6,7 +6,7 @@ use Amp\Coroutine; use Amp\Promise; use Amp\Internal\Watcher; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\rethrow; +use function Amp\Promise\rethrow; /** * Event loop driver which implements all basic operations to allow interoperability. diff --git a/lib/Loop/EvDriver.php b/lib/Loop/EvDriver.php index a57cb87..b14658e 100644 --- a/lib/Loop/EvDriver.php +++ b/lib/Loop/EvDriver.php @@ -6,7 +6,7 @@ use Amp\Coroutine; use Amp\Promise; use Amp\Internal\Watcher; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\rethrow; +use function Amp\Promise\rethrow; class EvDriver extends Driver { /** @var \EvSignal[]|null */ diff --git a/lib/Loop/EventDriver.php b/lib/Loop/EventDriver.php index c33d9f0..a325ebc 100644 --- a/lib/Loop/EventDriver.php +++ b/lib/Loop/EventDriver.php @@ -6,7 +6,7 @@ use Amp\Coroutine; use Amp\Promise; use Amp\Internal\Watcher; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\rethrow; +use function Amp\Promise\rethrow; class EventDriver extends Driver { /** @var \Event[]|null */ diff --git a/lib/Loop/NativeDriver.php b/lib/Loop/NativeDriver.php index 7ea02b5..078a269 100644 --- a/lib/Loop/NativeDriver.php +++ b/lib/Loop/NativeDriver.php @@ -6,7 +6,7 @@ use Amp\Coroutine; use Amp\Promise; use Amp\Internal\Watcher; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\rethrow; +use function Amp\Promise\rethrow; class NativeDriver extends Driver { /** @var resource[] */ diff --git a/lib/Loop/UvDriver.php b/lib/Loop/UvDriver.php index 2845511..12b90ba 100644 --- a/lib/Loop/UvDriver.php +++ b/lib/Loop/UvDriver.php @@ -6,7 +6,7 @@ use Amp\Coroutine; use Amp\Promise; use Amp\Internal\Watcher; use React\Promise\PromiseInterface as ReactPromise; -use function Amp\rethrow; +use function Amp\Promise\rethrow; class UvDriver extends Driver { /** @var resource A uv_loop resource created with uv_loop_new() */ diff --git a/lib/functions.php b/lib/functions.php index 9cefcd5..8f3672e 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -1,44 +1,76 @@ $arg) { + if (!$arg instanceof Promise) { + if ($arg instanceof ReactPromise) { + $args[$key] = Promise\adapt($arg); + } else { + $args[$key] = new Success($arg); + } + } + } + + if (1 === \count($args)) { + return Promise\pipe($args[0], $worker); + } + + return Promise\pipe(Promise\all($args), function (array $args) use ($worker) { + \ksort($args); // Needed to ensure correct argument order. + return $worker(...$args); + }); + }; + } } -/** - * Calls the given function, always returning a promise. If the function returns a Generator, it will be run as a - * coroutine. If the function throws, a failed promise will be returned. - * - * @param callable(mixed ...$args): mixed $functor - * @param array ...$args Arguments to pass to the function. - * - * @return \Amp\Promise - */ -function call(callable $functor, ...$args): Promise { - try { - $result = $functor(...$args); - } catch (\Throwable $exception) { - return new Failure($exception); - } +namespace Amp\Promise { - if ($result instanceof \Generator) { - return new Coroutine($result); - } + use Amp\Loop; + use Amp\Deferred; + use Amp\MultiReasonException; + use Amp\Promise; + use Amp\Success; + use Amp\TimeoutException; + use Amp\UnionTypeError; + use React\Promise\PromiseInterface as ReactPromise; - if ($result instanceof Promise) { - return $result; - } - - if ($result instanceof ReactPromise) { - return adapt($result); - } - - return new Success($result); -} - -/** - * Registers a callback that will forward the failure reason to the Loop error handler if the promise fails. - * - * @param \Amp\Promise|\React\Promise\PromiseInterface $promise - * - * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. - */ -function rethrow($promise) { - if (!$promise instanceof Promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } else { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + /** + * Registers a callback that will forward the failure reason to the Loop error handler if the promise fails. + * + * @param \Amp\Promise|\React\Promise\PromiseInterface $promise + * + * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. + */ + function rethrow($promise) { + if (!$promise instanceof Promise) { + if ($promise instanceof ReactPromise) { + $promise = Promise\adapt($promise); + } else { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } } + + $promise->when(function ($exception) { + if ($exception) { + throw $exception; + } + }); } - $promise->when(function ($exception) { + /** + * Runs the event loop until the promise is resolved. Should not be called within a running event loop. + * + * @param \Amp\Promise|\React\Promise\PromiseInterface $promise + * + * @return mixed Promise success value. + * + * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. + * @throws \Throwable Promise failure reason. + */ + function wait($promise) { + if (!$promise instanceof Promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } else { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + } + + $resolved = false; + Loop::run(function () use (&$resolved, &$value, &$exception, $promise) { + $promise->when(function ($e, $v) use (&$resolved, &$value, &$exception) { + Loop::stop(); + $resolved = true; + $exception = $e; + $value = $v; + }); + }); + + if (!$resolved) { + throw new \Error("Loop stopped without resolving promise"); + } + if ($exception) { throw $exception; } - }); -} -/** - * Runs the event loop until the promise is resolved. Should not be called within a running event loop. - * - * @param \Amp\Promise|\React\Promise\PromiseInterface $promise - * - * @return mixed Promise success value. - * - * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. - * @throws \Throwable Promise failure reason. - */ -function wait($promise) { - if (!$promise instanceof Promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } else { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } + return $value; } - $resolved = false; - Loop::run(function () use (&$resolved, &$value, &$exception, $promise) { - $promise->when(function ($e, $v) use (&$resolved, &$value, &$exception) { - Loop::stop(); - $resolved = true; - $exception = $e; - $value = $v; - }); - }); - - if (!$resolved) { - throw new \Error("Loop stopped without resolving promise"); - } - - if ($exception) { - throw $exception; - } - - return $value; -} - -/** - * Pipe the promised value through the specified functor once it resolves. - * - * @param \Amp\Promise|\React\Promise\PromiseInterface $promise - * @param callable(mixed $value): mixed $functor - * - * @return \Amp\Promise - * - * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. - */ -function pipe($promise, callable $functor): Promise { - if (!$promise instanceof Promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } else { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - } - - $deferred = new Deferred; - - $promise->when(function ($exception, $value) use ($deferred, $functor) { - if ($exception) { - $deferred->fail($exception); - return; - } - - try { - $deferred->resolve($functor($value)); - } catch (\Throwable $exception) { - $deferred->fail($exception); - } - }); - - return $deferred->promise(); -} - -/** - * @param \Amp\Promise|\React\Promise\PromiseInterface $promise - * @param string $className Throwable class name to capture. Given callback will only be invoked if the failure reason - * is an instance of the given throwable class name. - * @param callable(\Throwable $exception): mixed $functor - * - * @return \Amp\Promise - * - * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. - */ -function capture($promise, string $className, callable $functor): Promise { - if (!$promise instanceof Promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } else { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - } - - $deferred = new Deferred; - - $promise->when(function ($exception, $value) use ($deferred, $className, $functor) { - if (!$exception) { - $deferred->resolve($value); - return; - } - - if (!$exception instanceof $className) { - $deferred->fail($exception); - return; - } - - try { - $deferred->resolve($functor($exception)); - } catch (\Throwable $exception) { - $deferred->fail($exception); - } - }); - - return $deferred->promise(); -} - -/** - * Create an artificial timeout for any Promise. - * - * If the timeout expires before the promise is resolved, the returned promise fails with an instance of - * \Amp\TimeoutException. - * - * @param \Amp\Promise|\React\Promise\PromiseInterface $promise - * @param int $timeout Timeout in milliseconds. - * - * @return \Amp\Promise - * - * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. - */ -function timeout($promise, int $timeout): Promise { - if (!$promise instanceof Promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } else { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - } - - $deferred = new Deferred; - $resolved = false; - - $watcher = Loop::delay($timeout, function () use (&$resolved, $deferred) { - if (!$resolved) { - $resolved = true; - $deferred->fail(new TimeoutException); - } - }); - Loop::unreference($watcher); - - $promise->when(function () use (&$resolved, $promise, $deferred, $watcher) { - Loop::cancel($watcher); - - if ($resolved) { - return; - } - - $resolved = true; - $deferred->resolve($promise); - }); - - return $deferred->promise(); -} - -/** - * Adapts any object with a done(callable $onFulfilled, callable $onRejected) or then(callable $onFulfilled, - * callable $onRejected) method to a promise usable by components depending on placeholders implementing - * \AsyncInterop\Promise. - * - * @param object $promise Object with a done() or then() method. - * - * @return \Amp\Promise Promise resolved by the $thenable object. - * - * @throws \Error If the provided object does not have a then() method. - */ -function adapt($promise): Promise { - $deferred = new Deferred; - - if (\method_exists($promise, 'done')) { - $promise->done([$deferred, 'resolve'], [$deferred, 'fail']); - } elseif (\method_exists($promise, 'then')) { - $promise->then([$deferred, 'resolve'], [$deferred, 'fail']); - } else { - throw new \Error("Object must have a 'then' or 'done' method"); - } - - return $deferred->promise(); -} - -/** - * Wraps the given callable $worker in a promise aware function that has the same number of arguments as $worker, - * but those arguments may be promises for the future argument value or just values. The returned function will - * return a promise for the return value of $worker and will never throw. The $worker function will not be called - * until each promise given as an argument is fulfilled. If any promise provided as an argument fails, the - * promise returned by the returned function will be failed for the same reason. The promise succeeds with - * the return value of $worker or failed if $worker throws. - * - * @param callable $worker - * - * @return callable - */ -function lift(callable $worker): callable { /** - * @param mixed ...$args Promises or values. + * Pipe the promised value through the specified functor once it resolves. + * + * @param \Amp\Promise|\React\Promise\PromiseInterface $promise + * @param callable (mixed $value): mixed $functor * * @return \Amp\Promise + * + * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. */ - return function (...$args) use ($worker): Promise { - foreach ($args as $key => $arg) { - if (!$arg instanceof Promise) { - if ($arg instanceof ReactPromise) { - $args[$key] = adapt($arg); - } else { - $args[$key] = new Success($arg); - } - } - } - - if (1 === \count($args)) { - return pipe($args[0], $worker); - } - - return pipe(all($args), function (array $args) use ($worker) { - \ksort($args); // Needed to ensure correct argument order. - return $worker(...$args); - }); - }; -} - -/** - * Returns a promise that is resolved when all promises are resolved. The returned promise will not fail. - * Returned promise succeeds with a two-item array delineating successful and failed promise results, - * with keys identical and corresponding to the original given array. - * - * This function is the same as some() with the notable exception that it will never fail even - * if all promises in the array resolve unsuccessfully. - * - * @param Promise[] $promises - * - * @return \Amp\Promise - * - * @throws \Error If a non-Promise is in the array. - */ -function any(array $promises): Promise { - if (empty($promises)) { - return new Success([[], []]); - } - - $deferred = new Deferred; - - $pending = \count($promises); - $errors = []; - $values = []; - - foreach ($promises as $key => $promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } elseif (!$promise instanceof Promise) { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - - $promise->when(function ($error, $value) use (&$pending, &$errors, &$values, $key, $deferred) { - if ($error) { - $errors[$key] = $error; + function pipe($promise, callable $functor): Promise { + if (!$promise instanceof Promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); } else { - $values[$key] = $value; + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); } - - if (--$pending === 0) { - $deferred->resolve([$errors, $values]); - } - }); - } - return $deferred->promise(); -} - -/** - * Returns a promise that succeeds when all promises succeed, and fails if any promise fails. Returned - * promise succeeds with an array of values used to succeed each contained promise, with keys corresponding to - * the array of promises. - * - * @param Promise[] $promises - * - * @return \Amp\Promise - * - * @throws \Error If a non-Promise is in the array. - */ -function all(array $promises): Promise { - if (empty($promises)) { - return new Success([]); - } - - $deferred = new Deferred; - - $pending = \count($promises); - $resolved = false; - $values = []; - - foreach ($promises as $key => $promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } elseif (!$promise instanceof Promise) { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); } - $promise->when(function ($exception, $value) use (&$values, &$pending, &$resolved, $key, $deferred) { - if ($resolved) { - return; - } + $deferred = new Deferred; + $promise->when(function ($exception, $value) use ($deferred, $functor) { if ($exception) { - $resolved = true; $deferred->fail($exception); return; } - $values[$key] = $value; - if (0 === --$pending) { - $deferred->resolve($values); + try { + $deferred->resolve($functor($value)); + } catch (\Throwable $exception) { + $deferred->fail($exception); } }); + + return $deferred->promise(); } - return $deferred->promise(); -} - -/** - * Returns a promise that succeeds when the first promise succeeds, and fails only if all promises fail. - * - * @param Promise[] $promises - * - * @return \Amp\Promise - * - * @throws \Error If the array is empty or a non-Promise is in the array. - */ -function first(array $promises): Promise { - if (empty($promises)) { - throw new \Error("No promises provided"); - } - - $deferred = new Deferred; - - $pending = \count($promises); - $resolved = false; - $exceptions = []; - - foreach ($promises as $key => $promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } elseif (!$promise instanceof Promise) { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + /** + * @param \Amp\Promise|\React\Promise\PromiseInterface $promise + * @param string $className Throwable class name to capture. Given callback will only be invoked if the failure reason + * is an instance of the given throwable class name. + * @param callable (\Throwable $exception): mixed $functor + * + * @return \Amp\Promise + * + * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. + */ + function capture($promise, string $className, callable $functor): Promise { + if (!$promise instanceof Promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } else { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } } - $promise->when(function ($exception, $value) use (&$exceptions, &$pending, &$resolved, $key, $deferred) { - if ($resolved) { - return; - } + $deferred = new Deferred; + $promise->when(function ($exception, $value) use ($deferred, $className, $functor) { if (!$exception) { - $resolved = true; $deferred->resolve($value); return; } - $exceptions[$key] = $exception; - if (0 === --$pending) { - $deferred->fail(new MultiReasonException($exceptions)); - } - }); - } - - return $deferred->promise(); -} - -/** - * Resolves with a two-item array delineating successful and failed Promise results. - * - * The returned promise will only fail if ALL of the promises fail. - * - * @param Promise[] $promises - * - * @return \Amp\Promise - */ -function some(array $promises): Promise { - if (empty($promises)) { - throw new \Error("No promises provided"); - } - - $pending = \count($promises); - - $deferred = new Deferred; - $values = []; - $exceptions = []; - - foreach ($promises as $key => $promise) { - if ($promise instanceof ReactPromise) { - $promise = adapt($promise); - } elseif (!$promise instanceof Promise) { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - - $promise->when(function ($exception, $value) use (&$values, &$exceptions, &$pending, $key, $deferred) { - if ($exception) { - $exceptions[$key] = $exception; - } else { - $values[$key] = $value; - } - - if (0 === --$pending) { - if (empty($values)) { - $deferred->fail(new MultiReasonException($exceptions)); - return; - } - - $deferred->resolve([$exceptions, $values]); - } - }); - } - - return $deferred->promise(); -} - -/** - * Maps the callback to each promise as it succeeds. Returns an array of promises resolved by the return - * callback value of the callback function. The callback may return promises or throw exceptions to fail - * promises in the array. If a promise in the passed array fails, the callback will not be called and the - * promise in the array fails for the same reason. Tip: Use all() or any() to determine when all - * promises in the array have been resolved. - * - * @param callable(mixed $value): mixed $callback - * @param Promise[] ...$promises - * - * @return \Amp\Promise[] Array of promises resolved with the result of the mapped function. - */ -function map(callable $callback, array ...$promises): array { - foreach ($promises as $promiseSet) { - foreach ($promiseSet as $promise) { - if (!$promise instanceof Promise && !$promise instanceof ReactPromise) { - throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); - } - } - } - - return \array_map(lift($callback), ...$promises); -} - -/** - * Creates a stream from the given iterable, emitting the each value. The iterable may contain promises. If any promise - * fails, the stream will fail with the same reason. - * - * @param array|\Traversable $iterable - * - * @return \Amp\Stream - * - * @throws \TypeError If the argument is not an array or instance of \Traversable. - */ -function stream(/* iterable */ $iterable): Stream { - if (!$iterable instanceof \Traversable && !\is_array($iterable)) { - throw new UnionTypeError(["array", "Traversable"], $iterable); - } - - return new Producer(function (callable $emit) use ($iterable) { - foreach ($iterable as $value) { - yield $emit($value); - } - }); -} - -/** - * @param \Amp\Stream $stream - * @param callable(mixed $value): mixed $onNext - * @param callable(mixed $value): mixed|null $onComplete - * - * @return \Amp\Stream - */ -function each(Stream $stream, callable $onNext, callable $onComplete = null): Stream { - $listener = new Listener($stream); - return new Producer(function (callable $emit) use ($listener, $onNext, $onComplete) { - while (yield $listener->advance()) { - yield $emit($onNext($listener->getCurrent())); - } - if ($onComplete === null) { - return $listener->getResult(); - } - return $onComplete($listener->getResult()); - }); -} - -/** - * @param \Amp\Stream $stream - * @param callable(mixed $value): bool $filter - * - * @return \Amp\Stream - */ -function filter(Stream $stream, callable $filter): Stream { - $listener = new Listener($stream); - return new Producer(function (callable $emit) use ($listener, $filter) { - while (yield $listener->advance()) { - if ($filter($listener->getCurrent())) { - yield $emit($listener->getCurrent()); - } - } - return $listener->getResult(); - }); -} - -/** - * Creates a stream that emits values emitted from any stream in the array of streams. - * - * @param \Amp\Stream[] $streams - * - * @return \Amp\Stream - */ -function merge(array $streams): Stream { - $emitter = new Emitter; - $pending = true; - - foreach ($streams as $stream) { - if (!$stream instanceof Stream) { - throw new UnionTypeError([Stream::class], $stream); - } - $stream->listen(function ($value) use (&$pending, $emitter) { - if ($pending) { - return $emitter->emit($value); - } - return null; - }); - } - - all($streams)->when(function ($exception, array $values = null) use (&$pending, $emitter) { - $pending = false; - - if ($exception) { - $emitter->fail($exception); - return; - } - - $emitter->resolve($values); - }); - - return $emitter->stream(); -} - -/** - * Concatenates the given streams into a single stream, emitting values from a single stream at a time. The - * prior stream must complete before values are emitted from any subsequent stream. Streams are concatenated - * in the order given (iteration order of the array). - * - * @param array $streams - * - * @return \Amp\Stream - */ -function concat(array $streams): Stream { - foreach ($streams as $stream) { - if (!$stream instanceof Stream) { - throw new UnionTypeError([Stream::class], $stream); - } - } - - $emitter = new Emitter; - $subscriptions = []; - $previous = []; - $promise = all($previous); - - foreach ($streams as $stream) { - $generator = function ($value) use ($emitter, $promise) { - static $pending = true, $failed = false; - - if ($failed) { + if (!$exception instanceof $className) { + $deferred->fail($exception); return; } - if ($pending) { - try { - yield $promise; - $pending = false; - } catch (\Throwable $exception) { - $failed = true; - return; // Prior stream failed. - } + try { + $deferred->resolve($functor($exception)); + } catch (\Throwable $exception) { + $deferred->fail($exception); + } + }); + + return $deferred->promise(); + } + + /** + * Create an artificial timeout for any Promise. + * + * If the timeout expires before the promise is resolved, the returned promise fails with an instance of + * \Amp\TimeoutException. + * + * @param \Amp\Promise|\React\Promise\PromiseInterface $promise + * @param int $timeout Timeout in milliseconds. + * + * @return \Amp\Promise + * + * @throws \TypeError If $promise is not an instance of \Amp\Promise or \React\Promise\PromiseInterface. + */ + function timeout($promise, int $timeout): Promise { + if (!$promise instanceof Promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } else { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + } + + $deferred = new Deferred; + $resolved = false; + + $watcher = Loop::delay($timeout, function () use (&$resolved, $deferred) { + if (!$resolved) { + $resolved = true; + $deferred->fail(new TimeoutException); + } + }); + Loop::unreference($watcher); + + $promise->when(function () use (&$resolved, $promise, $deferred, $watcher) { + Loop::cancel($watcher); + + if ($resolved) { + return; } - yield $emitter->emit($value); - }; - $subscriptions[] = $stream->listen(function ($value) use ($generator) { - return new Coroutine($generator($value)); + $resolved = true; + $deferred->resolve($promise); }); - $previous[] = $stream; - $promise = all($previous); + + return $deferred->promise(); } - $promise->when(function ($exception, array $values = null) use ($emitter) { - if ($exception) { - $emitter->fail($exception); - return; + /** + * Adapts any object with a done(callable $onFulfilled, callable $onRejected) or then(callable $onFulfilled, + * callable $onRejected) method to a promise usable by components depending on placeholders implementing + * \AsyncInterop\Promise. + * + * @param object $promise Object with a done() or then() method. + * + * @return \Amp\Promise Promise resolved by the $thenable object. + * + * @throws \Error If the provided object does not have a then() method. + */ + function adapt($promise): Promise { + $deferred = new Deferred; + + if (\method_exists($promise, 'done')) { + $promise->done([$deferred, 'resolve'], [$deferred, 'fail']); + } elseif (\method_exists($promise, 'then')) { + $promise->then([$deferred, 'resolve'], [$deferred, 'fail']); + } else { + throw new \Error("Object must have a 'then' or 'done' method"); } - $emitter->resolve($values); - }); - - return $emitter->stream(); -} - -/** - * Returns a stream that emits a value every $interval milliseconds after (up to $count times). The value emitted - * is an integer of the number of times the stream emitted a value. - * - * @param int $interval Time interval between emitted values in milliseconds. - * @param int $count Number of values to emit. PHP_INT_MAX by default. - * - * @return \Amp\Stream - * - * @throws \Error If the number of times to emit is not a positive value. - */ -function interval(int $interval, int $count = PHP_INT_MAX): Stream { - if (0 >= $count) { - throw new \Error("The number of times to emit must be a positive value"); + return $deferred->promise(); } - $emitter = new Emitter; - - Loop::repeat($interval, function ($watcher) use (&$i, $emitter, $count) { - $emitter->emit(++$i); - - if ($i === $count) { - Loop::cancel($watcher); - $emitter->resolve(); + /** + * Returns a promise that is resolved when all promises are resolved. The returned promise will not fail. + * Returned promise succeeds with a two-item array delineating successful and failed promise results, + * with keys identical and corresponding to the original given array. + * + * This function is the same as some() with the notable exception that it will never fail even + * if all promises in the array resolve unsuccessfully. + * + * @param Promise[] $promises + * + * @return \Amp\Promise + * + * @throws \Error If a non-Promise is in the array. + */ + function any(array $promises): Promise { + if (empty($promises)) { + return new Success([[], []]); } - }); - return $emitter->stream(); + $deferred = new Deferred; + + $pending = \count($promises); + $errors = []; + $values = []; + + foreach ($promises as $key => $promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } elseif (!$promise instanceof Promise) { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + + $promise->when(function ($error, $value) use (&$pending, &$errors, &$values, $key, $deferred) { + if ($error) { + $errors[$key] = $error; + } else { + $values[$key] = $value; + } + + if (--$pending === 0) { + $deferred->resolve([$errors, $values]); + } + }); + } + return $deferred->promise(); + } + + /** + * Returns a promise that succeeds when all promises succeed, and fails if any promise fails. Returned + * promise succeeds with an array of values used to succeed each contained promise, with keys corresponding to + * the array of promises. + * + * @param Promise[] $promises + * + * @return \Amp\Promise + * + * @throws \Error If a non-Promise is in the array. + */ + function all(array $promises): Promise { + if (empty($promises)) { + return new Success([]); + } + + $deferred = new Deferred; + + $pending = \count($promises); + $resolved = false; + $values = []; + + foreach ($promises as $key => $promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } elseif (!$promise instanceof Promise) { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + + $promise->when(function ($exception, $value) use (&$values, &$pending, &$resolved, $key, $deferred) { + if ($resolved) { + return; + } + + if ($exception) { + $resolved = true; + $deferred->fail($exception); + return; + } + + $values[$key] = $value; + if (0 === --$pending) { + $deferred->resolve($values); + } + }); + } + + return $deferred->promise(); + } + + /** + * Returns a promise that succeeds when the first promise succeeds, and fails only if all promises fail. + * + * @param Promise[] $promises + * + * @return \Amp\Promise + * + * @throws \Error If the array is empty or a non-Promise is in the array. + */ + function first(array $promises): Promise { + if (empty($promises)) { + throw new \Error("No promises provided"); + } + + $deferred = new Deferred; + + $pending = \count($promises); + $resolved = false; + $exceptions = []; + + foreach ($promises as $key => $promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } elseif (!$promise instanceof Promise) { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + + $promise->when(function ($exception, $value) use (&$exceptions, &$pending, &$resolved, $key, $deferred) { + if ($resolved) { + return; + } + + if (!$exception) { + $resolved = true; + $deferred->resolve($value); + return; + } + + $exceptions[$key] = $exception; + if (0 === --$pending) { + $deferred->fail(new MultiReasonException($exceptions)); + } + }); + } + + return $deferred->promise(); + } + + /** + * Resolves with a two-item array delineating successful and failed Promise results. + * + * The returned promise will only fail if ALL of the promises fail. + * + * @param Promise[] $promises + * + * @return \Amp\Promise + */ + function some(array $promises): Promise { + if (empty($promises)) { + return new Success([[], []]); + } + + $pending = \count($promises); + + $deferred = new Deferred; + $values = []; + $exceptions = []; + + foreach ($promises as $key => $promise) { + if ($promise instanceof ReactPromise) { + $promise = adapt($promise); + } elseif (!$promise instanceof Promise) { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + + $promise->when(function ($exception, $value) use (&$values, &$exceptions, &$pending, $key, $deferred) { + if ($exception) { + $exceptions[$key] = $exception; + } else { + $values[$key] = $value; + } + + if (0 === --$pending) { + if (empty($values)) { + $deferred->fail(new MultiReasonException($exceptions)); + return; + } + + $deferred->resolve([$exceptions, $values]); + } + }); + } + + return $deferred->promise(); + } + + /** + * Maps the callback to each promise as it succeeds. Returns an array of promises resolved by the return + * callback value of the callback function. The callback may return promises or throw exceptions to fail + * promises in the array. If a promise in the passed array fails, the callback will not be called and the + * promise in the array fails for the same reason. Tip: Use all() or any() to determine when all + * promises in the array have been resolved. + * + * @param callable (mixed $value): mixed $callback + * @param Promise[] ...$promises + * + * @return \Amp\Promise[] Array of promises resolved with the result of the mapped function. + */ + function map(callable $callback, array ...$promises): array { + foreach ($promises as $promiseSet) { + foreach ($promiseSet as $promise) { + if (!$promise instanceof Promise && !$promise instanceof ReactPromise) { + throw new UnionTypeError([Promise::class, ReactPromise::class], $promise); + } + } + } + + return \array_map(\Amp\lift($callback), ...$promises); + } +} + +namespace Amp\Stream { + + use Amp\Coroutine; + use Amp\Emitter; + use Amp\Listener; + use Amp\Loop; + use Amp\Producer; + use Amp\Promise; + use Amp\Stream; + use Amp\UnionTypeError; + + /** + * Creates a stream from the given iterable, emitting the each value. The iterable may contain promises. If any promise + * fails, the stream will fail with the same reason. + * + * @param array|\Traversable $iterable + * + * @return \Amp\Stream + * + * @throws \TypeError If the argument is not an array or instance of \Traversable. + */ + function fromIterable(/* iterable */ $iterable): Stream { + if (!$iterable instanceof \Traversable && !\is_array($iterable)) { + throw new UnionTypeError(["array", "Traversable"], $iterable); + } + + return new Producer(function (callable $emit) use ($iterable) { + foreach ($iterable as $value) { + yield $emit($value); + } + }); + } + + /** + * @param \Amp\Stream $stream + * @param callable (mixed $value): mixed $onNext + * @param callable (mixed $value): mixed|null $onComplete + * + * @return \Amp\Stream + */ + function map(Stream $stream, callable $onNext, callable $onComplete = null): Stream { + $listener = new Listener($stream); + return new Producer(function (callable $emit) use ($listener, $onNext, $onComplete) { + while (yield $listener->advance()) { + yield $emit($onNext($listener->getCurrent())); + } + if ($onComplete === null) { + return $listener->getResult(); + } + return $onComplete($listener->getResult()); + }); + } + + /** + * @param \Amp\Stream $stream + * @param callable (mixed $value): bool $filter + * + * @return \Amp\Stream + */ + function filter(Stream $stream, callable $filter): Stream { + $listener = new Listener($stream); + return new Producer(function (callable $emit) use ($listener, $filter) { + while (yield $listener->advance()) { + if ($filter($listener->getCurrent())) { + yield $emit($listener->getCurrent()); + } + } + return $listener->getResult(); + }); + } + + /** + * Creates a stream that emits values emitted from any stream in the array of streams. + * + * @param \Amp\Stream[] $streams + * + * @return \Amp\Stream + */ + function merge(array $streams): Stream { + $emitter = new Emitter; + $pending = true; + + foreach ($streams as $stream) { + if (!$stream instanceof Stream) { + throw new UnionTypeError([Stream::class], $stream); + } + $stream->listen(function ($value) use (&$pending, $emitter) { + if ($pending) { + return $emitter->emit($value); + } + return null; + }); + } + + Promise\all($streams)->when(function ($exception, array $values = null) use (&$pending, $emitter) { + $pending = false; + + if ($exception) { + $emitter->fail($exception); + return; + } + + $emitter->resolve($values); + }); + + return $emitter->stream(); + } + + /** + * Concatenates the given streams into a single stream, emitting values from a single stream at a time. The + * prior stream must complete before values are emitted from any subsequent stream. Streams are concatenated + * in the order given (iteration order of the array). + * + * @param array $streams + * + * @return \Amp\Stream + */ + function concat(array $streams): Stream { + foreach ($streams as $stream) { + if (!$stream instanceof Stream) { + throw new UnionTypeError([Stream::class], $stream); + } + } + + $emitter = new Emitter; + $subscriptions = []; + $previous = []; + $promise = Promise\all($previous); + + foreach ($streams as $stream) { + $generator = function ($value) use ($emitter, $promise) { + static $pending = true, $failed = false; + + if ($failed) { + return; + } + + if ($pending) { + try { + yield $promise; + $pending = false; + } catch (\Throwable $exception) { + $failed = true; + return; // Prior stream failed. + } + } + + yield $emitter->emit($value); + }; + $subscriptions[] = $stream->listen(function ($value) use ($generator) { + return new Coroutine($generator($value)); + }); + $previous[] = $stream; + $promise = Promise\all($previous); + } + + $promise->when(function ($exception, array $values = null) use ($emitter) { + if ($exception) { + $emitter->fail($exception); + return; + } + + $emitter->resolve($values); + }); + + return $emitter->stream(); + } + + /** + * Returns a stream that emits a value every $interval milliseconds after (up to $count times). The value emitted + * is an integer of the number of times the stream emitted a value. + * + * @param int $interval Time interval between emitted values in milliseconds. + * @param int $count Number of values to emit. PHP_INT_MAX by default. + * + * @return \Amp\Stream + * + * @throws \Error If the number of times to emit is not a positive value. + */ + function interval(int $interval, int $count = PHP_INT_MAX): Stream { + if (0 >= $count) { + throw new \Error("The number of times to emit must be a positive value"); + } + + $emitter = new Emitter; + + Loop::repeat($interval, function ($watcher) use (&$i, $emitter, $count) { + $emitter->emit(++$i); + + if ($i === $count) { + Loop::cancel($watcher); + $emitter->resolve(); + } + }); + + return $emitter->stream(); + } } diff --git a/test/AdaptTest.php b/test/AdaptTest.php index 7b2705e..cb9306d 100644 --- a/test/AdaptTest.php +++ b/test/AdaptTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Success; use Amp\Promise; @@ -48,7 +47,7 @@ class AdaptTest extends \PHPUnit\Framework\TestCase { }) ); - $promise = Amp\adapt($mock); + $promise = Promise\adapt($mock); $this->assertInstanceOf(Promise::class, $promise); } @@ -61,7 +60,7 @@ class AdaptTest extends \PHPUnit\Framework\TestCase { $promise = new PromiseMock(new Success($value)); - $promise = Amp\adapt($promise); + $promise = Promise\adapt($promise); $promise->when(function ($exception, $value) use (&$result) { $result = $value; @@ -78,7 +77,7 @@ class AdaptTest extends \PHPUnit\Framework\TestCase { $promise = new PromiseMock(new Failure($exception)); - $promise = Amp\adapt($promise); + $promise = Promise\adapt($promise); $promise->when(function ($exception, $value) use (&$reason) { $reason = $exception; @@ -91,13 +90,13 @@ class AdaptTest extends \PHPUnit\Framework\TestCase { * @expectedException \Error */ public function testScalarValue() { - Amp\adapt(1); + Promise\adapt(1); } /** * @expectedException \Error */ public function testNonThenableObject() { - Amp\adapt(new \stdClass); + Promise\adapt(new \stdClass); } } diff --git a/test/AllTest.php b/test/AllTest.php index 0d4d908..a1e9679 100644 --- a/test/AllTest.php +++ b/test/AllTest.php @@ -2,8 +2,8 @@ namespace Amp\Test; -use Amp; use Amp\Pause; +use Amp\Promise; use Amp\Success; use Amp\Loop; @@ -13,7 +13,7 @@ class AllTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\all([])->when($callback); + Promise\all([])->when($callback); $this->assertSame([], $result); } @@ -25,7 +25,7 @@ class AllTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\all($promises)->when($callback); + Promise\all($promises)->when($callback); $this->assertSame([1, 2, 3], $result); } @@ -42,7 +42,7 @@ class AllTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\all($promises)->when($callback); + Promise\all($promises)->when($callback); }); $this->assertEquals([1, 2, 3], $result); @@ -62,7 +62,7 @@ class AllTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\all($promises)->when($callback); + Promise\all($promises)->when($callback); }); $this->assertEquals($expected, $result); @@ -72,6 +72,6 @@ class AllTest extends \PHPUnit\Framework\TestCase { * @expectedException \Amp\UnionTypeError */ public function testNonPromise() { - Amp\all([1]); + Promise\all([1]); } } diff --git a/test/AnyTest.php b/test/AnyTest.php index 7de7057..70bf9f0 100644 --- a/test/AnyTest.php +++ b/test/AnyTest.php @@ -2,9 +2,9 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Pause; +use Amp\Promise; use Amp\Success; use Amp\Loop; @@ -14,7 +14,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any([])->when($callback); + Promise\any([])->when($callback); $this->assertSame([[], []], $result); } @@ -26,7 +26,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any($promises)->when($callback); + Promise\any($promises)->when($callback); $this->assertEquals([[], [1, 2, 3]], $result); } @@ -39,7 +39,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any($promises)->when($callback); + Promise\any($promises)->when($callback); $this->assertEquals([[$exception, $exception, $exception], []], $result); } @@ -52,7 +52,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any($promises)->when($callback); + Promise\any($promises)->when($callback); $this->assertEquals([[1 => $exception], [0 => 1, 2 => 3]], $result); } @@ -69,7 +69,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any($promises)->when($callback); + Promise\any($promises)->when($callback); }); $this->assertEquals([[], [1, 2, 3]], $result); @@ -93,7 +93,7 @@ class AnyTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\any($promises)->when($callback); + Promise\any($promises)->when($callback); }); $this->assertEquals($expected, $result); @@ -103,6 +103,6 @@ class AnyTest extends \PHPUnit\Framework\TestCase { * @expectedException \Amp\UnionTypeError */ public function testNonPromise() { - Amp\any([1]); + Promise\any([1]); } } diff --git a/test/CaptureTest.php b/test/CaptureTest.php index 52f259a..bc312eb 100644 --- a/test/CaptureTest.php +++ b/test/CaptureTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Success; use Amp\Promise; @@ -20,7 +19,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { $promise = new Success($value); - $promise = Amp\capture($promise, \Exception::class, $callback); + $promise = Promise\capture($promise, \Exception::class, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -45,7 +44,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { $promise = new Failure($exception); - $promise = Amp\capture($promise, \Exception::class, $callback); + $promise = Promise\capture($promise, \Exception::class, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -73,7 +72,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { $promise = new Failure($exception); - $promise = Amp\capture($promise, \Exception::class, $callback); + $promise = Promise\capture($promise, \Exception::class, $callback); $callback = function ($exception, $value) use (&$reason) { $reason = $exception; @@ -100,7 +99,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { $promise = new Failure($exception); - $promise = Amp\capture($promise, \RuntimeException::class, $callback); + $promise = Promise\capture($promise, \RuntimeException::class, $callback); $callback = function ($exception, $value) use (&$reason) { $reason = $exception; @@ -127,7 +126,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { $promise = reject($exception); - $promise = Amp\capture($promise, \Exception::class, $callback); + $promise = Promise\capture($promise, \Exception::class, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -142,7 +141,7 @@ class CaptureTest extends \PHPUnit\Framework\TestCase { } public function testNonPromise() { - $this->expectException(Amp\UnionTypeError::class); - Amp\capture(42, \Error::class, function () {}); + $this->expectException(\Amp\UnionTypeError::class); + Promise\capture(42, \Error::class, function () {}); } } diff --git a/test/ConcatTest.php b/test/ConcatTest.php index e7f2a6e..3293940 100644 --- a/test/ConcatTest.php +++ b/test/ConcatTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Loop; use Amp\Producer; use Amp\Stream; @@ -24,12 +23,12 @@ class ConcatTest extends \PHPUnit\Framework\TestCase { */ public function testConcat(array $streams, array $expected) { $streams = \array_map(function (array $stream): Stream { - return Amp\stream($stream); + return Stream\fromIterable($stream); }, $streams); - $stream = Amp\concat($streams); + $stream = Stream\concat($streams); - Amp\each($stream, function ($value) use ($expected) { + Stream\map($stream, function ($value) use ($expected) { static $i = 0; $this->assertSame($expected[$i++], $value); }); @@ -49,7 +48,7 @@ class ConcatTest extends \PHPUnit\Framework\TestCase { throw $exception; }); - $stream = Amp\concat([Amp\stream(\range(1, 5)), $producer, Amp\stream(\range(7, 10))]); + $stream = Stream\concat([Stream\fromIterable(\range(1, 5)), $producer, Stream\fromIterable(\range(7, 10))]); $stream->listen(function ($value) use (&$results) { $results[] = $value; @@ -70,6 +69,6 @@ class ConcatTest extends \PHPUnit\Framework\TestCase { * @expectedException \Amp\UnionTypeError */ public function testNonStream() { - Amp\concat([1]); + Stream\concat([1]); } } diff --git a/test/CoroutineTest.php b/test/CoroutineTest.php index 78aacf0..175f2ef 100644 --- a/test/CoroutineTest.php +++ b/test/CoroutineTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Coroutine; use Amp\Failure; use Amp\InvalidYieldError; @@ -504,7 +503,7 @@ class CoroutineTest extends TestCase { } public function testCoroutineFunction() { - $callable = Amp\coroutine(function () { + $callable = \Amp\coroutine(function () { yield; }); @@ -517,7 +516,7 @@ class CoroutineTest extends TestCase { public function testCoroutineFunctionWithCallbackReturningPromise() { $value = 1; $promise = new Success($value); - $callable = Amp\coroutine(function ($value) { + $callable = \Amp\coroutine(function ($value) { return $value; }); @@ -540,7 +539,7 @@ class CoroutineTest extends TestCase { */ public function testCoroutineFunctionWithNonGeneratorCallback() { $value = 1; - $callable = Amp\coroutine(function ($value) { + $callable = \Amp\coroutine(function ($value) { return $value; }); @@ -563,7 +562,7 @@ class CoroutineTest extends TestCase { */ public function testCoroutineFunctionWithThrowingCallback() { $exception = new \Exception; - $callable = Amp\coroutine(function () use ($exception) { + $callable = \Amp\coroutine(function () use ($exception) { throw $exception; }); @@ -585,7 +584,7 @@ class CoroutineTest extends TestCase { * @depends testCoroutineFunction */ public function testCoroutineFunctionWithSuccessReturnCallback() { - $callable = Amp\coroutine(function () { + $callable = \Amp\coroutine(function () { return new Success(42); }); @@ -604,7 +603,7 @@ class CoroutineTest extends TestCase { } public function testCoroutineFunctionWithReactPromise() { - $callable = Amp\coroutine(function () { + $callable = \Amp\coroutine(function () { return new FulfilledReactPromise(42); }); diff --git a/test/FilterTest.php b/test/FilterTest.php index 3092da4..2b098b5 100644 --- a/test/FilterTest.php +++ b/test/FilterTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Producer; use Amp\Stream; use Amp\Emitter; @@ -14,7 +13,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase { Loop::run(function () use (&$invoked){ $emitter = new Emitter; - $stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) { + $stream = Stream\filter($emitter->stream(), function ($value) use (&$invoked) { $invoked = true; }); @@ -38,7 +37,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase { } }); - $stream = Amp\filter($producer, function ($value) use (&$count) { + $stream = Stream\filter($producer, function ($value) use (&$count) { ++$count; return $value & 1; }); @@ -69,7 +68,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase { } }); - $stream = Amp\filter($producer, function () use ($exception) { + $stream = Stream\filter($producer, function () use ($exception) { throw $exception; }); @@ -93,7 +92,7 @@ class FilterTest extends \PHPUnit\Framework\TestCase { Loop::run(function () use (&$invoked, &$reason, &$exception){ $emitter = new Emitter; - $stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) { + $stream = Stream\filter($emitter->stream(), function ($value) use (&$invoked) { $invoked = true; }); diff --git a/test/FirstTest.php b/test/FirstTest.php index 29db4a7..5ce6b54 100644 --- a/test/FirstTest.php +++ b/test/FirstTest.php @@ -6,6 +6,7 @@ use Amp; use Amp\Failure; use Amp\MultiReasonException; use Amp\Pause; +use Amp\Promise; use Amp\Success; use Amp\Loop; @@ -15,7 +16,7 @@ class FirstTest extends \PHPUnit\Framework\TestCase { * @expectedExceptionMessage No promises provided */ public function testEmptyArray() { - Amp\first([]); + Promise\first([]); } public function testSuccessfulPromisesArray() { @@ -25,7 +26,7 @@ class FirstTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\first($promises)->when($callback); + Promise\first($promises)->when($callback); $this->assertSame(1, $result); } @@ -38,7 +39,7 @@ class FirstTest extends \PHPUnit\Framework\TestCase { $reason = $exception; }; - Amp\first($promises)->when($callback); + Promise\first($promises)->when($callback); $this->assertInstanceOf(MultiReasonException::class, $reason); $this->assertEquals([$exception, $exception, $exception], $reason->getReasons()); @@ -52,7 +53,7 @@ class FirstTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\first($promises)->when($callback); + Promise\first($promises)->when($callback); $this->assertSame(3, $result); } @@ -69,7 +70,7 @@ class FirstTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\first($promises)->when($callback); + Promise\first($promises)->when($callback); }); $this->assertSame(3, $result); @@ -79,6 +80,6 @@ class FirstTest extends \PHPUnit\Framework\TestCase { * @expectedException \Amp\UnionTypeError */ public function testNonPromise() { - Amp\first([1]); + Promise\first([1]); } } diff --git a/test/IntervalTest.php b/test/IntervalTest.php index 17ac82b..95f3cfc 100644 --- a/test/IntervalTest.php +++ b/test/IntervalTest.php @@ -2,8 +2,9 @@ namespace Amp\Test; -use Amp; use Amp\Pause; +use Amp\Promise; +use Amp\Stream; use Amp\Loop; class IntervalTest extends \PHPUnit\Framework\TestCase { @@ -11,14 +12,14 @@ class IntervalTest extends \PHPUnit\Framework\TestCase { public function testInterval() { $count = 3; - $stream = Amp\interval(self::TIMEOUT, $count); + $stream = Stream\interval(self::TIMEOUT, $count); $i = 0; - $stream = Amp\each($stream, function ($value) use (&$i) { + $stream = Stream\map($stream, function ($value) use (&$i) { $this->assertSame(++$i, $value); }); - Amp\wait($stream); + Promise\wait($stream); $this->assertSame($count, $i); } @@ -30,7 +31,7 @@ class IntervalTest extends \PHPUnit\Framework\TestCase { $invoked = 0; $count = 5; Loop::run(function () use (&$invoked, $count) { - $stream = Amp\interval(self::TIMEOUT, $count); + $stream = Stream\interval(self::TIMEOUT, $count); $stream->listen(function () use (&$invoked) { ++$invoked; @@ -46,6 +47,6 @@ class IntervalTest extends \PHPUnit\Framework\TestCase { * @expectedExceptionMessage The number of times to emit must be a positive value */ public function testInvalidCount() { - Amp\interval(self::TIMEOUT, -1); + Stream\interval(self::TIMEOUT, -1); } } diff --git a/test/MergeTest.php b/test/MergeTest.php index 631ff5f..982ffce 100644 --- a/test/MergeTest.php +++ b/test/MergeTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Loop; use Amp\Producer; use Amp\Stream; @@ -24,12 +23,12 @@ class MergeTest extends \PHPUnit\Framework\TestCase { */ public function testMerge(array $streams, array $expected) { $streams = \array_map(function (array $stream): Stream { - return Amp\stream($stream); + return Stream\fromIterable($stream); }, $streams); - $stream = Amp\merge($streams); + $stream = Stream\merge($streams); - Amp\each($stream, function ($value) use ($expected) { + Stream\map($stream, function ($value) use ($expected) { static $i = 0; $this->assertSame($expected[$i++], $value); }); @@ -48,7 +47,7 @@ class MergeTest extends \PHPUnit\Framework\TestCase { throw $exception; }); - $stream = Amp\merge([$producer, Amp\stream(\range(1, 5))]); + $stream = Stream\merge([$producer, Stream\fromIterable(\range(1, 5))]); $callback = function ($exception, $value) use (&$reason) { $reason = $exception; @@ -64,6 +63,6 @@ class MergeTest extends \PHPUnit\Framework\TestCase { * @expectedException \Amp\UnionTypeError */ public function testNonStream() { - Amp\merge([1]); + Stream\merge([1]); } } diff --git a/test/PipeTest.php b/test/PipeTest.php index 3b94044..c00ae9b 100644 --- a/test/PipeTest.php +++ b/test/PipeTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Success; use Amp\Promise; @@ -20,7 +19,7 @@ class PipeTest extends \PHPUnit\Framework\TestCase { $promise = new Success($value); - $promise = Amp\pipe($promise, $callback); + $promise = Promise\pipe($promise, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -44,7 +43,7 @@ class PipeTest extends \PHPUnit\Framework\TestCase { $promise = new Failure($exception); - $promise = Amp\pipe($promise, $callback); + $promise = Promise\pipe($promise, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$reason) { @@ -71,7 +70,7 @@ class PipeTest extends \PHPUnit\Framework\TestCase { $promise = new Success($value); - $promise = Amp\pipe($promise, $callback); + $promise = Promise\pipe($promise, $callback); $callback = function ($exception, $value) use (&$reason) { $reason = $exception; @@ -97,7 +96,7 @@ class PipeTest extends \PHPUnit\Framework\TestCase { $promise = resolve($value); - $promise = Amp\pipe($promise, $callback); + $promise = Promise\pipe($promise, $callback); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -111,7 +110,7 @@ class PipeTest extends \PHPUnit\Framework\TestCase { } public function testNonPromise() { - $this->expectException(Amp\UnionTypeError::class); - Amp\pipe(42, function () {}); + $this->expectException(\Amp\UnionTypeError::class); + Promise\pipe(42, function () {}); } } diff --git a/test/ProducerTest.php b/test/ProducerTest.php index 685db12..606344a 100644 --- a/test/ProducerTest.php +++ b/test/ProducerTest.php @@ -175,7 +175,7 @@ class ProducerTest extends TestCase { throw $exception; }); - Amp\wait($producer); + Amp\Promise\wait($producer); }); } catch (\Exception $caught) { $this->assertSame($exception, $caught); diff --git a/test/MapTest.php b/test/PromiseMapTest.php similarity index 85% rename from test/MapTest.php rename to test/PromiseMapTest.php index fc7a20d..a91f0ca 100644 --- a/test/MapTest.php +++ b/test/PromiseMapTest.php @@ -2,20 +2,19 @@ namespace Amp\Test; -use Amp; use Amp\Deferred; use Amp\Failure; use Amp\Loop; use Amp\Pause; -use Amp\Success; use Amp\Promise; +use Amp\Success; -class MapTest extends \PHPUnit\Framework\TestCase { +class PromiseMapTest extends \PHPUnit\Framework\TestCase { public function testEmptyArray() { $values = []; $invoked = false; - $result = Amp\map(function () use (&$invoked) { + $result = Promise\map(function () use (&$invoked) { $invoked = true; }, $values); @@ -33,13 +32,13 @@ class MapTest extends \PHPUnit\Framework\TestCase { return $value - 1; }; - $result = Amp\map($callback, $promises); + $result = Promise\map($callback, $promises); $this->assertTrue(\is_array($result)); foreach ($result as $key => $promise) { $this->assertInstanceOf(Promise::class, $promise); - $this->assertSame($key, Amp\wait($promise)); + $this->assertSame($key, Promise\wait($promise)); } $this->assertSame(\count($promises), $count); @@ -63,7 +62,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { return $value - 1; }; - $result = Amp\map($callback, $promises); + $result = Promise\map($callback, $promises); $this->assertTrue(\is_array($result)); @@ -73,7 +72,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { foreach ($result as $key => $promise) { $this->assertInstanceOf(Promise::class, $promise); - $this->assertSame($key, Amp\wait($promise)); + $this->assertSame($key, Promise\wait($promise)); } $this->assertSame(\count($promises), $count); @@ -90,7 +89,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { return $value - 1; }; - $result = Amp\map($callback, $promises); + $result = Promise\map($callback, $promises); $this->assertTrue(\is_array($result)); @@ -115,7 +114,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { throw $exception; }; - $result = Amp\map($callback, $promises); + $result = Promise\map($callback, $promises); foreach ($result as $key => $promise) { $this->assertInstanceOf(Promise::class, $promise); @@ -123,7 +122,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { foreach ($result as $key => $promise) { try { - Amp\wait($promise); + Promise\wait($promise); } catch (\Exception $reason) { $this->assertSame($exception, $reason); } @@ -144,7 +143,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { return $value1 + $value2; }; - $result = Amp\map($callback, $promises1, $promises2); + $result = Promise\map($callback, $promises1, $promises2); foreach ($result as $key => $promise) { $this->assertInstanceOf(Promise::class, $promise); @@ -152,7 +151,7 @@ class MapTest extends \PHPUnit\Framework\TestCase { foreach ($result as $promise) { $this->assertInstanceOf(Promise::class, $promise); - $this->assertSame(4, Amp\wait($promise)); + $this->assertSame(4, Promise\wait($promise)); } $this->assertSame(3, $count); @@ -173,10 +172,10 @@ class MapTest extends \PHPUnit\Framework\TestCase { return $value1 + $value2; }; - $result = Amp\map($callback, $promises1, $promises2); + $result = Promise\map($callback, $promises1, $promises2); foreach ($result as $promise) { - $this->assertSame(4, Amp\wait($promise)); + $this->assertSame(4, Promise\wait($promise)); } $this->assertSame(3, $count); diff --git a/test/RethrowTest.php b/test/RethrowTest.php index 402932b..198734f 100644 --- a/test/RethrowTest.php +++ b/test/RethrowTest.php @@ -2,9 +2,9 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Loop; +use Amp\Promise; use PHPUnit\Framework\TestCase; use function React\Promise\reject; @@ -16,7 +16,7 @@ class RethrowTest extends TestCase { Loop::run(function () use ($exception) { $promise = new Failure($exception); - Amp\rethrow($promise); + Promise\rethrow($promise); }); } catch (\Exception $reason) { $this->assertSame($exception, $reason); @@ -36,7 +36,7 @@ class RethrowTest extends TestCase { Loop::run(function () use ($exception) { $promise = reject($exception); - Amp\rethrow($promise); + Promise\rethrow($promise); }); } catch (\Exception $reason) { $this->assertSame($exception, $reason); @@ -47,7 +47,7 @@ class RethrowTest extends TestCase { } public function testNonPromise() { - $this->expectException(Amp\UnionTypeError::class); - Amp\rethrow(42); + $this->expectException(\Amp\UnionTypeError::class); + Promise\rethrow(42); } } diff --git a/test/SomeTest.php b/test/SomeTest.php index 6841ebc..4492bbe 100644 --- a/test/SomeTest.php +++ b/test/SomeTest.php @@ -2,20 +2,16 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\MultiReasonException; use Amp\Pause; +use Amp\Promise; use Amp\Success; use Amp\Loop; class SomeTest extends \PHPUnit\Framework\TestCase { - /** - * @expectedException \Error - * @expectedExceptionMessage No promises provided - */ public function testEmptyArray() { - Amp\some([]); + $this->assertSame([[], []], Promise\wait(Promise\some([]))); } public function testSuccessfulPromisesArray() { @@ -25,7 +21,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\some($promises)->when($callback); + Promise\some($promises)->when($callback); $this->assertSame([[], [1, 2, 3]], $result); } @@ -38,7 +34,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase { $reason = $exception; }; - Amp\some($promises)->when($callback); + Promise\some($promises)->when($callback); $this->assertInstanceOf(MultiReasonException::class, $reason); $this->assertEquals([$exception, $exception, $exception], $reason->getReasons()); @@ -52,7 +48,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\some($promises)->when($callback); + Promise\some($promises)->when($callback); $this->assertSame([[0 => $exception, 1 => $exception], [2 => 3]], $result); } @@ -69,7 +65,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\some($promises)->when($callback); + Promise\some($promises)->when($callback); }); $this->assertEquals([[], [0 => 1, 1 => 2, 2 => 3]], $result); @@ -89,7 +85,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase { $result = $value; }; - Amp\some($promises)->when($callback); + Promise\some($promises)->when($callback); }); $this->assertEquals($expected, $result); @@ -99,6 +95,6 @@ class SomeTest extends \PHPUnit\Framework\TestCase { * @expectedException \Error */ public function testNonPromise() { - Amp\some([1]); + Promise\some([1]); } } diff --git a/test/StreamTest.php b/test/StreamFromIterableTest.php similarity index 80% rename from test/StreamTest.php rename to test/StreamFromIterableTest.php index ef89cd6..4373509 100644 --- a/test/StreamTest.php +++ b/test/StreamFromIterableTest.php @@ -2,17 +2,17 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Pause; +use Amp\Stream; use Amp\Success; use Amp\Loop; -class StreamTest extends \PHPUnit\Framework\TestCase { +class StreamFromIterableTest extends \PHPUnit\Framework\TestCase { public function testSuccessfulPromises() { $results = []; Loop::run(function () use (&$results) { - $stream = Amp\stream([new Success(1), new Success(2), new Success(3)]); + $stream = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]); $stream->listen(function ($value) use (&$results) { $results[] = $value; @@ -25,7 +25,7 @@ class StreamTest extends \PHPUnit\Framework\TestCase { public function testFailedPromises() { $exception = new \Exception; Loop::run(function () use (&$reason, $exception) { - $stream = Amp\stream([new Failure($exception), new Failure($exception)]); + $stream = Stream\fromIterable([new Failure($exception), new Failure($exception)]); $callback = function ($exception, $value) use (&$reason) { $reason = $exception; @@ -41,7 +41,7 @@ class StreamTest extends \PHPUnit\Framework\TestCase { $exception = new \Exception; $results = []; Loop::run(function () use (&$results, &$reason, $exception) { - $stream = Amp\stream([new Success(1), new Success(2), new Failure($exception), new Success(4)]); + $stream = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]); $stream->listen(function ($value) use (&$results) { $results[] = $value; @@ -61,7 +61,7 @@ class StreamTest extends \PHPUnit\Framework\TestCase { public function testPendingPromises() { $results = []; Loop::run(function () use (&$results) { - $stream = Amp\stream([new Pause(30, 1), new Pause(10, 2), new Pause(20, 3), new Success(4)]); + $stream = Stream\fromIterable([new Pause(30, 1), new Pause(10, 2), new Pause(20, 3), new Success(4)]); $stream->listen(function ($value) use (&$results) { $results[] = $value; @@ -80,7 +80,7 @@ class StreamTest extends \PHPUnit\Framework\TestCase { } })(); - $stream = Amp\stream($generator); + $stream = Stream\fromIterable($generator); $stream->listen(function ($value) use (&$results) { $results[] = $value; @@ -95,7 +95,7 @@ class StreamTest extends \PHPUnit\Framework\TestCase { * @dataProvider provideInvalidStreamArguments */ public function testInvalid($arg) { - Amp\stream($arg); + Stream\fromIterable($arg); } public function provideInvalidStreamArguments() { diff --git a/test/EachTest.php b/test/StreamMapTest.php similarity index 89% rename from test/EachTest.php rename to test/StreamMapTest.php index 06b23ab..fac1220 100644 --- a/test/EachTest.php +++ b/test/StreamMapTest.php @@ -8,13 +8,13 @@ use Amp\Stream; use Amp\Emitter; use Amp\Loop; -class EachTest extends \PHPUnit\Framework\TestCase { +class StreamMapTest extends \PHPUnit\Framework\TestCase { public function testNoValuesEmitted() { $invoked = false; Loop::run(function () use (&$invoked){ $emitter = new Emitter; - $stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) { + $stream = Stream\map($emitter->stream(), function ($value) use (&$invoked) { $invoked = true; }); @@ -39,7 +39,7 @@ class EachTest extends \PHPUnit\Framework\TestCase { return $final; }); - $stream = Amp\each($producer, function ($value) use (&$count) { + $stream = Stream\map($producer, function ($value) use (&$count) { ++$count; return $value + 1; }, function ($value) use (&$invoked) { @@ -73,7 +73,7 @@ class EachTest extends \PHPUnit\Framework\TestCase { } }); - $stream = Amp\each($producer, function () use ($exception) { + $stream = Stream\map($producer, function () use ($exception) { throw $exception; }); @@ -106,7 +106,7 @@ class EachTest extends \PHPUnit\Framework\TestCase { } }); - $stream = Amp\each($producer, function ($value) use (&$count) { + $stream = Stream\map($producer, function ($value) use (&$count) { ++$count; return $value + 1; }, function ($value) use ($exception) { @@ -135,7 +135,7 @@ class EachTest extends \PHPUnit\Framework\TestCase { Loop::run(function () use (&$invoked, &$reason, &$exception){ $emitter = new Emitter; - $stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) { + $stream = Stream\map($emitter->stream(), function ($value) use (&$invoked) { $invoked = true; }); diff --git a/test/TimeoutTest.php b/test/TimeoutTest.php index 07d5a0f..1aee3b0 100644 --- a/test/TimeoutTest.php +++ b/test/TimeoutTest.php @@ -2,7 +2,6 @@ namespace Amp\Test; -use Amp; use Amp\Failure; use Amp\Loop; use Amp\Pause; @@ -17,7 +16,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { $promise = new Success($value); - $promise = Amp\timeout($promise, 100); + $promise = Promise\timeout($promise, 100); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -36,7 +35,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { $promise = new Failure($exception); - $promise = Amp\timeout($promise, 100); + $promise = Promise\timeout($promise, 100); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$reason) { @@ -58,7 +57,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { Loop::run(function () use (&$result, $value) { $promise = new Pause(50, $value); - $promise = Amp\timeout($promise, 100); + $promise = Promise\timeout($promise, 100); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -78,7 +77,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { Loop::run(function () use (&$reason) { $promise = new Pause(200); - $promise = Amp\timeout($promise, 100); + $promise = Promise\timeout($promise, 100); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$reason) { @@ -88,7 +87,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { $promise->when($callback); }); - $this->assertInstanceOf(Amp\TimeoutException::class, $reason); + $this->assertInstanceOf(\Amp\TimeoutException::class, $reason); } /** @@ -100,7 +99,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { $promise = resolve($value); - $promise = Amp\timeout($promise, 100); + $promise = Promise\timeout($promise, 100); $this->assertInstanceOf(Promise::class, $promise); $callback = function ($exception, $value) use (&$result) { @@ -114,7 +113,7 @@ class TimeoutTest extends \PHPUnit\Framework\TestCase { } public function testNonPromise() { - $this->expectException(Amp\UnionTypeError::class); - Amp\timeout(42, 42); + $this->expectException(\Amp\UnionTypeError::class); + Promise\timeout(42, 42); } } diff --git a/test/WaitTest.php b/test/WaitTest.php index a9c6d08..39915a2 100644 --- a/test/WaitTest.php +++ b/test/WaitTest.php @@ -2,10 +2,10 @@ namespace Amp\Test; -use Amp; use Amp\Deferred; use Amp\Failure; use Amp\Pause; +use Amp\Promise; use Amp\Success; use Amp\Loop; use PHPUnit\Framework\TestCase; @@ -17,7 +17,7 @@ class WaitTest extends TestCase { $promise = new Success($value); - $result = Amp\wait($promise); + $result = Promise\wait($promise); $this->assertSame($value, $result); } @@ -28,7 +28,7 @@ class WaitTest extends TestCase { $promise = new Failure($exception); try { - $result = Amp\wait($promise); + $result = Promise\wait($promise); } catch (\Exception $e) { $this->assertSame($exception, $e); return; @@ -46,7 +46,7 @@ class WaitTest extends TestCase { $promise = new Pause(100, $value); - $result = Amp\wait($promise); + $result = Promise\wait($promise); $this->assertSame($value, $result); }); @@ -59,7 +59,7 @@ class WaitTest extends TestCase { public function testPromiseWithNoResolutionPathThrowsException() { $promise = new Deferred; - $result = Amp\wait($promise->promise()); + $result = Promise\wait($promise->promise()); } /** @@ -70,13 +70,13 @@ class WaitTest extends TestCase { $promise = resolve($value); - $result = Amp\wait($promise); + $result = Promise\wait($promise); $this->assertSame($value, $result); } public function testNonPromise() { - $this->expectException(Amp\UnionTypeError::class); - Amp\wait(42); + $this->expectException(\Amp\UnionTypeError::class); + Promise\wait(42); } }