Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unmaintained SQL formatter dependency #1171

Merged
merged 1 commit into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions Tests/ProfilerTest.php
Expand Up @@ -87,14 +87,18 @@ public function testRender() : void
'queries' => $this->logger->queries,
]);

$output = str_replace(["\e[37m", "\e[0m", "\e[32;1m", "\e[34;1m"], '', $output);
$this->assertContains("SELECT * FROM foo WHERE bar IN ('foo', 'bar') AND \"\" >= \"\";", $output);

$expectedEscapedSql = 'SELECT 
  * 
FROM 
  foo 
WHERE 
  bar IN (?, ?) 
  AND "" >= ""';
$this->assertContains($expectedEscapedSql, $output);
$expectedEscapedSql = 'SELECT
  *
FROM
  foo
WHERE
  bar IN (?, ?)
  AND "" >= ""';
$this->assertSame(
"SELECT \n * \nFROM \n foo \nWHERE \n bar IN (?, ?) \n AND \"\" >= \"\"",
"SELECT\n *\nFROM\n foo\nWHERE\n bar IN (?, ?)\n AND \"\" >= \"\"",
html_entity_decode($expectedEscapedSql)
);

$this->assertContains($expectedEscapedSql, $output);

$this->assertSame(1, preg_match('/' . str_replace(
ostrolucky marked this conversation as resolved.
Show resolved Hide resolved
' ',
'.*',
preg_quote('SELECT * FROM foo WHERE bar IN ( ? , ? )')
) . '/', $output));
}
}
57 changes: 57 additions & 0 deletions Tests/Twig/DoctrineExtensionTest.php
Expand Up @@ -99,6 +99,63 @@ public function testEscapeBooleanParameter() : void
{
$this->assertEquals('1', DoctrineExtension::escapeFunction(true));
}

/**
* @group legacy
*/
public function testItHighlightsSqlQueriesUsingCssClasses() : void
{
$extension = new DoctrineExtension();
self::assertStringContainsString(
'class=',
$extension->formatQuery('CREATE DATABASE 📚;')
);
self::assertStringContainsString(
'class=',
$extension->formatSql('CREATE DATABASE 📚;', true)
);
}

/**
* @group legacy
*/
public function testItDoesNotOutputDuplicatePreTags() : void
{
$extension = new DoctrineExtension();
self::assertSame(
1,
substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<pre')
);
self::assertSame(
1,
substr_count($extension->formatSQL('CREATE DATABASE 📚;', true), '<pre')
);
}

/**
* @group legacy
*/
public function testItUsesCssOnTheDivTag() : void
{
$extension = new DoctrineExtension();
self::assertSame(
1,
substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<div class=')
);
self::assertSame(
1,
substr_count($extension->formatQuery('CREATE DATABASE 📚;'), '<pre>')
);
}

public function testItUsesCssOnThePreTag() : void
{
$extension = new DoctrineExtension();
self::assertSame(
1,
substr_count($extension->formatSQL('CREATE DATABASE 📚;', true), '<pre class=')
);
}
}

class DummyClass
Expand Down
50 changes: 28 additions & 22 deletions Twig/DoctrineExtension.php
Expand Up @@ -2,7 +2,9 @@

namespace Doctrine\Bundle\DoctrineBundle\Twig;

use SqlFormatter;
use Doctrine\SqlFormatter\HtmlHighlighter;
use Doctrine\SqlFormatter\NullHighlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use Symfony\Component\VarDumper\Cloner\Data;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
Expand All @@ -12,6 +14,9 @@
*/
class DoctrineExtension extends AbstractExtension
{
/** @var SqlFormatter */
private $sqlFormatter;

/**
* Define our functions
*
Expand Down Expand Up @@ -168,45 +173,46 @@ public function formatQuery($sql, $highlightOnly = false)
{
@trigger_error(sprintf('The "%s()" method is deprecated and will be removed in DoctrineBundle 3.0.', __METHOD__), E_USER_DEPRECATED);

$this->setUpSqlFormatter();
$this->setUpSqlFormatter(true, true);

if ($highlightOnly) {
$html = SqlFormatter::highlight($sql);
$html = preg_replace('/<pre class=".*">([^"]*+)<\/pre>/Us', '\1', $html);
} else {
$html = SqlFormatter::format($sql);
$html = preg_replace('/<pre class="(.*)">([^"]*+)<\/pre>/Us', '<div class="\1"><pre>\2</pre></div>', $html);
return $this->sqlFormatter->highlight($sql);
}

return $html;
return sprintf(
'<div class="highlight highlight-sql"><pre>%s</pre></div>',
$this->sqlFormatter->format($sql)
);
}

public function prettifySql(string $sql) : string
{
$this->setUpSqlFormatter();

return SqlFormatter::highlight($sql);
return $this->sqlFormatter->highlight($sql);
}

public function formatSql(string $sql, bool $highlight) : string
{
$this->setUpSqlFormatter();
$this->setUpSqlFormatter($highlight);

return SqlFormatter::format($sql, $highlight);
return $this->sqlFormatter->format($sql);
}

private function setUpSqlFormatter() : void
private function setUpSqlFormatter(bool $highlight = true, bool $legacy = false) : void
{
SqlFormatter::$pre_attributes = 'class="highlight highlight-sql"';
SqlFormatter::$quote_attributes = 'class="string"';
SqlFormatter::$backtick_quote_attributes = 'class="string"';
SqlFormatter::$reserved_attributes = 'class="keyword"';
SqlFormatter::$boundary_attributes = 'class="symbol"';
SqlFormatter::$number_attributes = 'class="number"';
SqlFormatter::$word_attributes = 'class="word"';
SqlFormatter::$error_attributes = 'class="error"';
SqlFormatter::$comment_attributes = 'class="comment"';
SqlFormatter::$variable_attributes = 'class="variable"';
$this->sqlFormatter = new SqlFormatter($highlight ? new HtmlHighlighter([
HtmlHighlighter::HIGHLIGHT_PRE => 'class="highlight highlight-sql"',
HtmlHighlighter::HIGHLIGHT_QUOTE => 'class="string"',
HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'class="string"',
HtmlHighlighter::HIGHLIGHT_RESERVED => 'class="keyword"',
HtmlHighlighter::HIGHLIGHT_BOUNDARY => 'class="symbol"',
HtmlHighlighter::HIGHLIGHT_NUMBER => 'class="number"',
HtmlHighlighter::HIGHLIGHT_WORD => 'class="word"',
HtmlHighlighter::HIGHLIGHT_ERROR => 'class="error"',
HtmlHighlighter::HIGHLIGHT_COMMENT => 'class="comment"',
HtmlHighlighter::HIGHLIGHT_VARIABLE => 'class="variable"',
], ! $legacy) : new NullHighlighter());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -28,7 +28,7 @@
"php": "^7.1",
"doctrine/dbal": "^2.9.0",
"doctrine/persistence": "^1.3.3",
"jdorn/sql-formatter": "^1.2.16",
"doctrine/sql-formatter": "^1.0.1",
"symfony/cache": "^4.3.3|^5.0",
"symfony/config": "^4.3.3|^5.0",
"symfony/console": "^3.4.30|^4.3.3|^5.0",
Expand Down