function UrlTest::testUrlFromRequest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/UrlTest.php \Drupal\Tests\Core\UrlTest::testUrlFromRequest()
  2. 8.9.x core/tests/Drupal/Tests/Core/UrlTest.php \Drupal\Tests\Core\UrlTest::testUrlFromRequest()
  3. 10 core/tests/Drupal/Tests/Core/UrlTest.php \Drupal\Tests\Core\UrlTest::testUrlFromRequest()

Tests creating a URL from a request.

File

core/tests/Drupal/Tests/Core/UrlTest.php, line 120

Class

UrlTest
@coversDefaultClass \Drupal\Core\Url @group UrlTest

Namespace

Drupal\Tests\Core

Code

public function testUrlFromRequest() {
    $this->router
        ->expects($this->exactly(3))
        ->method('matchRequest')
        ->willReturnCallback(function (Request $request) {
        [
            $route_name,
            $vars,
        ] = match ($request->getPathInfo()) {    '/node' => [
                'view.frontpage.page_1',
                [],
            ],
            '/node/1' => [
                'node_view',
                [
                    'node' => '1',
                ],
            ],
            '/node/2/edit' => [
                'node_edit',
                [
                    'node' => '2',
                ],
            ],
        
        };
        return [
            RouteObjectInterface::ROUTE_NAME => $route_name,
            '_raw_variables' => new InputBag($vars),
        ];
    });
    $urls = [];
    foreach ($this->map as $index => $values) {
        $path = array_pop($values);
        $url = Url::createFromRequest(Request::create("{$path}"));
        $expected = Url::fromRoute($values[0], $values[1], $values[2]);
        $this->assertEquals($expected, $url);
        $urls[$index] = $url;
    }
    return $urls;
}

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