Skip to content

Commit

Permalink
Prevent double etl evaluation in buffered response (#1499)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Feb 26, 2025
1 parent be1a274 commit 50f21d1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ private function evaluate() : void
->write($this->output->memoryLoader($id = \bin2hex(\random_bytes(16)) . '.memory'))
->run();

$this->content = $config->fstab()->for(protocol('memory'))->readFrom(path_memory($id))->content();
$fs = $config->fstab()->for(protocol('memory'));

if ($fs->status(path_memory($id)) === null) {
$this->buffered = true;
$this->content = '';
$this->statusCode = self::HTTP_NO_CONTENT;

return;
}

$this->content = $fs->readFrom(path_memory($id))->content();
$this->buffered = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Flow\Bridge\Symfony\HttpFoundation\Tests\Unit\Response;

use function Flow\Bridge\Symfony\HttpFoundation\{http_json_output, http_stream_open};
use function Flow\ETL\DSL\{from_array, int_entry, row, rows};
use Flow\ETL\Extractor;
use Flow\ETL\Tests\FlowTestCase;

final class FlowBufferedResponseTest extends FlowTestCase
{
public function test_response_from_empty_dataset() : void
{
$response = http_stream_open(from_array([]))->response(http_json_output());

self::assertEquals('', $response->getContent());
self::assertEquals(204, $response->getStatusCode());
}

public function test_response_is_buffered_only_once() : void
{
$extractor = $this->createMock(Extractor::class);

$extractor->expects(self::once())->method('extract')->willReturn((function () : \Generator {
yield rows(row(int_entry('id', 1)));
})());

$response = http_stream_open($extractor)->response(http_json_output());

$response->getContent();
$response->getContent();

self::assertEquals('[{"id":1}]', $response->getContent());
self::assertEquals(200, $response->getStatusCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Flow\Bridge\Symfony\HttpFoundation\Tests\Unit\Response;

use function Flow\Bridge\Symfony\HttpFoundation\{http_json_output, http_stream_open};
use function Flow\ETL\DSL\from_array;
use PHPUnit\Framework\TestCase;

final class FlowStreamedResponseTest extends TestCase
{
public function test_response_from_empty_dataset() : void
{
$response = http_stream_open(from_array([]))->streamedResponse(http_json_output());

self::assertEquals('', $response->getContent());
self::assertEquals(200, $response->getStatusCode());
}
}

0 comments on commit 50f21d1

Please sign in to comment.