ControllerArgumentsEvent example

        if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        // controller arguments         $arguments = $this->argumentResolver->getArguments($request$controller$event->getControllerReflector());

        $event = new ControllerArgumentsEvent($this$event$arguments$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
        $controller = $event->getController();
        $arguments = $event->getArguments();

        // call controller         $response = $controller(...$arguments);

        // view         if (!$response instanceof Response) {
            $event = new ViewEvent($this$request$type$response$event);
            $this->dispatcher->dispatch($event, KernelEvents::VIEW);

            
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Bar;
use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Baz;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ControllerArgumentsEventTest extends TestCase
{
    public function testControllerArgumentsEvent()
    {
        $event = new ControllerArgumentsEvent(new TestHttpKernel()function D) {}['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $this->assertEquals($event->getArguments()['test']);
    }

    public function testSetAttributes()
    {
        $controller = function D) {};
        $event = new ControllerArgumentsEvent(new TestHttpKernel()$controller['test']new Request(), HttpKernelInterface::MAIN_REQUEST);
        $event->setController($controller[]);

        $this->assertSame([]$event->getAttributes());
    }

    
new Serializer(),
            $this->createMock(ValidatorInterface::class),
        );

        $argument = new ArgumentMetadata('notTyped', null, false, false, null, false, [
            MapRequestPayload::class => new MapRequestPayload(),
        ]);
        $request = Request::create('/', 'POST', server: ['HTTP_CONTENT_TYPE' => 'application/json']);

        $kernel = $this->createMock(HttpKernelInterface::class);
        $arguments = $resolver->resolve($request$argument);
        $event = new ControllerArgumentsEvent($kernelfunction D) {}$arguments$request, HttpKernelInterface::MAIN_REQUEST);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Could not resolve the "$notTyped" controller argument: argument should be typed.');

        $resolver->onKernelControllerArguments($event);
    }

    public function testDefaultValueArgument()
    {
        $payload = new RequestPayload(50);

        
use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeMethodsController;

class IsGrantedAttributeListenerTest extends TestCase
{
    public function testAttribute()
    {
        $authChecker = $this->createMock(AuthorizationCheckerInterface::class);
        $authChecker->expects($this->exactly(2))
            ->method('isGranted')
            ->willReturn(true);

        $event = new ControllerArgumentsEvent(
            $this->createMock(HttpKernelInterface::class),
            [new IsGrantedAttributeController(), 'foo'],
            [],
            new Request(),
            null
        );

        $listener = new IsGrantedAttributeListener($authChecker);
        $listener->onKernelControllerArguments($event);

        $authChecker = $this->createMock(AuthorizationCheckerInterface::class);
        
        if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        // controller arguments         $arguments = $this->argumentResolver->getArguments($request$controller$event->getControllerReflector());

        $event = new ControllerArgumentsEvent($this$event$arguments$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
        $controller = $event->getController();
        $arguments = $event->getArguments();

        // call controller         $response = $controller(...$arguments);

        // view         if (!$response instanceof Response) {
            $event = new ViewEvent($this$request$type$response$event);
            $this->dispatcher->dispatch($event, KernelEvents::VIEW);

            

    public function testLastModifiedNotModifiedResponse(string $expression)
    {
        $entity = new TestEntity();

        $request = $this->createRequest(new Cache(lastModified: $expression));
        $request->attributes->set('date', new \DateTimeImmutable('Fri, 23 Aug 2013 00:00:00 GMT'));
        $request->headers->add(['If-Modified-Since' => 'Fri, 23 Aug 2013 00:00:00 GMT']);

        $listener = new CacheAttributeListener();
        $controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel()fn (TestEntity $test) => new Response()[$entity]$request, null);

        $listener->onKernelControllerArguments($controllerArgumentsEvent);
        $response = $controllerArgumentsEvent->getController()($entity);

        $this->assertSame(304, $response->getStatusCode());
    }

    /** * @testWith ["test.getDate()"] * ["date"] */
    
/** * @dataProvider controllerProvider */
    public function testOnControllerArguments(callable $controller)
    {
        $listener = new ErrorListener($controller$this->createMock(LoggerInterface::class), true);

        $kernel = $this->createMock(HttpKernelInterface::class);
        $kernel->method('handle')->willReturnCallback(function DRequest $request) use ($listener$controller$kernel) {
            $this->assertSame($controller$request->attributes->get('_controller'));
            $arguments = (new ArgumentResolver())->getArguments($request$controller);
            $event = new ControllerArgumentsEvent($kernel$controller$arguments$request, HttpKernelInterface::SUB_REQUEST);
            $listener->onControllerArguments($event);

            return $controller(...$event->getArguments());
        });

        $event = new ExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::MAIN_REQUEST, new \Exception('foo'));
        $listener->onKernelException($event);

        $this->assertSame('OK: foo', $event->getResponse()->getContent());
    }

    
'templates/foo.html.twig', []],
                ];

                $this->assertSame(array_shift($series)$args);

                return 'Bar';
            })
        ;

        $request = new Request();
        $kernel = $this->createMock(HttpKernelInterface::class);
        $controllerArgumentsEvent = new ControllerArgumentsEvent($kernel[new TemplateAttributeController(), 'foo']['Bar']$request, null);
        $listener = new TemplateAttributeListener($twig);

        $event = new ViewEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST, ['foo' => 'bar']$controllerArgumentsEvent);
        $listener->onKernelView($event);
        $this->assertSame('Bar', $event->getResponse()->getContent());

        $event = new ViewEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST, null, $controllerArgumentsEvent);
        $listener->onKernelView($event);
        $this->assertSame('Bar', $event->getResponse()->getContent());

        $event = new ViewEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST, null);
        
Home | Imprint | This part of the site doesn't use cookies.