public function testFetchFile() { $response = new Response(200, array(), '# hello', '1.0', 'OK'); $this->expectRequest($this->uri . 'README.md?view=co')->will($this->returnValue(Promise\resolve($response))); $promise = $this->client->fetchFile('README.md'); $this->expectPromiseResolveWith('# hello', $promise); }
public function testWillWriteAuthCommandIfTargetContainsUserInfo() { $stream = $this->getMockBuilder('React\\Stream\\Stream')->disableOriginalConstructor()->getMock(); $stream->expects($this->once())->method('write')->with("*2\r\n\$4\r\nauth\r\n\$11\r\nhello:world\r\n"); $this->connector->expects($this->once())->method('create')->willReturn(Promise\resolve($stream)); $this->factory->createClient('tcp://*****:*****@127.0.0.1'); }
private function resolveHostname($host) { if (false !== filter_var($host, FILTER_VALIDATE_IP)) { return Promise\resolve($host); } return $this->resolver->resolve($host); }
public function get($key) { if (!isset($this->data[$key])) { return Promise\reject(); } return Promise\resolve($this->data[$key]); }
public function testWillNotInvokeProberIfSchemeIsProtocol() { $stream = $this->getMockBuilder('React\\Stream\\Stream')->disableOriginalConstructor()->getMock(); $this->connector->expects($this->once())->method('create')->will($this->returnValue(Promise\resolve($stream))); $this->prober->expects($this->never())->method('probe'); $this->expectPromiseResolve($this->factory->createClient('legacy://localhost')); }
/** @test */ public function rejectShouldRejectWithFulfilledPromise() { extract($this->getPromiseTestAdapter()); $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1)); $promise()->then($this->expectCallableNever(), $mock); $reject(Promise\resolve(1)); }
/** * {@inheritdoc} */ public function getValues() : PromiseInterface { $counters = $this->infoProvider->getCounters(); $storage = new \SplObjectStorage(); $storage->attach(new Metric('current_read_streams', (double) $counters['streams']['read']['current'])); $storage->attach(new Metric('current_write_streams', (double) $counters['streams']['write']['current'])); return resolve($storage); }
public function create($path, $unusedPort = 0) { $resource = @stream_socket_client('unix://' . $path, $errno, $errstr, 1.0); if (!$resource) { return Promise\reject(new RuntimeException('Unable to connect to unix domain socket "' . $path . '": ' . $errstr, $errno)); } return Promise\resolve(new Stream($resource, $this->loop)); }
/** @test */ public function resolveShouldResolveWithPromisedValue() { $adapter = $this->getPromiseTestAdapter(); $mock = $this->createCallableMock(); $mock->expects($this->once())->method('__invoke')->with($this->identicalTo(1)); $adapter->promise()->then($mock); $adapter->resolve(Promise\resolve(1)); }
/** @internal */ public function checkConnectedSocket($socket) { // The following hack looks like the only way to // detect connection refused errors with PHP's stream sockets. if (false === stream_socket_get_name($socket, true)) { return Promise\reject(new ConnectionException('Connection refused')); } return Promise\resolve($socket); }
public function testResolvedWillNotStartTimer() { $promise = Promise\resolve(); Timer\timeout($promise, 3, $this->loop); $time = microtime(true); $this->loop->run(); $time = microtime(true) - $time; $this->assertLessThan(0.5, $time); }
public function testAwaitOnceWithTimeoutWillResolvemmediatelyAndCleanUpTimeout() { $promise = Promise\resolve(true); $time = microtime(true); Block\await($promise, $this->loop, 5.0); $this->loop->run(); $time = microtime(true) - $time; $this->assertLessThan(0.1, $time); }
/** @test */ public function resolveSouldProvideDefaultErrorbackToExecutor() { $executor = $this->createExecutorMock(); $executor->expects($this->once())->method('query')->with($this->anything(), $this->isInstanceOf('React\\Dns\\Query\\Query'))->will($this->returnCallback(function ($nameserver, $query) { return Promise\resolve(); })); $resolver = new Resolver('8.8.8.8:53', $executor); $resolver->resolve('igor.io')->then($this->expectCallableNever()); }
private function callQueryCallbackWithAddress($address) { return $this->returnCallback(function ($nameserver, $query) use($address) { $response = new Message(); $response->header->set('qr', 1); $response->questions[] = new Record($query->name, $query->type, $query->class); $response->answers[] = new Record($query->name, $query->type, $query->class, 3600, $address); return Promise\resolve($response); }); }
/** * @param string $data * @return PromiseInterface * @throws Exception */ public function send($data) { try { $this->connect(); stream_set_blocking($this->sock, true); fwrite($this->sock, $data); return Promise\resolve(); } catch (\Exception $e) { return Promise\reject($e); } }
public function testAwaitAnyWithResolvedWillCancelPending() { $cancelled = false; $promise = new Promise\Promise(function () { }, function () use(&$cancelled) { $cancelled = true; }); $all = array(Promise\resolve(2), $promise); $this->assertEquals(2, Block\awaitAny($all, $this->loop)); $this->assertTrue($cancelled); }
public function testListenerSetsResolvedPromiseAsResponseOnEvent() { $loop = Factory::create(); $response = new Response(); $promise = Promise\resolve($response); $event = $this->getMockBuilder(GetResponseForControllerResultEvent::class)->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getControllerResult')->willReturn($promise); $event->expects($this->once())->method('setResponse')->with($response); $listener = new PromiseListener($loop); $listener->onKernelView($event); }
public function refresh() : PromiseInterface { return $this->getTransport()->request('repos/' . $this->repositoryId() . '/branches')->then(function ($json) { foreach ($json['broadcasts'] as $broadcast) { if ($broadcast['id'] != $this->id()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('Broadcast', $broadcast)); } return reject(); }); }
/** * @return PromiseInterface */ public function refresh() : PromiseInterface { return $this->getTransport()->request('jobs/' . $this->jobId() . '/annotations')->then(function ($json) { foreach ($json['annotations'] as $annotation) { if ($annotation['id'] != $this->id()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('Annotation', $annotation)); } return reject(); }); }
public function refresh() : PromiseInterface { return $this->getTransport()->request('hooks')->then(function ($json) { foreach ($json['hooks'] as $hook) { if ($hook['id'] != $this->id()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('Hook', $hook)); } return reject(); }); }
public function refresh() : PromiseInterface { return $this->getTransport()->request('repos/' . $this->repositoryId() . '/caches')->then(function ($json) { foreach ($json['caches'] as $cache) { if ($cache['slug'] != $this->slug()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('Cache', $cache)); } return reject(); }); }
/** * @return PromiseInterface */ public function refresh() : PromiseInterface { return $this->getTransport()->request('accounts')->then(function ($json) { foreach ($json['accounts'] as $account) { if ($account['id'] != $this->id()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('Account', $account)); } return reject(); }); }
public function refresh() : PromiseInterface { return $this->getTransport()->request('settings/env_vars?repository_id=' . $this->repositoryId())->then(function ($json) { foreach ($json['env_vars'] as $envVar) { if ($envVar['id'] != $this->id()) { continue; } return resolve($this->getTransport()->getHydrator()->hydrate('EnvironmentVariable', $envVar)); } return reject(); }); }
public function testWillCloseOtherIfOneResolves() { $resolved = Promise\resolve($this->getMock('React\\Stream\\DuplexStreamInterface')); $first = $this->getMock('React\\SocketClient\\ConnectorInterface'); $first->expects($this->once())->method('create')->with('google.com', 80)->willReturn($resolved); $slower = $this->getMock('React\\Stream\\DuplexStreamInterface'); $slower->expects($this->once())->method('close'); $second = $this->getMock('React\\SocketClient\\ConnectorInterface'); $second->expects($this->once())->method('create')->with('google.com', 80)->willReturn(Promise\resolve($slower)); $connector = new ConnectionManagerConcurrent(array($first, $second)); $promise = $connector->create('google.com', 80); $this->assertPromiseResolve($promise); }
/** * @internal * @param ResponseInterface $response * @return PromiseInterface Promise<ResponseInterface, Exception> */ public function bufferResponse(ResponseInterface $response) { $stream = $response->getBody(); // body is not streaming => already buffered if (!$stream instanceof ReadableStreamInterface) { return Promise\resolve($response); } // buffer stream and resolve with buffered body $messageFactory = $this->messageFactory; return BufferedSink::createPromise($stream)->then(function ($body) use($response, $messageFactory) { return $response->withBody($messageFactory->body($body)); }); }
/** * @test */ public function resolveWithNoAnswersShouldCallErrbackIfGiven() { $executor = $this->createExecutorMock(); $executor->expects($this->once())->method('query')->with($this->anything(), $this->isInstanceOf('React\\Dns\\Query\\Query'))->will($this->returnCallback(function ($nameserver, $query) { $response = new Message(); $response->header->set('qr', 1); $response->questions[] = new Record($query->name, $query->type, $query->class); return Promise\resolve($response); })); $errback = $this->expectCallableOnceWith($this->isInstanceOf('React\\Dns\\RecordNotFoundException')); $resolver = new Resolver('8.8.8.8:53', $executor); $resolver->resolve('igor.io')->then($this->expectCallableNever(), $errback); }
public function testReceivingStreamingBodyWillResolveWithStreamingResponseIfStreamingIsEnabled() { $messageFactory = new MessageFactory(); $request = $this->getMock('Psr\\Http\\Message\\RequestInterface'); $response = $messageFactory->response(1.0, 200, 'OK', array(), $this->getMock('React\\Stream\\ReadableStreamInterface')); // mock sender to resolve promise with the given $response in response to the given $request $sender = $this->getMockBuilder('Clue\\React\\Buzz\\Io\\Sender')->disableOriginalConstructor()->getMock(); $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response)); $transaction = new Transaction($request, $sender, array('streaming' => true), $messageFactory); $promise = $transaction->send(); $response = Block\await($promise, $this->getMock('React\\EventLoop\\LoopInterface')); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('', (string) $response->getBody()); }
public function parseEtcResolvConf($contents) { $nameservers = array(); $contents = preg_replace('/^#/', '', $contents); $lines = preg_split('/\\r?\\n/is', $contents); foreach ($lines as $line) { if (preg_match('/^nameserver (.+)/', $line, $match)) { $nameservers[] = $match[1]; } } $config = new Config(); $config->nameservers = $nameservers; return Promise\resolve($config); }
/** * @covers React\Dns\Query\RetryExecutor * @test */ public function queryShouldRetryQueryOnTimeout() { $response = $this->createStandardResponse(); $executor = $this->createExecutorMock(); $executor->expects($this->exactly(2))->method('query')->with('8.8.8.8', $this->isInstanceOf('React\\Dns\\Query\\Query'))->will($this->onConsecutiveCalls($this->returnCallback(function ($domain, $query) { return Promise\reject(new TimeoutException("timeout")); }), $this->returnCallback(function ($domain, $query) use($response) { return Promise\resolve($response); }))); $callback = $this->createCallableMock(); $callback->expects($this->once())->method('__invoke')->with($this->isInstanceOf('React\\Dns\\Model\\Message')); $errorback = $this->expectCallableNever(); $retryExecutor = new RetryExecutor($executor, 2); $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $retryExecutor->query('8.8.8.8', $query)->then($callback, $errorback); }
/** * Waits until the controller's promise resolves. * * @param GetResponseForControllerResultEvent $event * * @return mixed * * @throws \Exception */ private function awaitControllerResult(GetResponseForControllerResultEvent $event) { $wait = true; $resolved = null; $exception = null; Promise\resolve($event->getControllerResult())->then(function ($result) use(&$resolved, &$wait) { $resolved = $result; $wait = false; }, function ($result) use(&$exception, &$wait) { $exception = $result; $wait = false; })->always(function () { $this->loop->stop(); }); while ($wait) { $this->loop->run(); } if ($exception instanceof \Throwable || $exception instanceof \Exception) { throw $exception; } return $resolved; }