/** * Test __construct. */ public function testConstruct() { $this->given($resolve = $this->delegateMock(), $reject = $this->delegateMock(), $notify = $this->delegateMock())->when($this->newTestedInstance($resolve, $reject, $notify))->then()->delegateCall($resolve)->once()->delegateCall($reject)->once()->delegateCall($notify)->once(); $this->given($promise = $this->newDefaultTestedInstance())->then()->boolean($promise->state()->equals(State::PENDING()))->isTrue(); }
/** * @param string $method * * @return string */ protected function scheduleTickTest($method) { $this->given($loop = $this->newDefaultMockTestedInstance(), $task = $this->delegateMockWithReturn('foo'))->when($promise = $loop->{$method}($task))->then()->object($promise)->isInstanceOf(PromiseInterface::class)->boolean($promise->state()->equals(State::PENDING()))->isTrue(); $this->given($onFulfilled = $this->delegateMock())->when(function () use($loop, $promise, $onFulfilled) { $promise->then($onFulfilled); $loop->tick(); })->then()->delegateCall($task)->once()->delegateCall($onFulfilled)->withArguments('foo')->once(); }
/** * 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 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(); }
/** * Test reject. */ public function testReject() { $this->given($deferred = $this->newDefaultTestedInstance(), $reason = new \Exception())->when($deferred->reject($reason))->then()->boolean($deferred->promise()->state()->equals(State::REJECTED()))->isTrue(); $this->given($onRejected = $this->delegateMock())->when($deferred->promise()->then(null, $onRejected))->then()->delegateCall($onRejected)->withArguments($reason)->once(); $this->invalidActionTest($deferred); }
/** * @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); }