From 108292b65d3035b1dc9b4999302422dd10b5aa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Fri, 22 May 2020 14:29:51 +0200 Subject: [PATCH] Replace unmaintained dependency Closes #785 --- Tests/ProfilerTest.php | 16 +++++--- Tests/Twig/DoctrineExtensionTest.php | 57 ++++++++++++++++++++++++++++ Twig/DoctrineExtension.php | 50 +++++++++++++----------- composer.json | 2 +- 4 files changed, 96 insertions(+), 29 deletions(-) diff --git a/Tests/ProfilerTest.php b/Tests/ProfilerTest.php index ee0031e6b..8276d554f 100644 --- a/Tests/ProfilerTest.php +++ b/Tests/ProfilerTest.php @@ -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( + ' ', + '.*', + preg_quote('SELECT * FROM foo WHERE bar IN ( ? , ? )') + ) . '/', $output)); } } diff --git a/Tests/Twig/DoctrineExtensionTest.php b/Tests/Twig/DoctrineExtensionTest.php index 1dc59112a..48e8823da 100644 --- a/Tests/Twig/DoctrineExtensionTest.php +++ b/Tests/Twig/DoctrineExtensionTest.php @@ -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 📚;'), 'formatSQL('CREATE DATABASE 📚;', true), 'formatQuery('CREATE DATABASE 📚;'), '
') + ); + } + + public function testItUsesCssOnThePreTag() : void + { + $extension = new DoctrineExtension(); + self::assertSame( + 1, + substr_count($extension->formatSQL('CREATE DATABASE 📚;', true), '
setUpSqlFormatter();
+        $this->setUpSqlFormatter(true, true);
 
         if ($highlightOnly) {
-            $html = SqlFormatter::highlight($sql);
-            $html = preg_replace('/
([^"]*+)<\/pre>/Us', '\1', $html);
-        } else {
-            $html = SqlFormatter::format($sql);
-            $html = preg_replace('/
([^"]*+)<\/pre>/Us', '
\2
', $html); + return $this->sqlFormatter->highlight($sql); } - return $html; + return sprintf( + '
%s
', + $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()); } /** diff --git a/composer.json b/composer.json index 79547dd2f..e9b002a45 100644 --- a/composer.json +++ b/composer.json @@ -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",