/** @test */ public function shouldForwardReasonWhenCallbackIsNull() { $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1)); $d = new Deferred(); $d->then($this->expectCallableNever())->then($this->expectCallableNever(), $mock); $d->reject(1); }
/** @test */ public function shouldRejectARejectedPromise() { $expected = 123; $d = new Deferred(); $d->reject($expected); $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo($expected)); When::resolve($d->promise())->then($this->expectCallableNever(), $mock); }
/** @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(); }
/** @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 * @dataProvider invalidCallbackDataProvider **/ public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var) { $errorCollector = new ErrorCollector(); $errorCollector->register(); $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1)); $d = new Deferred(); $d->then(null, $var)->then($this->expectCallableNever(), $mock); $d->reject(1); $errorCollector->assertCollectedError('Invalid $errorHandler argument passed to then(), must be null or callable.', E_USER_NOTICE); $errorCollector->unregister(); }
public static function some($promisesOrValues, $howMany, $fulfilledHandler = null, $errorHandler = null, $progressHandler = null) { return When::resolve($promisesOrValues)->then(function ($array) use($howMany, $fulfilledHandler, $errorHandler, $progressHandler) { if (!is_array($array)) { $array = array(); } $len = count($array); $toResolve = max(0, min($howMany, $len)); $values = array(); $deferred = new Deferred(); if (!$toResolve) { $deferred->resolve($values); } else { $toReject = $len - $toResolve + 1; $reasons = array(); $progress = array($deferred, 'progress'); $fulfillOne = function ($val, $i) use(&$values, &$toResolve, $deferred) { $values[$i] = $val; if (0 === --$toResolve) { $deferred->resolve($values); return true; } }; $rejectOne = function ($reason, $i) use(&$reasons, &$toReject, $deferred) { $reasons[$i] = $reason; if (0 === --$toReject) { $deferred->reject($reasons); return true; } }; foreach ($array as $i => $promiseOrValue) { $fulfiller = function ($val) use($i, &$fulfillOne, &$rejectOne) { $reset = $fulfillOne($val, $i); if (true === $reset) { $fulfillOne = $rejectOne = function () { }; } }; $rejecter = function ($val) use($i, &$fulfillOne, &$rejectOne) { $reset = $rejectOne($val, $i); if (true === $reset) { $fulfillOne = $rejectOne = function () { }; } }; When::resolve($promiseOrValue)->then($fulfiller, $rejecter, $progress); } } return $deferred->then($fulfilledHandler, $errorHandler, $progressHandler); }); }
/** * Declare this exchange. * * @recoil-coroutine * * @param boolean $passive True to use a "passive" declare mode. * @param Channel|null $channel The application-managed channel to use (null = auto-managed). * * @throws ResourceNotFoundException The exchange does not exist. * @throws DeclareException The exchange already exists with different a type or options. * @throws DeclareException A reserved exchange name was used. * @throws ConnectionException The connection is closed. * @throws ChannelException The channel is closed. */ private function declareExchange($passive, Channel $channel = null) : Generator { if ('amq.' === substr($this->name, 0, 4)) { return reject(DeclareException::exchangeNameIsReserved($this->name)); } $deferred = new Deferred(); $this->broker->acquireChannel(function ($exception, $brokerChannel) use($channel, $passive, $deferred) { if ($exception) { $deferred->reject($exception); } else { $brokerChannel->call(ExchangeDeclareFrame::create($this->name, $this->type->value(), $passive, $this->options->durable, $this->options->autoDelete, $this->options->internal, false, $this->arguments), ExchangeDeclareOkFrame::METHOD_ID, function ($exception, $frame) use($brokerChannel, $channel, $deferred) { if (!$channel) { $this->broker->releaseChannel($brokerChannel); } if ($exception) { if ($exception instanceof AmqpException) { if ($exception->getCode() === Constants::PRECONDITION_FAILED) { $exception = DeclareException::exchangeTypeOrOptionMismatch($this->name, $this->type, $this->options, $exception); } elseif ($exception->getCode() === Constants::NOT_FOUND) { $exception = ResourceNotFoundException::exchangeNotFound($this->name, $exception); } } $deferred->reject($exception); } else { $deferred->resolve($this); } }); } }, $channel); return $deferred->promise(); }
/** * Get the message content as it becomes available. * * @see IncomingMessage::content() to get the content as a string. * * @see IncomingMessage::contentStream() to read the content from a stream. * * The returned promise is notified when content arrives. * @see https://github.com/reactphp/promise#extendedpromiseinterfaceprogress * * Cancelling the returned promise discards any remaining content. * * @return null [via promise] All content has been received. * @throws LogicException [via promise] The message content has already been consumed or discarded. */ public function contentUnbuffered() { if ($this->contentHandled) { return reject(new LogicException('The message content has already been consumed or discarded.')); } $this->contentHandled = true; if (0 === $this->headerFrame->contentLength) { return resolve(); } $deferred = new Deferred($this->cancelContentListener); $this->onBodyFrame = function ($exception, $frame, $final) use($deferred) { if ($exception) { $deferred->reject($exception); } else { $deferred->notify($frame->content); if ($final) { $deferred->resolve(); } } }; return $deferred->promise(); }
/** @test */ public function shouldReturnSilentlyOnProgressWhenAlreadyRejected() { $d = new Deferred(); $d->reject(1); $this->assertNull($d->progress()); }