createDatabaseInterface();
}
public function testWithoutParams(): void
{
$request = self::createStub(ServerRequest::class);
$request->method('isAjax')->willReturn(true);
$request->method('getParsedBodyParam')->willReturnMap([['sql_query', '', ''], ['options', null, null]]);
$response = $this->getLintController()($request);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame(['application/json; charset=UTF-8'], $response->getHeader('Content-Type'));
$output = (string) $response->getBody();
self::assertJson($output);
self::assertJsonStringEqualsJsonString('[]', $output);
}
public function testWithoutSqlErrors(): void
{
$request = self::createStub(ServerRequest::class);
$request->method('isAjax')->willReturn(true);
$request->method('getParsedBodyParam')->willReturnMap([
['sql_query', '', 'SELECT * FROM `actor` WHERE `actor_id` = 1;'],
['options', null, null],
]);
$response = $this->getLintController()($request);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame(['application/json; charset=UTF-8'], $response->getHeader('Content-Type'));
$output = (string) $response->getBody();
self::assertJson($output);
self::assertJsonStringEqualsJsonString('[]', $output);
}
public function testWithSqlErrors(): void
{
$expectedJson = json_encode([
[
'message' => 'An alias was previously found. (near `actor_id`)',
'fromLine' => 0,
'fromColumn' => 29,
'toLine' => 0,
'toColumn' => 39,
'severity' => 'error',
],
[
'message' => 'Unexpected token. (near `actor_id`)',
'fromLine' => 0,
'fromColumn' => 29,
'toLine' => 0,
'toColumn' => 39,
'severity' => 'error',
],
[
'message' => 'Unexpected token. (near =)',
'fromLine' => 0,
'fromColumn' => 40,
'toLine' => 0,
'toColumn' => 41,
'severity' => 'error',
],
[
'message' => 'Unexpected token. (near 1)',
'fromLine' => 0,
'fromColumn' => 42,
'toLine' => 0,
'toColumn' => 43,
'severity' => 'error',
],
]);
self::assertNotFalse($expectedJson);
$request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/')
->withParsedBody([
'ajax_request' => '1',
'sql_query' => 'SELECT * FROM `actor` WHEREE `actor_id` = 1',
'options' => null,
]);
$response = $this->getLintController()($request);
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
self::assertSame(['application/json; charset=UTF-8'], $response->getHeader('Content-Type'));
$output = (string) $response->getBody();
self::assertJson($output);
self::assertJsonStringEqualsJsonString($expectedJson, $output);
}
private function getLintController(): LintController
{
return new LintController(ResponseFactory::create());
}
}