Skip to content

Commit 64a3d46

Browse files
committed
Stream datex response
1 parent db0e4d1 commit 64a3d46

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

src/Infrastructure/Controller/Api/Regulations/GetCifsIncidentsController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Application\Regulation\Query\GetCifsIncidentsQuery;
99
use OpenApi\Attributes as OA;
1010
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\HttpFoundation\StreamedResponse;
1112
use Symfony\Component\Routing\Attribute\Route;
1213

1314
final class GetCifsIncidentsController
@@ -29,10 +30,12 @@ public function __invoke(): Response
2930
{
3031
$incidents = $this->queryBus->handle(new GetCifsIncidentsQuery());
3132

32-
return new Response(
33-
$this->twig->render('api/regulations/cifs.xml.twig', [
34-
'incidents' => $incidents,
35-
]),
33+
return new StreamedResponse(
34+
function () use ($incidents): void {
35+
$this->twig->display('api/regulations/cifs.xml.twig', [
36+
'incidents' => $incidents,
37+
]);
38+
},
3639
Response::HTTP_OK,
3740
['Content-Type' => 'text/xml; charset=UTF-8'],
3841
);

src/Infrastructure/Controller/Api/Regulations/GetRegulationsController.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Application\Regulation\Query\GetRegulationOrdersToDatexFormatQuery;
1010
use OpenApi\Attributes as OA;
1111
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\HttpFoundation\StreamedResponse;
1213
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
1314
use Symfony\Component\Routing\Attribute\Route;
1415

@@ -44,11 +45,13 @@ public function __invoke(
4445
),
4546
);
4647

47-
return new Response(
48-
$this->twig->render('api/regulations.xml.twig', [
49-
'publicationTime' => $this->dateUtils->getNow(),
50-
'regulationOrders' => $regulationOrders,
51-
]),
48+
return new StreamedResponse(
49+
function () use ($regulationOrders): void {
50+
$this->twig->display('api/regulations.xml.twig', [
51+
'publicationTime' => $this->dateUtils->getNow(),
52+
'regulationOrders' => $regulationOrders,
53+
]);
54+
},
5255
Response::HTTP_OK,
5356
['Content-Type' => 'text/xml; charset=UTF-8'],
5457
);

tests/Integration/Infrastructure/Controller/Api/GetCifsIncidentsControllerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Tests\Integration\Infrastructure\Controller\Api;
66

77
use App\Tests\Integration\Infrastructure\Controller\AbstractWebTestCase;
8+
use Symfony\Component\HttpFoundation\StreamedResponse;
89

910
final class GetCifsIncidentsControllerTest extends AbstractWebTestCase
1011
{
@@ -14,15 +15,17 @@ public function testGet(): void
1415
$client->request('GET', '/api/regulations/cifs.xml');
1516
$response = $client->getResponse();
1617

18+
$this->assertInstanceOf(StreamedResponse::class, $response);
1719
$this->assertResponseStatusCodeSame(200);
1820
$this->assertSecurityHeaders();
1921
$this->assertSame('text/xml; charset=UTF-8', $response->headers->get('content-type'));
2022

23+
$content = $client->getInternalResponse()->getContent();
2124
$xml = new \DOMDocument();
22-
$xml->loadXML($response->getContent(), \LIBXML_NOBLANKS);
25+
$xml->loadXML($content, \LIBXML_NOBLANKS);
2326
$this->assertXmlStringEqualsXmlFile(
2427
__DIR__ . '/cifs-incidents-expected-result.xml',
25-
$response->getContent(),
28+
$content,
2629
);
2730
$this->assertTrue($xml->schemaValidate(self::$kernel->getProjectDir() . '/docs/spec/cifs/cifsv2.xsd'));
2831
}

tests/Integration/Infrastructure/Controller/Api/GetRegulationsControllerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Tests\Integration\Infrastructure\Controller\AbstractWebTestCase;
1111
use App\Tests\SessionHelper;
1212
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
13+
use Symfony\Component\HttpFoundation\StreamedResponse;
1314

1415
final class GetRegulationsControllerTest extends AbstractWebTestCase
1516
{
@@ -44,6 +45,7 @@ public function testGetRegulationsToDatexFormat(): void
4445
$client->request('GET', '/api/regulations.xml');
4546
$response = $client->getResponse();
4647

48+
$this->assertInstanceOf(StreamedResponse::class, $response);
4749
$this->assertSame('text/xml; charset=UTF-8', $response->headers->get('content-type'));
4850
$this->assertResponseStatusCodeSame(200);
4951
$this->assertSecurityHeaders();
@@ -69,6 +71,7 @@ public function testGetRegulationsToDatexFormatWithFilters(): void
6971
$client->request('GET', '/api/regulations.xml?includePermanent=false&includeTemporary=true&includeExpired=true');
7072
$response = $client->getResponse();
7173

74+
$this->assertInstanceOf(StreamedResponse::class, $response);
7275
$this->assertSame('text/xml; charset=UTF-8', $response->headers->get('content-type'));
7376
$this->assertResponseStatusCodeSame(200);
7477
$this->assertSecurityHeaders();

tests/Integration/Infrastructure/Symfony/Command/RunMetabaseExportCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
99
use Symfony\Component\Console\Tester\CommandTester;
1010

11+
/**
12+
* @group only
13+
*/
1114
final class RunMetabaseExportCommandTest extends KernelTestCase
1215
{
1316
public function testExecute(): void

tests/Mock/DiaLogMockHttpClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ private function handleRequests(string $method, string $url, array $options): Mo
2424
$request = Request::create($url, $method);
2525
$response = $this->httpKernel->handle($request);
2626

27-
return new MockResponse($response->getContent());
27+
ob_start();
28+
$response->sendContent();
29+
$content = ob_get_clean();
30+
31+
return new MockResponse($content);
2832
}
2933
}

0 commit comments

Comments
 (0)