summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/src/Tests/Routing/MockRouteProvider.php
blob: ee629187f1f0899b7d66df6da8db3c3eb242e4f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

declare(strict_types=1);

namespace Drupal\system\Tests\Routing;

use Drupal\Core\Routing\RouteProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouteCollection;

/**
 * Easily configurable mock route provider.
 */
class MockRouteProvider implements RouteProviderInterface {

  /**
   * A collection of routes for this route provider.
   *
   * @var \Symfony\Component\Routing\RouteCollection
   */
  protected $routes;

  /**
   * Constructs a new MockRouteProvider.
   *
   * @param \Symfony\Component\Routing\RouteCollection $routes
   *   The route collection to use for this provider.
   */
  public function __construct(RouteCollection $routes) {
    $this->routes = $routes;
  }

  /**
   * Implements \Drupal\Core\Routing\RouteProviderInterface::getRouteCollectionForRequest().
   *
   * Simply return all routes to prevent
   * \Symfony\Component\Routing\Exception\ResourceNotFoundException.
   */
  public function getRouteCollectionForRequest(Request $request) {
    return $this->routes;
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteByName($name) {
    $routes = $this->getRoutesByNames([$name]);
    if (empty($routes)) {
      throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    return reset($routes);
  }

  /**
   * {@inheritdoc}
   */
  public function preLoadRoutes($names) {
    // Nothing to do.
  }

  /**
   * {@inheritdoc}
   */
  public function getRoutesByNames($names) {
    $routes = [];
    foreach ($names as $name) {
      $routes[] = $this->routes->get($name);
    }

    return $routes;
  }

  /**
   * {@inheritdoc}
   */
  public function getRoutesByPattern($pattern) {
    return new RouteCollection();
  }

  /**
   * {@inheritdoc}
   */
  public function getAllRoutes() {
    return $this->routes->all();
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    $this->routes = [];
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteAliases(string $route_name): iterable {
    return new RouteCollection();
  }

}