Add unit test for Database\SqlFormatController
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
This commit is contained in:
parent
45ac0da83c
commit
c7e93b3934
@ -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>
|
||||
|
||||
@ -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();
|
||||
|
||||
52
tests/unit/Controllers/Database/SqlFormatControllerTest.php
Normal file
52
tests/unit/Controllers/Database/SqlFormatControllerTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user