Remove ResponseRenderer::disable from SchemaExportController

Creates a new Response object instead of using the response from
ResponseRenderer class.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2024-05-06 20:12:07 -03:00
parent 058ba3d3a2
commit ad1336feb5
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
5 changed files with 32 additions and 25 deletions

View File

@ -616,7 +616,7 @@ return [
],
SchemaExportController::class => [
'class' => SchemaExportController::class,
'arguments' => ['$export' => '@export', '$response' => '@response'],
'arguments' => ['@export', '@response', '@' . ResponseFactory::class],
],
Server\BinlogController::class => [
'class' => Server\BinlogController::class,

View File

@ -2503,11 +2503,6 @@
<code><![CDATA[__construct]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Controllers/SchemaExportController.php">
<PossiblyUnusedReturnValue>
<code><![CDATA[Response|null]]></code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Controllers/Server/BinlogController.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>

View File

@ -8,6 +8,7 @@ use PhpMyAdmin\Core;
use PhpMyAdmin\Exceptions\ExportException;
use PhpMyAdmin\Export\Export;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Identifiers\DatabaseName;
@ -23,8 +24,11 @@ use function mb_strlen;
*/
final class SchemaExportController implements InvocableController
{
public function __construct(private readonly Export $export, private readonly ResponseRenderer $response)
{
public function __construct(
private readonly Export $export,
private readonly ResponseRenderer $response,
private readonly ResponseFactory $responseFactory,
) {
}
public function __invoke(ServerRequest $request): Response|null
@ -54,14 +58,13 @@ final class SchemaExportController implements InvocableController
return null;
}
$this->response->disable();
$response = $this->responseFactory->createResponse();
Core::downloadHeader(
$exportInfo['fileName'],
$exportInfo['mediaType'],
mb_strlen($exportInfo['fileData'], '8bit'),
);
echo $exportInfo['fileData'];
return null;
return $response->write($exportInfo['fileData']);
}
}

View File

@ -4,16 +4,23 @@ declare(strict_types=1);
namespace PhpMyAdmin\Tests\Controllers;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Controllers\SchemaExportController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Export\Export;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use function function_exists;
use function xdebug_get_headers;
#[CoversClass(SchemaExportController::class)]
class SchemaExportControllerTest extends AbstractTestCase
#[RunTestsInSeparateProcesses]
final class SchemaExportControllerTest extends AbstractTestCase
{
public function testExport(): void
{
@ -28,13 +35,20 @@ class SchemaExportControllerTest extends AbstractTestCase
'fileData' => 'file data',
]);
$response = new ResponseRenderer();
$controller = new SchemaExportController($export, $response);
$controller($request);
$output = $this->getActualOutputForAssertion();
self::assertSame('file data', $output);
self::assertTrue($response->isDisabled());
self::assertSame('', $response->getHTMLResult());
self::assertSame([], $response->getJSONResult());
$controller = new SchemaExportController($export, new ResponseRenderer(), ResponseFactory::create());
$response = $controller($request);
self::assertNotNull($response);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame('file data', (string) $response->getBody());
if (! function_exists('xdebug_get_headers')) {
return;
}
$headersList = xdebug_get_headers();
self::assertContains('Content-Disposition: attachment; filename="file.svg"', $headersList);
self::assertContains('Content-Type: image/svg+xml', $headersList);
self::assertContains('Content-Length: 9', $headersList);
}
}

View File

@ -172,11 +172,6 @@ class ResponseRenderer extends \PhpMyAdmin\ResponseRenderer
return $this->isAjax;
}
public function isDisabled(): bool
{
return $this->isDisabled;
}
public function getResponse(): Response
{
return $this->response;