e4b20bd840c9f37e778718c3c9c0cb9380b36a75
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / LayoutBuilderRoutesTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
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;
14
15 /**
16  * @coversDefaultClass \Drupal\layout_builder\Routing\LayoutBuilderRoutes
17  *
18  * @group layout_builder
19  */
20 class LayoutBuilderRoutesTest extends UnitTestCase {
21
22   /**
23    * The Layout Builder route builder.
24    *
25    * @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
26    */
27   protected $sectionStorageManager;
28
29   /**
30    * The Layout Builder route builder.
31    *
32    * @var \Drupal\layout_builder\Routing\LayoutBuilderRoutes
33    */
34   protected $routeBuilder;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     $this->sectionStorageManager = $this->prophesize(SectionStorageManagerInterface::class);
43     $this->routeBuilder = new LayoutBuilderRoutes($this->sectionStorageManager->reveal());
44   }
45
46   /**
47    * @covers ::onAlterRoutes
48    */
49   public function testOnAlterRoutes() {
50     $expected = [
51       'test_route1' => new Route('/test/path1'),
52       'test_route_shared' => new Route('/test/path/shared2'),
53       'test_route2' => new Route('/test/path2'),
54     ];
55
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'));
62     });
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'));
69     });
70
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);
76
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()));
82   }
83
84 }