ControllerReference example


    public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
    {
        $strategy = new EsiFragmentRenderer(new Esi()$this->getInlineStrategy(true));
        $strategy->render('/', Request::create('/'));
    }

    public function testRenderFallbackWithScalar()
    {
        $strategy = new EsiFragmentRenderer(new Esi()$this->getInlineStrategy(true)new UriSigner('foo'));
        $request = Request::create('/');
        $reference = new ControllerReference('main_controller', ['foo' => [true]][]);
        $strategy->render($reference$request);
    }

    public function testRender()
    {
        $strategy = new EsiFragmentRenderer(new Esi()$this->getInlineStrategy());

        $request = Request::create('/');
        $request->setLocale('fr');
        $request->headers->set('Surrogate-Capability', 'ESI/1.0');

        
public function testRender()
    {
        $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));

        $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
    }

    public function testRenderWithControllerReference()
    {
        $strategy = new InlineFragmentRenderer($this->getKernel($this->returnValue(new Response('foo'))));

        $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', [][]), Request::create('/'))->getContent());
    }

    public function testRenderWithObjectsAsAttributes()
    {
        $object = new \stdClass();

        $subRequest = Request::create('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller');
        $subRequest->attributes->replace(['object' => $object, '_format' => 'html', '_controller' => 'main_controller', '_locale' => 'en']);
        $subRequest->headers->set('x-forwarded-for', ['127.0.0.1']);
        $subRequest->headers->set('forwarded', ['for="127.0.0.1";host="localhost";proto=http']);
        $subRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
        

        return new Response($request->getLocale());
    }

    public function forwardLocaleAction(Request $request)
    {
        return new Response($request->getLocale());
    }

    public function fragmentUriAction(Request $request, FragmentUriGeneratorInterface $fragmentUriGenerator)
    {
        return new Response($fragmentUriGenerator->generate(new ControllerReference(self::class.'::indexAction')$request));
    }
}

class Bar
{
    private string $bar = 'bar';

    public function getBar()
    {
        return $this->bar;
    }
}


    public function testRenderControllerReference()
    {
        $signer = new UriSigner('foo');
        $strategy = new SsiFragmentRenderer(new Ssi()$this->getInlineStrategy()$signer);

        $request = Request::create('/');
        $request->setLocale('fr');
        $request->headers->set('Surrogate-Capability', 'SSI/1.0');

        $reference = new ControllerReference('main_controller', [][]);
        $altReference = new ControllerReference('alt_controller', [][]);

        $this->assertEquals(
            '<!--#include virtual="/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" -->',
            $strategy->render($reference$request['alt' => $altReference])->getContent()
        );
    }

    public function testRenderControllerReferenceWithAbsoluteUri()
    {
        $signer = new UriSigner('foo');
        
use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer;
use Symfony\Component\HttpKernel\UriSigner;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class HIncludeFragmentRendererTest extends TestCase
{
    public function testRenderExceptionWhenControllerAndNoSigner()
    {
        $this->expectException(\LogicException::class);
        $strategy = new HIncludeFragmentRenderer();
        $strategy->render(new ControllerReference('main_controller', [][]), Request::create('/'));
    }

    public function testRenderWithControllerAndSigner()
    {
        $strategy = new HIncludeFragmentRenderer(null, new UriSigner('foo'));

        $this->assertEquals('<hx:include src="/_fragment?_hash=BP%2BOzCD5MRUI%2BHJpgPDOmoju00FnzLhP3TGcSHbbBLs%3D&amp;_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller"></hx:include>', $strategy->render(new ControllerReference('main_controller', [][]), Request::create('/'))->getContent());
    }

    public function testRenderWithUri()
    {
        
/** * @dataProvider getGenerateFragmentUriData */
    public function testGenerateAbsoluteFragmentUri($uri$controller)
    {
        $this->assertEquals('http://localhost'.$uri$this->callGenerateFragmentUriMethod($controller, Request::create('/'), true));
    }

    public static function getGenerateFragmentUriData()
    {
        return [
            ['/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', [][])],
            ['/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['_format' => 'xml'][])],
            ['/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => 'foo', '_format' => 'json'][])],
            ['/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => 'foo']['bar' => 'bar'])],
            ['/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', []['foo' => 'foo'])],
            ['/_fragment?_path=foo%255B0%255D%3Dfoo%26foo%255B1%255D%3Dbar%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', ['foo' => ['foo', 'bar']][])],
        ];
    }

    public function testGenerateFragmentUriWithARequest()
    {
        $request = Request::create('/');
        

        $errorUrl = $this->generateUrl('subrequest_fragment_error', ['_locale' => 'fr', '_format' => 'json']);
        $altUrl = $this->generateUrl('subrequest_fragment', ['_locale' => 'fr', '_format' => 'json']);

        // simulates a failure during the rendering of a fragment...         // should render fr/json         $content = $handler->render($errorUrl, 'inline', ['alt' => $altUrl]);

        // ...to check that the FragmentListener still references the right Request         // when rendering another fragment after the error occurred         // should render en/html instead of fr/json         $content .= $handler->render(new ControllerReference(self::class.'::fragmentAction'));

        // forces the LocaleListener to set fr for the locale...         // should render fr/json         $content .= $handler->render($altUrl);

        // ...and check that after the rendering, the original Request is back         // and en is used as a locale         // should use en/html instead of fr/json         $content .= '--'.$this->generateUrl('subrequest_fragment');

        // The RouterListener is also tested as if it does not keep the right

        return [
            new TwigFunction('render', [HttpKernelRuntime::class, 'renderFragment']['is_safe' => ['html']]),
            new TwigFunction('render_*', [HttpKernelRuntime::class, 'renderFragmentStrategy']['is_safe' => ['html']]),
            new TwigFunction('fragment_uri', [HttpKernelRuntime::class, 'generateFragmentUri']),
            new TwigFunction('controller', [self::class, 'controller']),
        ];
    }

    public static function controller(string $controller, array $attributes = [], array $query = []): ControllerReference
    {
        return new ControllerReference($controller$attributes$query);
    }
}
Home | Imprint | This part of the site doesn't use cookies.