getRouteCollectionForRequest example

$route_collection = new RouteCollection();

    $route = new Route('/user/{user}');
    $route->setOption('compiler_class', RouteCompiler::class);
    $route_collection->add('user_view', $route);

    $route = new Route('/user/login');
    $route->setOption('compiler_class', RouteCompiler::class);
    $route_collection->add('user_login', $route);

    $route_provider->getRouteCollectionForRequest(Argument::any())
      ->willReturn($route_collection);

    $url_generator = $this->prophesize(UrlGeneratorInterface::class);
    $current_path_stack = $this->prophesize(CurrentPathStack::class);
    $router = new Router($route_provider->reveal()$current_path_stack->reveal()$url_generator->reveal());

    $request_context = $this->createMock(RequestContext::class);
    $request_context->expects($this->any())
      ->method('getScheme')
      ->willReturn('http');
    $router->setContext($request_context);

    
$this->fixtures->createTables($connection);

    $dumper = new MatcherDumper($connection$this->state, $this->logger, 'test_routes');
    $dumper->addRoutes($this->fixtures->sampleRouteCollection());
    $dumper->dump();

    $path = '/path/one';

    $request = Request::create($path, 'GET');

    $routes = $provider->getRouteCollectionForRequest($request);

    foreach ($routes as $route) {
      $this->assertEquals($path$route->getPath(), 'Found path has correct pattern');
    }
  }

  /** * Confirms that we can find routes whose pattern would match the request. */
  public function testOutlinePathMatch() {
    $connection = Database::getConnection();
    
protected function getRouteProvider() {
    if (!$this->rebuilt && !$this->rebuilding) {
      $this->routeBuilder->rebuild();
    }
    return $this->routeProvider;
  }

  /** * {@inheritdoc} */
  public function getRouteCollectionForRequest(Request $request) {
    return $this->getRouteProvider()->getRouteCollectionForRequest($request);
  }

  /** * {@inheritdoc} */
  public function getRouteByName($name) {
    return $this->getRouteProvider()->getRouteByName($name);
  }

  /** * {@inheritdoc} */
$this->service = $container->get('test.router.route_provider');
      $container->get('router.builder')->rebuild();
    }

    return $this->service;
  }

  /** * {@inheritdoc} */
  public function getRouteCollectionForRequest(Request $request) {
    return $this->lazyLoadItself()->getRouteCollectionForRequest($request);
  }

  /** * {@inheritdoc} */
  public function getRouteByName($name) {
    return $this->lazyLoadItself()->getRouteByName($name);
  }

  /** * {@inheritdoc} */

class OptionsRequestSubscriberTest extends UnitTestCase {

  /** * @covers ::onRequest */
  public function testWithNonOptionRequest() {
    $kernel = $this->prophesize(HttpKernelInterface::class);
    $request = Request::create('/example', 'GET');

    $route_provider = $this->prophesize(RouteProviderInterface::class);
    $route_provider->getRouteCollectionForRequest($request)->shouldNotBeCalled();

    $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
    $event = new RequestEvent($kernel->reveal()$request, HttpKernelInterface::MAIN_REQUEST);
    $subscriber->onRequest($event);

    $this->assertFalse($event->hasResponse());
  }

  /** * @covers ::onRequest */
  
/** * Returns a collection of potential matching routes for a request. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * * @return \Symfony\Component\Routing\RouteCollection * The initial fetched route collection. */
  protected function getInitialRouteCollection(Request $request) {
    return $this->routeProvider->getRouteCollectionForRequest($request);
  }

  /** * Apply the route enhancers to the defaults, according to priorities. * * @param array $defaults * The defaults coming from the final matched route. * @param \Symfony\Component\HttpFoundation\Request $request * The request. * * @return array * The request attributes after applying the enhancers. This might consist * raw values from the URL but also upcasted values, like entity objects, * from route enhancers. */
$this->routeProvider = $route_provider;
  }

  /** * Tries to handle the options request. * * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event * The request event. */
  public function onRequest(RequestEvent $event) {
    if ($event->getRequest()->isMethod('OPTIONS')) {
      $routes = $this->routeProvider->getRouteCollectionForRequest($event->getRequest());
      // In case we don't have any routes, a 403 should be thrown by the normal       // request handling.       if (count($routes) > 0) {
        // Flatten and unique the available methods.         $methods = array_reduce($routes->all()function D$methods, Route $route) {
          return array_merge($methods$route->getMethods());
        }[]);
        $methods = array_unique($methods);
        $response = new Response('', 200, ['Allow' => implode(', ', $methods)]);
        $event->setResponse($response);
      }
    }
Home | Imprint | This part of the site doesn't use cookies.