Output of the "Get the routing information of the referer" Symfony snippet

Environment:

  • Strangebuzz Snippet VM: 2.1.0
  • Symfony: 5.1.0

Code execution output:

Referer is invalid or empty.

 Back to the code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  More on Stackoverflow  Random snippet

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;

/**
 * @covers Snippet7Trait
 */
final class Snippet7Test extends KernelTestCase
{
    /**
     * @var RouterInterface
     */
    private $router;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->router = self::$container->get('router');
    }

    /**
     * @covers Snippet7Trait::snippet7
     *
     * @see https://www.strangebuzz.com/en/snippets/get-the-routing-information-of-the-referer
     */
    public function testSnippet7(): void
    {
        $referer = '/en/snippets/get-the-routing-information-of-the-referer';
        $refererPathInfo = Request::create($referer)->getPathInfo();
        $routeInfos = $this->router->match($refererPathInfo);
        $refererRoute = $routeInfos['_route'] ?? '';
        unset($routeInfos['_route'], $routeInfos['_controller']);
        $routeInfos['foo'] = 'bar';
        $newUrl = $this->router->generate($refererRoute, $routeInfos);
        $this->assertSame($referer.'?foo=bar', $newUrl);
    }
}