fromGlobals() public static method

If any argument is not supplied, the corresponding superglobal value will be used. The ServerRequest created is then passed to the fromServer() method in order to marshal the request URI and headers.
See also: fromServer()
public static fromGlobals ( array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null ) : ServerRequest
$server array $_SERVER superglobal
$query array $_GET superglobal
$body array $_POST superglobal
$cookies array $_COOKIE superglobal
$files array $_FILES superglobal
return ServerRequest
Ejemplo n.º 1
0
 /**
  * @test
  */
 public function datesAreReadFromQuery()
 {
     $shortCode = 'abc123';
     $this->visitsTracker->info($shortCode, new DateRange(null, new \DateTime('2016-01-01 00:00:00')))->willReturn([])->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)->withQueryParams(['endDate' => '2016-01-01 00:00:00']), new Response());
     $this->assertEquals(200, $response->getStatusCode());
 }
Ejemplo n.º 2
0
 /**
  * Get the request from server.
  *
  * @return RequestInterface Server request
  */
 public function getRequest()
 {
     if (!isset($this->request)) {
         $this->request = ServerRequestFactory::fromGlobals();
     }
     return $this->request;
 }
 /**
  * Get Request
  *
  * @param array $routeVariables Route Variables
  *
  * @return HttpMessages_CraftRequest Request
  */
 public function getRequest(array $routeVariables = [])
 {
     $route = craft()->httpMessages_routes->getRoute($routeVariables);
     $serverRequest = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
     $request = HttpMessages_RequestFactory::fromRequest($serverRequest);
     return $request->withRoute($route);
 }
Ejemplo n.º 4
0
 /**
  * @test
  */
 public function anExceptionsReturnsErrorResponse()
 {
     $page = 3;
     $this->service->listShortUrls($page, null, [], null)->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withQueryParams(['page' => $page]), new Response());
     $this->assertEquals(500, $response->getStatusCode());
 }
Ejemplo n.º 5
0
 public function testOversizedResponse()
 {
     $expectedClass = 'NetworkJs\\Response\\OversizedResponse';
     $moduleName = 'download';
     $this->assertInstanceOf($expectedClass, ResponseFactory::fromValues($moduleName, 10, 5));
     $this->assertInstanceOf($expectedClass, ResponseFactory::fromRequest(ServerRequestFactory::fromGlobals(null, ['module' => $moduleName, 'size' => 10]), 5));
 }
Ejemplo n.º 6
0
 public function testIsNotExcluded()
 {
     $request = ServerRequestFactory::fromGlobals();
     $request = $request->withHeader('X-Forwarded', '80.80.80.80');
     $excluder = new IP($this->excludedIPs, ['10.10.10.10']);
     self::assertFalse($excluder->isExcluded($request));
 }
Ejemplo n.º 7
0
 /**
  * Handles the request and returns a response.
  *
  * @param  \Psr\Http\Message\ServerRequestInterface|null $request
  * @param  \Psr\Http\Message\ResponseInterface|null      $response
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function handle(ServerRequestInterface $request = null, ResponseInterface $response = null)
 {
     $request = $request ?: ServerRequestFactory::fromGlobals();
     $response = $response ?: new Response();
     $this->emit(Event::REQUEST_RECEIVED, $request, $response);
     return $this->dispatchThroughMiddleware($request, $response);
 }
Ejemplo n.º 8
0
 /**
  * @test
  */
 public function invalidApiKeyReturnsErrorResponse()
 {
     $this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setEnabled(false))->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withParsedBody(['apiKey' => 'foo']);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(401, $response->getStatusCode());
 }
Ejemplo n.º 9
0
 /**
  * @test
  */
 public function tagsListIsReturnedIfCorrectShortCodeIsProvided()
 {
     $shortCode = 'abc123';
     $this->shortUrlService->setTagsByShortCode($shortCode, [])->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')->withParsedBody(['tags' => []]), new Response());
     $this->assertEquals(200, $response->getStatusCode());
 }
Ejemplo n.º 10
0
 public static function createRequest(RequestInterface $request)
 {
     $psrRequest = ServerRequestFactory::fromGlobals()->withUri($request->getUri())->withMethod($request->getMethod())->withBody($request->getBody());
     foreach ($request->getHeaders() as $name => $values) {
         $psrRequest = $psrRequest->withHeader($name, $values);
     }
     return $psrRequest;
 }
