class RouteBuilderTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php \Drupal\Tests\Core\Routing\RouteBuilderTest
  2. 10 core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php \Drupal\Tests\Core\Routing\RouteBuilderTest
  3. 11.x core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php \Drupal\Tests\Core\Routing\RouteBuilderTest

@coversDefaultClass \Drupal\Core\Routing\RouteBuilder @group Routing

Hierarchy

Expanded class hierarchy of RouteBuilderTest

File

core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php, line 24

Namespace

Drupal\Tests\Core\Routing
View source
class RouteBuilderTest extends UnitTestCase {
    
    /**
     * The actual tested route builder.
     *
     * @var \Drupal\Core\Routing\RouteBuilder
     */
    protected $routeBuilder;
    
    /**
     * The mocked matcher dumper.
     *
     * @var \Drupal\Core\Routing\MatcherDumperInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $dumper;
    
    /**
     * The mocked lock backend.
     *
     * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $lock;
    
    /**
     * The mocked event dispatcher.
     *
     * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $dispatcher;
    
    /**
     * The mocked YAML discovery.
     *
     * @var \Drupal\Core\Discovery\YamlDiscovery|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $yamlDiscovery;
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * The controller resolver.
     *
     * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $controllerResolver;
    
    /**
     * @var \Drupal\Core\Access\CheckProviderInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $checkProvider;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->dumper = $this->createMock('Drupal\\Core\\Routing\\MatcherDumperInterface');
        $this->lock = $this->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
        $this->dispatcher = $this->createMock('\\Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface');
        $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
        $this->controllerResolver = $this->createMock('Drupal\\Core\\Controller\\ControllerResolverInterface');
        $this->yamlDiscovery = $this->getMockBuilder('\\Drupal\\Core\\Discovery\\YamlDiscovery')
            ->disableOriginalConstructor()
            ->getMock();
        $this->checkProvider = $this->createMock('\\Drupal\\Core\\Access\\CheckProviderInterface');
        $this->routeBuilder = new TestRouteBuilder($this->dumper, $this->lock, $this->dispatcher, $this->moduleHandler, $this->controllerResolver, $this->checkProvider);
        $this->routeBuilder
            ->setYamlDiscovery($this->yamlDiscovery);
    }
    
    /**
     * Tests that the route rebuilding both locks and unlocks.
     */
    public function testRebuildLockingUnlocking() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(TRUE);
        $this->lock
            ->expects($this->once())
            ->method('release')
            ->with('router_rebuild');
        $this->yamlDiscovery
            ->expects($this->any())
            ->method('findAll')
            ->willReturn([]);
        $this->assertTrue($this->routeBuilder
            ->rebuild());
    }
    
    /**
     * Tests route rebuilding with a blocking lock.
     */
    public function testRebuildBlockingLock() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(FALSE);
        $this->lock
            ->expects($this->once())
            ->method('wait')
            ->with('router_rebuild');
        $this->lock
            ->expects($this->never())
            ->method('release');
        $this->yamlDiscovery
            ->expects($this->never())
            ->method('findAll');
        $this->assertFalse($this->routeBuilder
            ->rebuild());
    }
    
    /**
     * Tests that provided routes by a module is put into the dumper/dispatcher.
     *
     * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
     */
    public function testRebuildWithStaticModuleRoutes() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(TRUE);
        $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())
            ->method('setChecks')
            ->with($route_collection);
        // Ensure that the routes are set to the dumper and dumped.
        $this->dumper
            ->expects($this->once())
            ->method('addRoutes')
            ->with($route_collection);
        $this->dumper
            ->expects($this->once())
            ->method('dump')
            ->with();
        $this->assertTrue($this->routeBuilder
            ->rebuild());
    }
    
    /**
     * Tests the rebuild with routes provided by a callback.
     *
     * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
     */
    public function testRebuildWithProviderBasedRoutes() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(TRUE);
        $this->yamlDiscovery
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([
            'test_module' => [
                'route_callbacks' => [
                    '\\Drupal\\Tests\\Core\\Routing\\TestRouteSubscriber::routesFromArray',
                    'test_module.route_service:routesFromCollection',
                ],
            ],
        ]);
        $container = new ContainerBuilder();
        $container->set('test_module.route_service', new TestRouteSubscriber());
        $this->controllerResolver
            ->expects($this->any())
            ->method('getControllerFromDefinition')
            ->willReturnCallback(function ($controller) use ($container) {
            $count = substr_count($controller, ':');
            if ($count == 1) {
                [
                    $service,
                    $method,
                ] = explode(':', $controller, 2);
                $object = $container->get($service);
            }
            else {
                [
                    $class,
                    $method,
                ] = explode('::', $controller, 2);
                $object = new $class();
            }
            return [
                $object,
                $method,
            ];
        });
        $route_collection_filled = new RouteCollection();
        $route_collection_filled->add('test_route.1', new Route('/test-route/1'));
        $route_collection_filled->add('test_route.2', new Route('/test-route/2'));
        $route_build_event = new RouteBuildEvent($route_collection_filled);
        // 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())
            ->method('setChecks')
            ->with($route_collection_filled);
        // Ensure that the routes are set to the dumper and dumped.
        $this->dumper
            ->expects($this->once())
            ->method('addRoutes')
            ->with($route_collection_filled);
        $this->dumper
            ->expects($this->once())
            ->method('dump');
        $this->assertTrue($this->routeBuilder
            ->rebuild());
    }
    
    /**
     * Tests \Drupal\Core\Routing\RouteBuilder::rebuildIfNeeded() method.
     */
    public function testRebuildIfNeeded() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(TRUE);
        $this->lock
            ->expects($this->once())
            ->method('release')
            ->with('router_rebuild');
        $this->yamlDiscovery
            ->expects($this->any())
            ->method('findAll')
            ->willReturn([]);
        $this->routeBuilder
            ->setRebuildNeeded();
        // This will trigger a successful rebuild.
        $this->assertTrue($this->routeBuilder
            ->rebuildIfNeeded());
        // This will not trigger a rebuild.
        $this->assertFalse($this->routeBuilder
            ->rebuildIfNeeded());
    }
    
    /**
     * Tests routes can use alternative compiler classes.
     *
     * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
     */
    public function testRebuildWithOverriddenRouteClass() {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('router_rebuild')
            ->willReturn(TRUE);
        $this->yamlDiscovery
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([
            'test_module' => [
                'test_route.override' => [
                    'path' => '/test_route_override',
                    'options' => [
                        'compiler_class' => 'Class\\Does\\Not\\Exist',
                    ],
                ],
                'test_route' => [
                    'path' => '/test_route',
                ],
            ],
        ]);
        $container = new ContainerBuilder();
        $container->set('test_module.route_service', new TestRouteSubscriber());
        // Test that routes can have alternative compiler classes.
        $route_collection_filled = new RouteCollection();
        $route_collection_filled->add('test_route.override', new Route('/test_route_override', [], [], [
            'compiler_class' => 'Class\\Does\\Not\\Exist',
        ]));
        $route_collection_filled->add('test_route', new Route('/test_route', [], [], [
            'compiler_class' => RouteCompiler::class,
        ]));
        $route_build_event = new RouteBuildEvent($route_collection_filled);
        $this->dispatcher
            ->expects($this->atLeast(2))
            ->method('dispatch')
            ->withConsecutive([
            $route_build_event,
            RoutingEvents::DYNAMIC,
        ], [
            $route_build_event,
            RoutingEvents::ALTER,
        ]);
        $this->assertTrue($this->routeBuilder
            ->rebuild());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RouteBuilderTest::$checkProvider protected property
RouteBuilderTest::$controllerResolver protected property The controller resolver.
RouteBuilderTest::$dispatcher protected property The mocked event dispatcher.
RouteBuilderTest::$dumper protected property The mocked matcher dumper.
RouteBuilderTest::$lock protected property The mocked lock backend.
RouteBuilderTest::$moduleHandler protected property The module handler.
RouteBuilderTest::$routeBuilder protected property The actual tested route builder.
RouteBuilderTest::$yamlDiscovery protected property The mocked YAML discovery.
RouteBuilderTest::setUp protected function Overrides UnitTestCase::setUp
RouteBuilderTest::testRebuildBlockingLock public function Tests route rebuilding with a blocking lock.
RouteBuilderTest::testRebuildIfNeeded public function Tests \Drupal\Core\Routing\RouteBuilder::rebuildIfNeeded() method.
RouteBuilderTest::testRebuildLockingUnlocking public function Tests that the route rebuilding both locks and unlocks.
RouteBuilderTest::testRebuildWithOverriddenRouteClass public function Tests routes can use alternative compiler classes.
RouteBuilderTest::testRebuildWithProviderBasedRoutes public function Tests the rebuild with routes provided by a callback.
RouteBuilderTest::testRebuildWithStaticModuleRoutes public function Tests that provided routes by a module is put into the dumper/dispatcher.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.