RouteBuildEvent example

public function testSubscribing() {

    // Ensure that onRouteFinished can be called without throwing notices     // when no path roots got set.     $this->pathRootsSubscriber->onRouteFinished();

    $route_collection = new RouteCollection();
    $route_collection->add('test_route1', new Route('/test/bar'));
    $route_collection->add('test_route2', new Route('/test/baz'));
    $route_collection->add('test_route3', new Route('/test2/bar/baz'));

    $event = new RouteBuildEvent($route_collection);
    $this->pathRootsSubscriber->onRouteAlter($event);

    $route_collection = new RouteCollection();
    $route_collection->add('test_route4', new Route('/test1/bar'));
    $route_collection->add('test_route5', new Route('/test2/baz'));
    $route_collection->add('test_route6', new Route('/test2/bar/baz'));

    $event = new RouteBuildEvent($route_collection);
    $this->pathRootsSubscriber->onRouteAlter($event);

    $this->state->expects($this->once())
      
$collection->add('test_route_shared', new Route('/test/path/shared2'));
      $collection->add('test_route2', new Route('/test/path2'));
    });

    $this->sectionStorageManager->loadEmpty('first')->willReturn($section_storage_first->reveal());
    $this->sectionStorageManager->loadEmpty('second')->willReturn($section_storage_second->reveal());
    $definitions['first'] = new SectionStorageDefinition();
    $definitions['second'] = new SectionStorageDefinition();
    $this->sectionStorageManager->getDefinitions()->willReturn($definitions);

    $collection = new RouteCollection();
    $event = new RouteBuildEvent($collection);
    $this->routeBuilder->onAlterRoutes($event);
    $this->assertEquals($expected$collection->all());
    $this->assertSame(array_keys($expected)array_keys($collection->all()));
  }

}
        $route_info['options'] += [
          'compiler_class' => RouteCompiler::class,
        ];

        $route = new Route($route_info['path']$route_info['defaults']$route_info['requirements']$route_info['options']$route_info['host']$route_info['schemes']$route_info['methods']$route_info['condition']);
        $collection->add($name$route);
      }
    }

    // DYNAMIC is supposed to be used to add new routes based upon all the     // static defined ones.     $this->dispatcher->dispatch(new RouteBuildEvent($collection), RoutingEvents::DYNAMIC);

    // ALTER is the final step to alter all the existing routes. We cannot stop     // people from adding new routes here, but we define two separate steps to     // make it clear.     $this->dispatcher->dispatch(new RouteBuildEvent($collection), RoutingEvents::ALTER);

    $this->checkProvider->setChecks($collection);

    $this->dumper->addRoutes($collection);
    $this->dumper->dump();

    
$routing_fixtures = new RoutingFixtures();
    $routes = $routing_fixtures->staticSampleRouteCollection();

    $this->yamlDiscovery->expects($this->once())
      ->method('findAll')
      ->willReturn(['test_module' => $routes]);

    $route_collection = $routing_fixtures->sampleRouteCollection();
    foreach ($route_collection->all() as $route) {
      $route->setOption('compiler_class', RouteCompiler::class);
    }
    $route_build_event = new RouteBuildEvent($route_collection);

    // Ensure that the alter routes events are fired.     $this->dispatcher->expects($this->atLeast(2))
      ->method('dispatch')
      ->withConsecutive(
        [$route_build_event, RoutingEvents::DYNAMIC],
        [$route_build_event, RoutingEvents::ALTER],
      );

    // Ensure that access checks are set.     $this->checkProvider->expects($this->once())
      

  public function testOnAlterRoutes() {
    $collection = new RouteCollection();
    // The first route will be overridden later.     $collection->add('test_route', new Route('test_route', ['_controller' => 'Drupal\Tests\Core\Controller\TestController']));
    $route_2 = new Route('test_route/example', ['_controller' => 'Drupal\Tests\Core\Controller\TestController']);
    $collection->add('test_route_2', $route_2);

    $route_event = new RouteBuildEvent($collection);

    [$display_1$display_2] = $this->setupMocks();

    // The page_1 display overrides an existing route, so the dynamicRoutes     // should only call the second display.     $display_1->expects($this->once())
      ->method('collectRoutes')
      ->willReturnCallback(function D) use ($collection) {
        $collection->add('views.test_id.page_1', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController']));
        return ['test_id.page_1' => 'views.test_id.page_1'];
      });
    

  public function testSetLatestRevisionFlag($defaults$parameters$expected_parameters = FALSE) {
    $route = new Route('/foo/{entity_test}', $defaults[][
      'parameters' => $parameters,
    ]);

    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);
    $event = new RouteBuildEvent($route_collection);
    $this->routeSubscriber->onAlterRoutes($event);

    // If expected parameters have not been provided, assert they are unchanged.     $this->assertEquals($expected_parameters ?: $parameters$route->getOption('parameters'));
  }

}

/** * A concrete entity. */

  protected function childRoutes(EntityTypeInterface $entity_type): array {
    $route_names = [];

    if ($field_ui_base_route = $entity_type->get('field_ui_base_route')) {
      $updated_routes = new RouteCollection();
      $updated_routes->add($field_ui_base_route$this->collection->get($field_ui_base_route));
      $event = new RouteBuildEvent($updated_routes);

      // Apply route subscribers that add routes based on field_ui_base_route,       // in the order of their weights.       $subscribers = [
        'field_ui' => 'field_ui.subscriber',
        'content_translation' => 'content_translation.subscriber',
      ];
      foreach ($subscribers as $module_name => $service_name) {
        if ($this->moduleHandler->moduleExists($module_name)) {
          \Drupal::service($service_name)->onAlterRoutes($event);
        }
      }

  public function testOnRouteBuildingValidVariables(Route $route) {
    $route_collection = new RouteCollection();
    $route_collection->add('test', $route);

    $event = new RouteBuildEvent($route_collection);
    $subscriber = new SpecialAttributesRouteSubscriber();
    $this->assertNull($subscriber->onAlterRoutes($event));
  }

  /** * Tests the onAlterRoutes method for invalid variables. * * @param \Symfony\Component\Routing\Route $route * The route to check. * * @dataProvider providerTestOnRouteBuildingInvalidVariables * @covers ::onAlterRoutes */
class AdminRouteSubscriberTest extends UnitTestCase {

  /** * @covers ::alterRoutes * @covers ::isHtmlRoute * * @dataProvider providerTestAlterRoutes */
  public function testAlterRoutes(Route $route$is_admin) {
    $collection = new RouteCollection();
    $collection->add('the_route', $route);
    (new AdminRouteSubscriber())->onAlterRoutes(new RouteBuildEvent($collection));

    $this->assertSame($is_admin$route->getOption('_admin_route'));
  }

  public function providerTestAlterRoutes() {
    $data = [];
    $data['non-admin'] = [
      new Route('/foo'),
      NULL,
    ];
    $data['admin prefix'] = [
      

  public function testRemoveRoute($route_name, array $requirements$removed) {
    $collection = new RouteCollection();
    $route = new Route('', []$requirements);
    $collection->add($route_name$route);

    $event = new RouteBuildEvent($collection);
    $route_subscriber = new ModuleRouteSubscriber($this->moduleHandler);
    $route_subscriber->onAlterRoutes($event);

    if ($removed) {
      $this->assertNull($collection->get($route_name));
    }
    else {
      $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get($route_name));
    }
  }

  
Home | Imprint | This part of the site doesn't use cookies.