MockRouteProvider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\system\Tests\Routing;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  5. use Symfony\Component\Routing\RouteCollection;
  6. use Drupal\Core\Routing\RouteProviderInterface;
  7. /**
  8. * Easily configurable mock route provider.
  9. */
  10. class MockRouteProvider implements RouteProviderInterface {
  11. /**
  12. * A collection of routes for this route provider.
  13. *
  14. * @var \Symfony\Component\Routing\RouteCollection
  15. */
  16. protected $routes;
  17. /**
  18. * Constructs a new MockRouteProvider.
  19. *
  20. * @param \Symfony\Component\Routing\RouteCollection $routes
  21. * The route collection to use for this provider.
  22. */
  23. public function __construct(RouteCollection $routes) {
  24. $this->routes = $routes;
  25. }
  26. /**
  27. * Implements \Symfony\Cmf\Component\Routing\RouteProviderInterface::getRouteCollectionForRequest().
  28. *
  29. * Simply return all routes to prevent
  30. * \Symfony\Component\Routing\Exception\ResourceNotFoundException.
  31. */
  32. public function getRouteCollectionForRequest(Request $request) {
  33. return $this->routes;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getRouteByName($name) {
  39. $routes = $this->getRoutesByNames([$name]);
  40. if (empty($routes)) {
  41. throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
  42. }
  43. return reset($routes);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function preLoadRoutes($names) {
  49. // Nothing to do.
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getRoutesByNames($names) {
  55. $routes = [];
  56. foreach ($names as $name) {
  57. $routes[] = $this->routes->get($name);
  58. }
  59. return $routes;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getRoutesByPattern($pattern) {
  65. return new RouteCollection();
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getAllRoutes() {
  71. return $this->routes->all();
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function reset() {
  77. $this->routes = [];
  78. }
  79. }