3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\PathRootsSubscriber;
6 use Drupal\Core\Routing\RouteBuildEvent;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
9 use Symfony\Component\Routing\RouteCollection;
12 * @coversDefaultClass \Drupal\Core\EventSubscriber\PathRootsSubscriber
13 * @group EventSubscriber
15 class PathRootsSubscriberTest extends UnitTestCase {
20 * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
25 * The tested path root subscriber.
27 * @var \Drupal\Core\EventSubscriber\PathRootsSubscriber
29 protected $pathRootsSubscriber;
34 protected function setUp() {
35 $this->state = $this->getMock('Drupal\Core\State\StateInterface');
36 $this->pathRootsSubscriber = new PathRootsSubscriber($this->state);
40 * Tests altering and finished event.
42 * @covers ::onRouteAlter
43 * @covers ::onRouteFinished
45 public function testSubscribing() {
47 // Ensure that onRouteFinished can be called without throwing notices
48 // when no path roots got set.
49 $this->pathRootsSubscriber->onRouteFinished();
51 $route_collection = new RouteCollection();
52 $route_collection->add('test_route1', new Route('/test/bar'));
53 $route_collection->add('test_route2', new Route('/test/baz'));
54 $route_collection->add('test_route3', new Route('/test2/bar/baz'));
56 $event = new RouteBuildEvent($route_collection, 'provider');
57 $this->pathRootsSubscriber->onRouteAlter($event);
59 $route_collection = new RouteCollection();
60 $route_collection->add('test_route4', new Route('/test1/bar'));
61 $route_collection->add('test_route5', new Route('/test2/baz'));
62 $route_collection->add('test_route6', new Route('/test2/bar/baz'));
64 $event = new RouteBuildEvent($route_collection, 'provider');
65 $this->pathRootsSubscriber->onRouteAlter($event);
67 $this->state->expects($this->once())
69 ->with('router.path_roots', ['test', 'test2', 'test1']);
71 $this->pathRootsSubscriber->onRouteFinished();