ViewEvent example

/** * Sets up an event that returns $controllerResult. * * @param mixed $controller_result * The return Object. * * @return \Symfony\Component\HttpKernel\Event\ViewEvent * A ViewEvent object to test. */
  protected function createEvent($controller_result) {
    return new ViewEvent(
      $this->createMock(HttpKernelInterface::class),
      $this->createMock(Request::class),
      HttpKernelInterface::MAIN_REQUEST,
      $controller_result
    );
  }

}
$listener->onKernelView($event);
        self::assertFalse($event->hasResponse());

        $event = $this->createEventMock(null);

        $listener->onKernelView($event);
        self::assertFalse($event->hasResponse());
    }

    private function createEventMock(mixed $controllerResult): ViewEvent
    {
        return new ViewEvent($this->createMock(HttpKernelInterface::class)new Request(), HttpKernelInterface::MAIN_REQUEST, $controllerResult);
    }
}
$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);

            if ($event->hasResponse()) {
                $response = $event->getResponse();
            } else {
                $msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));

                // the user may have forgotten to return something                 if (null === $response) {
                    $msg .= ' Did you forget to add a return statement somewhere in your controller?';
                }

                
$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);
        $listener->onKernelView($event);
        $this->assertNull($event->getResponse());

        
$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);

            if ($event->hasResponse()) {
                $response = $event->getResponse();
            } else {
                $msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));

                // the user may have forgotten to return something                 if (null === $response) {
                    $msg .= ' Did you forget to add a return statement somewhere in your controller?';
                }

                
Home | Imprint | This part of the site doesn't use cookies.