Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 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');
 }
Ejemplo n.º 3
0
 private function resolveHostname($host)
 {
     if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
         return Promise\resolve($host);
     }
     return $this->resolver->resolve($host);
 }
Ejemplo n.º 4
0
 public function get($key)
 {
     if (!isset($this->data[$key])) {
         return Promise\reject();
     }
     return Promise\resolve($this->data[$key]);
 }
Ejemplo n.º 5
0
 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'));
 }
Ejemplo n.º 6
0
 /** @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);
 }
Ejemplo n.º 8
0
 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));
 }
Ejemplo n.º 9
0
 /** @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));
 }
Ejemplo n.º 10
0
 /** @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);
 }
Ejemplo n.º 11
0
 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);
 }
Ejemplo n.º 12
0
 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);
 }
Ejemplo n.º 13
0
 /** @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());
 }
Ejemplo n.º 14
0
 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);
     });
 }
Ejemplo n.º 15
0
 /**
  * @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);
     }
 }
Ejemplo n.º 16
0
 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);
 }
Ejemplo n.º 18
0
 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();
     });
 }
Ejemplo n.º 19
0
 /**
  * @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();
     });
 }
Ejemplo n.º 20
0
 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();
     });
 }
Ejemplo n.º 21
0
 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();
     });
 }
Ejemplo n.º 22
0
 /**
  * @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);
 }
Ejemplo n.º 25
0
 /**
  * @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);
 }
Ejemplo n.º 27
0
 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());
 }
Ejemplo n.º 28
0
 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);
 }
Ejemplo n.º 29
0
 /**
  * @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;
 }