RouterTest.php

Same filename in this branch
  1. main core/modules/system/tests/src/Functional/Routing/RouterTest.php
  2. main core/tests/Drupal/Tests/Core/Routing/RouterTest.php
Same filename and directory in other branches
  1. 11.x core/modules/system/tests/src/Functional/Routing/RouterTest.php
  2. 11.x core/tests/Drupal/Tests/Core/Routing/RouterTest.php
  3. 10 core/modules/system/tests/src/Functional/Routing/RouterTest.php
  4. 10 core/tests/Drupal/Tests/Core/Routing/RouterTest.php
  5. 9 core/modules/system/tests/src/Functional/Routing/RouterTest.php
  6. 9 core/tests/Drupal/Tests/Core/Routing/RouterTest.php
  7. 8.9.x core/modules/system/tests/src/Functional/Routing/RouterTest.php
  8. 8.9.x core/tests/Drupal/Tests/Core/Routing/RouterTest.php
  9. 11.x core/tests/Drupal/KernelTests/Core/Routing/RouterTest.php

Namespace

Drupal\KernelTests\Core\Routing

File

core/tests/Drupal/KernelTests/Core/Routing/RouterTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\Routing;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Routing\Exception\CacheableResourceNotFoundException;
use Drupal\Core\Routing\Router;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the router.
 */
class RouterTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
  ];
  
  /**
   * The router.
   *
   * @var \Drupal\Core\Routing\Router
   */
  protected Router $router;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->router = $this->container
      ->get('router.no_access_checks');
  }
  
  /**
   * {@inheritdoc}
   */
  protected function tearDown() : void {
    parent::tearDown();
  }
  
  /**
   * Tests the router request matching.
   */
  public function testMatchRequest() : void {
    $defaults = $this->router
      ->match('/admin');
    $this->assertIsArray($defaults);
    $this->assertArrayHasKey('_route', $defaults);
    $this->assertEquals('system.admin', $defaults['_route']);
    try {
      $this->router
        ->match('/does/not/exist');
      $this->fail(sprintf('%s::match() should throw an exception for a non-existing path.', Router::class));
    } catch (CacheableResourceNotFoundException $exception) {
      $this->assertEquals([], $exception->getCacheContexts());
      $this->assertEquals([], $exception->getCacheTags());
      $this->assertEquals(Cache::PERMANENT, $exception->getCacheMaxAge());
    } catch (\Throwable $exception) {
      $this->fail(sprintf('%s::match() should throw %s, %s thrown instead.', Router::class, CacheableResourceNotFoundException::class, get_class($exception)));
    }
  }

}

Classes

Title Deprecated Summary
RouterTest Tests the router.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.