Skip to content

Commit 6c91cdd

Browse files
mathieudzandig
authored andcommitted
Support BinaryFileResponse and custom response classes (#132)
1 parent 61e760a commit 6c91cdd

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

Bridges/HttpKernel.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
1515
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1616
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
17-
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
1817
use Symfony\Component\HttpKernel\TerminableInterface;
1918
use Illuminate\Contracts\Http\Kernel;
2019

@@ -280,13 +279,8 @@ protected function mapResponse(SymfonyResponse $syResponse, $stdout='')
280279

281280
// get contents
282281
ob_start();
283-
if ($syResponse instanceof SymfonyStreamedResponse) {
284-
$syResponse->sendContent();
285-
$content = @ob_get_clean();
286-
} else {
287-
$content = $syResponse->getContent();
288-
@ob_end_flush();
289-
}
282+
$syResponse->sendContent();
283+
$content = @ob_get_clean();
290284

291285
if ($stdout) {
292286
$content = $stdout . $content;

tests/SymfonyBootstrapTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,26 @@ public function testPostJSON()
8383
$this->assertEquals(201, $response->getStatusCode());
8484
$this->assertEquals('Received JSON: {"some_json_array":[{"map1":"value1"},{"map2":"value2"}]}', (string)$response->getBody());
8585
}
86+
87+
/**
88+
* @runInSeparateProcess
89+
*/
90+
public function testStreamedResponse()
91+
{
92+
putenv('APP_KERNEL_NAMESPACE=PHPPM\\Tests\\SymfonyMocks\\');
93+
$bridge = new HttpKernel();
94+
$bridge->bootstrap('Symfony', 'test', true);
95+
96+
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
97+
$request->method('getHeader')->with('Cookie')->willReturn([]);
98+
$request->method('getQueryParams')->willReturn([]);
99+
$request->method('getUploadedFiles')->willReturn([]);
100+
$request->method('getMethod')->willReturn('GET');
101+
102+
$_SERVER['REQUEST_URI'] = '/streamed';
103+
104+
$response = $bridge->handle($request);
105+
$this->assertEquals(200, $response->getStatusCode());
106+
$this->assertEquals('streamed data', (string)$response->getBody());
107+
}
86108
}

tests/SymfonyMocks/Kernel.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\HttpFoundation\Request;
66
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpFoundation\StreamedResponse;
78

89
class Kernel
910
{
@@ -69,6 +70,12 @@ public function handle(Request $request)
6970
return new Response('Received JSON: '.$request->getContent(), 201);
7071
}
7172
} elseif ($request->getMethod() == 'GET') {
73+
if ($request->getPathInfo() == '/streamed') {
74+
return new StreamedResponse(function () {
75+
echo 'streamed data';
76+
}, 200);
77+
}
78+
7279
// Simple get request
7380
return new Response('Success', 200);
7481
}

0 commit comments

Comments
 (0)