/** * Test always. * * @param PromiseInterface $promise * * @dataProvider promiseDataProvider */ public function testAlways(PromiseInterface $promise) { $this->given($promise, $onFulfilledOrRejected = $this->delegateMock(), $onNotify = $this->delegateMock())->when($always = $promise->always($onFulfilledOrRejected, $onNotify))->then()->object($always)->isInstanceOf(PromiseInterface::class)->boolean($promise->state()->equals($always->state()))->isTrue(); if ($promise->state()->equals(State::FULFILLED())) { $this->given($value = $this->defaultResolveValue())->then()->delegateCall($onFulfilledOrRejected)->withArguments($value)->once(); } if ($promise->state()->equals(State::REJECTED())) { $this->given($reason = $this->defaultRejectReason())->then()->delegateCall($onFulfilledOrRejected)->withArguments($reason)->once(); } }
/** * Test resolve. */ public function testResolve() { $this->given($deferred = $this->newDefaultTestedInstance(), $value = 'foo')->when($deferred->resolve($value))->then()->boolean($deferred->promise()->state()->equals(State::FULFILLED()))->isTrue(); $this->given($onFulfilled = $this->delegateMock())->when($deferred->promise()->then($onFulfilled))->then()->delegateCall($onFulfilled)->withArguments($value)->once(); $this->invalidActionTest($deferred); }
/** * Test all. */ public function testAll() { $this->given($deferred = Promises::defer(), $promise1 = Promises::fulfilled(1), $promise2 = Promises::fulfilled(2))->when($all = Promises::all(array($deferred->promise(), $promise1, $promise2)))->then()->object($all)->isInstanceOf(PromiseInterface::class)->boolean($all->state()->equals(State::PENDING()))->isTrue(); $this->when($deferred->resolve(0))->then()->boolean($all->state()->equals(State::FULFILLED()))->isTrue(); $this->given($onFulfilled = $this->delegateMock())->when($all->then($onFulfilled))->then()->delegateCall($onFulfilled)->withArguments(array(0, 1, 2))->once(); }
/** * @param PromiseInterface $promise * @param LoopInterface $loop * @param int|float $timeout * * @throws RejectionException * * @return mixed */ public static function get(PromiseInterface $promise, LoopInterface $loop, $timeout = null) { $result = null; $rejectionReason = null; if ($timeout !== null) { $promise = self::timeout($promise, $timeout, $loop); } $promise->then(function ($value = null) use(&$result) { $result = $value; }, function ($reason = null) use(&$rejectionReason) { $rejectionReason = $reason; })->always(function () use($loop) { $loop->stop(); }); while ($promise->state()->equals(State::PENDING())) { $loop->run(); } if ($promise->state()->equals(State::FULFILLED())) { return $result; } throw new RejectionException($rejectionReason); }