Skip to content

Support BinaryFileResponse and custom streaming response classes. #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Bridges/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
use Symfony\Component\HttpKernel\TerminableInterface;
use Illuminate\Contracts\Http\Kernel;

Expand Down Expand Up @@ -290,13 +289,8 @@ protected function mapResponse(SymfonyResponse $syResponse, $stdout='')

// get contents
ob_start();
if ($syResponse instanceof SymfonyStreamedResponse) {
$syResponse->sendContent();
$content = @ob_get_clean();
} else {
$content = $syResponse->getContent();
@ob_end_flush();
}
$syResponse->sendContent();
$content = @ob_get_clean();

if ($stdout) {
$content = $stdout . $content;
Expand Down
22 changes: 22 additions & 0 deletions tests/SymfonyBootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ public function testPostJSON()
$this->assertEquals(201, $response->getStatusCode());
$this->assertEquals('Received JSON: {"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}', (string)$response->getBody());
}

/**
* @runInSeparateProcess
*/
public function testStreamedResponse()
{
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
$bridge = new HttpKernel();
$bridge->bootstrap('Symfony', 'test', true);

$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
$request->method('getHeader')->with('Cookie')->willReturn([]);
$request->method('getQueryParams')->willReturn([]);
$request->method('getUploadedFiles')->willReturn([]);
$request->method('getMethod')->willReturn('GET');

$_SERVER['REQUEST_URI'] = '/streamed';

$response = $bridge->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('streamed data', (string)$response->getBody());
}
}
7 changes: 7 additions & 0 deletions tests/SymfonyMocks/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

class Kernel
{
Expand Down Expand Up @@ -69,6 +70,12 @@ public function handle(Request $request)
return new Response('Received JSON: '.$request->getContent(), 201);
}
} elseif ($request->getMethod() == 'GET') {
if ($request->getPathInfo() == '/streamed') {
return new StreamedResponse(function () {
echo 'streamed data';
}, 200);
}

// Simple get request
return new Response('Success', 200);
}
Expand Down