Ejemplo n.º 11
0
 /**
  * @test
  */
 public function to_applies_filters()
 {
     $applied = false;
     $this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $response) use(&$applied) {
         $applied = true;
     })->to('http://www.example.com');
     $this->assertTrue($applied);
 }
Ejemplo n.º 12
0
 /**
  * @test
  */
 public function aGenericExceptionWillReturnError()
 {
     $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'))->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withParsedBody(['longUrl' => 'http://www.domain.com/foo/bar']);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(500, $response->getStatusCode());
     $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
 }
Ejemplo n.º 13
0
 /**
  *
  */
 public function __construct()
 {
     parent::__construct();
     $this->_middleware = new \ArrayObject();
     $this->_routeMap = new RouteMap();
     $this->_request = ServerRequestFactory::fromGlobals();
     $this->_response = new Response();
 }
Ejemplo n.º 14
0
 public function testNamedRoutes()
 {
     $routes = new RouteCollection(new HttpMessageStrategy());
     $routes->addRoute('GET', '/user/{id}', function () {
     })->setName('UserProfile');
     $builder = new UrlBuilder(ServerRequestFactory::fromGlobals($this->fakeServerParams()), $routes);
     $this->assertEquals('/site/user/23', $builder->createByName('UserProfile', ['id' => 23]));
 }
Ejemplo n.º 15
0
 /**
  * @test
  */
 public function nonRestPathsAreNotProcessed()
 {
     $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/non-rest'));
     $test = $this;
     $this->middleware->__invoke($request, new Response(), function ($req) use($request, $test) {
         $test->assertSame($request, $req);
     });
 }
Ejemplo n.º 16
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     $this->request = ServerRequestFactory::fromGlobals();
     $this->response = new Response();
     $this->callback = function ($request, $response) {
         return $response;
     };
 }
Ejemplo n.º 17
0
 /**
  * Initial setup of DI\ContainerBuilder.
  *
  * @param bool $annotation annotation usage.
  *
  * @return DI\ContainerBuilder
  */
 protected static function initialSetupContainerBuilder($annotation)
 {
     $builder = new DI\ContainerBuilder();
     $builder->useAnnotations($annotation);
     $builder->addDefinitions(['request' => ServerRequestFactory::fromGlobals(), 'response' => DI\object(Response::class), 'http_flow_event' => DI\object(ZendHttpFlowEvent::class)->constructor('bootstrap', DI\get('request'), DI\get('response')), 'event_manager' => DI\object(ZendEvmProxy::class), 'dispatcher' => DI\factory(function ($container) {
         return new Dispatcher($container->get('router'), $container);
     })]);
     return $builder;
 }
 /**
  * Test missing routes not being caught.
  *
  * @expectedException \Cake\Routing\Exception\MissingRouteException
  */
 public function testMissingRouteNotCaught()
 {
     $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/missing']);
     $response = new Response();
     $next = function ($req, $res) {
     };
     $middleware = new RoutingMiddleware();
     $middleware($request, $response, $next);
 }
Ejemplo n.º 19
0
 /**
  * @test
  */
 public function unexpectedExceptionWillReturnError()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(500, $response->getStatusCode());
     $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
 }
Ejemplo n.º 20
0
 public function testPostRequestSavesPostDataInSessionAndReturnsRedirect()
 {
     $request = ServerRequestFactory::fromGlobals(null, null, $this->fullData)->withMethod('POST')->withUri(new Uri('/foo/bar'));
     $this->assertFalse($this->session->offsetExists(Contact::PRG_DATA));
     $resp = $this->contact->dispatch($request, new Response());
     $this->assertInstanceOf(RedirectResponse::class, $resp);
     $this->assertEquals(['/foo/bar'], $resp->getHeader('Location'));
     $this->assertTrue($this->session->offsetExists(Contact::PRG_DATA));
 }
Ejemplo n.º 21
0
 public function testWithoutSuffix()
 {
     $request = ServerRequestFactory::fromGlobals();
     $response = new Response();
     $rtime = new ResponseTime(['suffix' => false]);
     $response = $rtime($request, $response);
     $this->assertInstanceOf(Response::class, $response);
     $this->assertNotContains('ms', $response->getHeaders()['X-Response-Time'][0]);
 }
