3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\Core\Routing\RouteBuildEvent;
6 use Drupal\layout_builder\Routing\LayoutBuilderRoutes;
7 use Drupal\layout_builder\SectionStorage\SectionStorageDefinition;
8 use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
9 use Drupal\layout_builder\SectionStorageInterface;
10 use Drupal\Tests\UnitTestCase;
11 use Prophecy\Argument;
12 use Symfony\Component\Routing\Route;
13 use Symfony\Component\Routing\RouteCollection;
16 * @coversDefaultClass \Drupal\layout_builder\Routing\LayoutBuilderRoutes
18 * @group layout_builder
20 class LayoutBuilderRoutesTest extends UnitTestCase {
23 * The Layout Builder route builder.
25 * @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
27 protected $sectionStorageManager;
30 * The Layout Builder route builder.
32 * @var \Drupal\layout_builder\Routing\LayoutBuilderRoutes
34 protected $routeBuilder;
39 protected function setUp() {
42 $this->sectionStorageManager = $this->prophesize(SectionStorageManagerInterface::class);
43 $this->routeBuilder = new LayoutBuilderRoutes($this->sectionStorageManager->reveal());
47 * @covers ::onAlterRoutes
49 public function testOnAlterRoutes() {
51 'test_route1' => new Route('/test/path1'),
52 'test_route_shared' => new Route('/test/path/shared2'),
53 'test_route2' => new Route('/test/path2'),
56 $section_storage_first = $this->prophesize(SectionStorageInterface::class);
57 $section_storage_first->buildRoutes(Argument::type(RouteCollection::class))->shouldBeCalled()->will(function ($args) {
58 /** @var \Symfony\Component\Routing\RouteCollection $collection */
59 $collection = $args[0];
60 $collection->add('test_route_shared', new Route('/test/path/shared1'));
61 $collection->add('test_route1', new Route('/test/path1'));
63 $section_storage_second = $this->prophesize(SectionStorageInterface::class);
64 $section_storage_second->buildRoutes(Argument::type(RouteCollection::class))->shouldBeCalled()->will(function ($args) {
65 /** @var \Symfony\Component\Routing\RouteCollection $collection */
66 $collection = $args[0];
67 $collection->add('test_route_shared', new Route('/test/path/shared2'));
68 $collection->add('test_route2', new Route('/test/path2'));
71 $this->sectionStorageManager->loadEmpty('first')->willReturn($section_storage_first->reveal());
72 $this->sectionStorageManager->loadEmpty('second')->willReturn($section_storage_second->reveal());
73 $definitions['first'] = new SectionStorageDefinition();
74 $definitions['second'] = new SectionStorageDefinition();
75 $this->sectionStorageManager->getDefinitions()->willReturn($definitions);
77 $collection = new RouteCollection();
78 $event = new RouteBuildEvent($collection);
79 $this->routeBuilder->onAlterRoutes($event);
80 $this->assertEquals($expected, $collection->all());
81 $this->assertSame(array_keys($expected), array_keys($collection->all()));