function LayoutBuilderRoutesTest::testOnAlterRoutes

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php \Drupal\Tests\layout_builder\Unit\LayoutBuilderRoutesTest::testOnAlterRoutes()
  2. 10 core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php \Drupal\Tests\layout_builder\Unit\LayoutBuilderRoutesTest::testOnAlterRoutes()
  3. 11.x core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php \Drupal\Tests\layout_builder\Unit\LayoutBuilderRoutesTest::testOnAlterRoutes()

@covers ::onAlterRoutes

File

core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php, line 49

Class

LayoutBuilderRoutesTest
@coversDefaultClass \Drupal\layout_builder\Routing\LayoutBuilderRoutes

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testOnAlterRoutes() {
    $expected = [
        'test_route1' => new Route('/test/path1'),
        'test_route_shared' => new Route('/test/path/shared2'),
        'test_route2' => new Route('/test/path2'),
    ];
    $section_storage_first = $this->prophesize(SectionStorageInterface::class);
    $section_storage_first->buildRoutes(Argument::type(RouteCollection::class))
        ->shouldBeCalled()
        ->will(function ($args) {
        
        /** @var \Symfony\Component\Routing\RouteCollection $collection */
        $collection = $args[0];
        $collection->add('test_route_shared', new Route('/test/path/shared1'));
        $collection->add('test_route1', new Route('/test/path1'));
    });
    $section_storage_second = $this->prophesize(SectionStorageInterface::class);
    $section_storage_second->buildRoutes(Argument::type(RouteCollection::class))
        ->shouldBeCalled()
        ->will(function ($args) {
        
        /** @var \Symfony\Component\Routing\RouteCollection $collection */
        $collection = $args[0];
        $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()));
}

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