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

Resolves #939 replaced single quotes with nowdoc #1473

Merged
merged 1 commit into from
Nov 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]),
),
),
);
}
}