Ejemplo n.º 22
0
 public function register()
 {
     $this->forge->singleton(['routing', Routing::class], function () {
         $routing = new Routing(new HttpRequest(ServerRequestFactory::fromGlobals()), new HttpResponse(new Response()));
         $routing->makeDispatcher(HTTP . "routes.php");
         return $routing;
     });
     $this->provides += [Routing::class];
 }
Ejemplo n.º 23
0
 /**
  * Use the register method to register items with the container via the
  * protected $this->container property or the `getContainer` method
  * from the ContainerAwareTrait.
  */
 public function register()
 {
     $this->container->share(ServerRequestInterface::class, function () {
         return ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
     });
     $this->container->share(ResponseInterface::class, function () {
         return new Response();
     });
 }
Ejemplo n.º 24
0
 /**
  * @test
  */
 public function previewExceptionReturnsNotFound()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(PreviewGenerationException::class)->shouldBeCalledTimes(1);
     $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertEquals(500, $resp->getStatusCode());
 }
Ejemplo n.º 25
0
 /**
  * @test
  */
 public function aCorrectRequestReturnsTheQrCodeResponse()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
     $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertInstanceOf(QrCodeResponse::class, $resp);
     $this->assertEquals(200, $resp->getStatusCode());
 }
Ejemplo n.º 26
0
 public function __construct($request = null, $responseHeaderSender = null, $responseBody = 'php://output')
 {
     $this->request = $request ? $request : ServerRequestFactory::fromGlobals();
     $this->responseBody = $responseBody;
     $this->responseHeaderSender = $responseHeaderSender ? $responseHeaderSender : [new ResponseSender(), 'emitStatusLineAndHeaders'];
     $this->client = new Client($this->request);
     $parsedbody = $this->request->getParsedBody() ? $this->request->getParsedBody() : [];
     $this->params = $this->request->getCookieParams() + $parsedbody + $this->request->getQueryParams();
     $this->reply = new Reply();
 }
Ejemplo n.º 27
0
 /**
  * Constructor. In most cases you won't need to worry about the constructor
  * arguments, but optionally you can pass a path part all routes _must_
  * match (e.g. if Reroute only needs to catch parts of your project).
  *
  * @param string $url The path part _all_ URLs for this router must fall
  *  under in order to match.
  * @param League\Pipeline\Pipeline $pipe Optional pipeline to chain onto.
  * @return void
  */
 public function __construct($url = null, Pipeline $pipe = null)
 {
     $this->request = ServerRequestFactory::fromGlobals();
     $this->url = $this->normalize($url);
     $this->pipeline = new PipelineBuilder();
     if (isset($pipe)) {
         $this->pipeline->add($pipe);
     }
     self::$matchedArguments = [];
 }
 /**
  * {@inheritDoc}
  */
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null)
 {
     $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
     list($base, $webroot) = static::getBase($request);
     $request = $request->withAttribute('base', $base)->withAttribute('webroot', $webroot);
     if ($base) {
         $request = static::updatePath($base, $request);
     }
     return $request;
 }
Ejemplo n.º 29
0
 /**
  * @return array|Definition[]
  */
 public function getDefinitions() : array
 {
     return [RequestContract::class => function () {
         return new Request(ServerRequestFactory::fromGlobals());
     }, ResponseContract::class => function () {
         return new Response(new DiactorosResponse());
     }, ResponseFactoryContract::class => function () {
         return new ResponseFactory();
     }];
 }
Ejemplo n.º 30
0
 public function testRedirectScheduled()
 {
     $watcher = $this->getMockBuilder(ScheduledWatcherInterface::class)->disableOriginalConstructor()->getMock();
     $watcher->expects(self::once())->method('getEnd')->will(self::returnValue(new \DateTime()));
     $handler = new Redirect('http://example.com');
     /* @var \Psr\Http\Message\ResponseInterface $response */
     $response = $handler(ServerRequestFactory::fromGlobals(), new Response('php://temp'), $watcher);
     self::assertEquals(302, $response->getStatusCode());
     self::assertTrue($response->hasHeader('Expires'));
 }