From c7e93b393424b6135dc2d81693e17e52aa8a7d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 2 Mar 2026 17:58:08 -0300 Subject: [PATCH] Add unit test for Database\SqlFormatController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- psalm-baseline.xml | 5 -- .../Database/SqlFormatController.php | 10 ++-- .../Database/SqlFormatControllerTest.php | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 tests/unit/Controllers/Database/SqlFormatControllerTest.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0d15960414..37b944d701 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -926,11 +926,6 @@ - - - - - diff --git a/src/Controllers/Database/SqlFormatController.php b/src/Controllers/Database/SqlFormatController.php index 6f03c57c49..59c08d2ff4 100644 --- a/src/Controllers/Database/SqlFormatController.php +++ b/src/Controllers/Database/SqlFormatController.php @@ -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(); diff --git a/tests/unit/Controllers/Database/SqlFormatControllerTest.php b/tests/unit/Controllers/Database/SqlFormatControllerTest.php new file mode 100644 index 0000000000..909ae39361 --- /dev/null +++ b/tests/unit/Controllers/Database/SqlFormatControllerTest.php @@ -0,0 +1,52 @@ +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); + } +}