Unverified Commit 88b77e67 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3588331 PsrResponseSubscriber doesn't support \Psr\Http\Message\StreamInterface

By: mglaman
parent c0f1c27c
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -41,7 +41,8 @@ public function onKernelView(ViewEvent $event) {
    $controller_result = $event->getControllerResult();

    if ($controller_result instanceof ResponseInterface) {
      $event->setResponse($this->httpFoundationFactory->createResponse($controller_result));
      $streamed = !$controller_result->getBody()->isSeekable();
      $event->setResponse($this->httpFoundationFactory->createResponse($controller_result, $streamed));
    }

  }
+54 −1
Original line number Diff line number Diff line
@@ -10,9 +10,11 @@
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\Stub;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

@@ -57,11 +59,62 @@ protected function setUp(): void {
   * @legacy-covers ::onKernelView
   */
  public function testConvertsControllerResult(): void {
    $event = $this->createEvent($this->createStub(ResponseInterface::class));
    $body = $this->createStub(StreamInterface::class);
    $body->method('isSeekable')->willReturn(TRUE);
    $psr_response = $this->createStub(ResponseInterface::class);
    $psr_response->method('getBody')->willReturn($body);

    $event = $this->createEvent($psr_response);
    $this->psrResponseSubscriber->onKernelView($event);
    $this->assertInstanceOf(Response::class, $event->getResponse());
  }

  /**
   * Tests that a seekable body results in a non-streamed response.
   *
   * @legacy-covers ::onKernelView
   */
  public function testConvertsSeekableBodyWithoutStreaming(): void {
    $body = $this->createStub(StreamInterface::class);
    $body->method('isSeekable')->willReturn(TRUE);
    $psr_response = $this->createStub(ResponseInterface::class);
    $psr_response->method('getBody')->willReturn($body);

    $factory = $this->createMock(HttpFoundationFactoryInterface::class);
    $factory->expects($this->once())
      ->method('createResponse')
      ->with($psr_response, FALSE)
      ->willReturn($this->createStub(Response::class));

    $subscriber = new PsrResponseSubscriber($factory);
    $event = $this->createEvent($psr_response);
    $subscriber->onKernelView($event);
    $this->assertInstanceOf(Response::class, $event->getResponse());
  }

  /**
   * Tests that a non-seekable body results in a streamed response.
   *
   * @legacy-covers ::onKernelView
   */
  public function testConvertsNonSeekableBodyWithStreaming(): void {
    $body = $this->createStub(StreamInterface::class);
    $body->method('isSeekable')->willReturn(FALSE);
    $psr_response = $this->createStub(ResponseInterface::class);
    $psr_response->method('getBody')->willReturn($body);

    $factory = $this->createMock(HttpFoundationFactoryInterface::class);
    $factory->expects($this->once())
      ->method('createResponse')
      ->with($psr_response, TRUE)
      ->willReturn($this->createStub(StreamedResponse::class));

    $subscriber = new PsrResponseSubscriber($factory);
    $event = $this->createEvent($psr_response);
    $subscriber->onKernelView($event);
    $this->assertInstanceOf(StreamedResponse::class, $event->getResponse());
  }

  /**
   * Tests altering and finished event.
   *