/** * Perform a request and return a promise object (an object * implementing then method) * * @param RequestInterface $request * @param array $options * * @return object|P\PromiseInterface */ public function send(RequestInterface $request, array $options = []) { $request = $this->preRequest($request, $options); $promise = $this->transport->exec($request)->then(function (ResponseInterface $response) use($request, $options, &$promise) { $event = $this->responseEvent($request, $response, $options); $response = $event->getResponse(); if (!$this->isResponseOK($response)) { return P\rejection_for($event); } return $response; })->then(null, function (ResponseEvent $event) use($options) { // options is passed by reference. Use array_merge to ensure // opts is a copy and not a reference $opts = array_merge([], $options); $naReq = $this->getNextRequest($event, $opts); if ($naReq) { $options['exceptions'] = false; return $this->send($naReq, $opts); } $response = $event->getResponse(); if ($options['exceptions']) { throw new BadApiResponseException($response, $response->getStatusCode(), $response->getReasonPhrase()); } return $response; }); // Add in shutdown queue $this->queue->add(function () use($promise) { $promise->wait(false); }); return $promise; }
public function testExecutesTasksInOrder() { $tq = new TaskQueue(false); $called = []; $tq->add(function () use(&$called) { $called[] = 'a'; }); $tq->add(function () use(&$called) { $called[] = 'b'; }); $tq->add(function () use(&$called) { $called[] = 'c'; }); $tq->run(); $this->assertEquals(['a', 'b', 'c'], $called); }