Skip to content

Commit

Permalink
Merge pull request #1473 from kamil-karkus/3.8.x
Browse files Browse the repository at this point in the history
Resolves #939 replaced single quotes with nowdoc
  • Loading branch information
greg0ire authored Nov 30, 2024
2 parents 3592645 + 07432c5 commit 72f58ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/Generator/SqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use function count;
use function get_class;
use function implode;
use function preg_replace;
use function sprintf;
use function str_repeat;
use function stripos;
use function strlen;
use function var_export;

/**
* The SqlGenerator class is responsible for generating the body of the up() and down() methods for a migration
Expand Down Expand Up @@ -61,7 +62,10 @@ public function generate(
}
}

$code[] = sprintf('$this->addSql(%s);', var_export($query, true));
$code[] = sprintf(
"\$this->addSql(<<<'SQL'\n%s\nSQL);",
preg_replace('/^/m', str_repeat(' ', 4), $query),
);
}

if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) {
Expand Down
49 changes: 38 additions & 11 deletions tests/Generator/SqlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
use PHPUnit\Framework\TestCase;

use function class_exists;
use function explode;
use function implode;
use function sprintf;
use function str_repeat;

// DBAL 3 compatibility
class_exists('Doctrine\DBAL\Platforms\SqlitePlatform');
Expand Down Expand Up @@ -47,10 +50,16 @@ public function testGenerate(): void
!\$this->connection->getDatabasePlatform() instanceof \\$expectedPlatform,
"Migration can only be executed safely on '\\$expectedPlatform'."
);
\$this->addSql('SELECT 1');
\$this->addSql('SELECT 2');
\$this->addSql('%s');
\$this->addSql(<<<'SQL'
SELECT 1
SQL);
\$this->addSql(<<<'SQL'
SELECT 2
SQL);
\$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

Expand All @@ -65,9 +74,15 @@ public function testGenerationWithoutCheckingDatabasePlatform(): void

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql('%s');
$this->addSql(<<<'SQL'
SELECT 1
SQL);
$this->addSql(<<<'SQL'
SELECT 2
SQL);
$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

Expand All @@ -82,9 +97,15 @@ public function testGenerationWithoutCheckingDatabasePlatformWithConfiguration()

$expectedCode = $this->prepareGeneratedCode(
<<<'CODE'
$this->addSql('SELECT 1');
$this->addSql('SELECT 2');
$this->addSql('%s');
$this->addSql(<<<'SQL'
SELECT 1
SQL);
$this->addSql(<<<'SQL'
SELECT 2
SQL);
$this->addSql(<<<'SQL'
%s
SQL);
CODE,
);

Expand Down Expand Up @@ -119,7 +140,13 @@ private function prepareGeneratedCode(string $expectedCode): string

return sprintf(
$expectedCode,
(new SqlFormatter(new NullHighlighter()))->format($this->sql[2]),
implode(
"\n" . str_repeat(' ', 4),
explode(
"\n",
(new SqlFormatter(new NullHighlighter()))->format($this->sql[2]),
),
),
);
}
}

0 comments on commit 72f58ae

Please sign in to comment.