Add unit test for Database\SqlFormatController

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
Maurício Meneghini Fauth 2026-03-02 17:58:08 -03:00
parent 45ac0da83c
commit c7e93b3934
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
3 changed files with 58 additions and 9 deletions

View File

@ -926,11 +926,6 @@
<code><![CDATA[Current::$database]]></code>
</MixedArgument>
</file>
<file src="src/Controllers/Database/SqlFormatController.php">
<PossiblyUnusedMethod>
<code><![CDATA[__construct]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Controllers/Database/Structure/AddPrefixTableController.php">
<PossiblyUnusedMethod>
<code><![CDATA[__construct]]></code>

View File

@ -15,9 +15,9 @@ use PhpMyAdmin\SqlParser\Utils\Formatter;
* Format SQL for SQL editors.
*/
#[Route('/database/sql/format', ['POST'])]
final class SqlFormatController implements InvocableController
final readonly class SqlFormatController implements InvocableController
{
public function __construct(private readonly ResponseRenderer $response)
public function __construct(private ResponseRenderer $response)
{
}
@ -25,9 +25,11 @@ final class SqlFormatController implements InvocableController
{
$query = $request->getParsedBodyParamAsString('sql', '');
if ($request->getParsedBodyParamAsString('formatSingleLine') === 'true') {
$this->response->addJSON(['sql' => Formatter::format($query, ['line_ending' => ' ', 'indentation' => ''])]);
$this->response->addJSON([
'sql' => Formatter::format($query, ['type' => 'text', 'line_ending' => ' ', 'indentation' => '']),
]);
} else {
$this->response->addJSON(['sql' => Formatter::format($query)]);
$this->response->addJSON(['sql' => Formatter::format($query, ['type' => 'text'])]);
}
return $this->response->response();

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Controllers\Database;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Controllers\Database\SqlFormatController;
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(SqlFormatController::class)]
final class SqlFormatControllerTest extends AbstractTestCase
{
public function testSqlFormatMultipleLines(): void
{
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
->withParsedBody(['sql' => "\nselect\n*\nfrom\ntbl\nwhere\n1\n", 'formatSingleLine' => 'false']);
$response = ($this->getSqlFormatController())($request);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame(
'{"sql":"SELECT\n *\nFROM\n tbl\nWHERE\n 1","success":true}',
(string) $response->getBody(),
);
}
public function testSqlFormatSingleLine(): void
{
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
->withParsedBody(['sql' => "\nselect\n*\nfrom\ntbl\nwhere\n1\n", 'formatSingleLine' => 'true']);
$response = ($this->getSqlFormatController())($request);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame(
'{"sql":"SELECT * FROM tbl WHERE 1","success":true}',
(string) $response->getBody(),
);
}
private function getSqlFormatController(): SqlFormatController
{
$responseRenderer = new ResponseRenderer();
$responseRenderer->setAjax(true);
return new SqlFormatController($responseRenderer);
}
}