-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
Implement platform-aware schema comparison #1229
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,9 @@ | |
use Doctrine\DBAL\Configuration as DBALConfiguration; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\Schema\Comparator; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\DBAL\Schema\SchemaDiff; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\Migrations\Generator\DiffGenerator; | ||
use Doctrine\Migrations\Generator\Generator; | ||
|
@@ -16,6 +18,8 @@ | |
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function method_exists; | ||
|
||
class DiffGeneratorTest extends TestCase | ||
{ | ||
/** @var DBALConfiguration|MockObject */ | ||
|
@@ -92,15 +96,42 @@ static function ($name): bool { | |
->method('dropTable') | ||
->will(self::onConsecutiveCalls('schema.table_name2', 'schema.table_name3')); | ||
|
||
$fromSchema->expects(self::once()) | ||
->method('getMigrateToSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['UPDATE table SET value = 2']); | ||
if (method_exists($this->schemaManager, 'createComparator')) { | ||
$schemaDiff = $this->createStub(SchemaDiff::class); | ||
$schemaDiff->method('toSql')->willReturn(self::onConsecutiveCalls( | ||
['UPDATE table SET value = 2'], | ||
['UPDATE table SET value = 1'] | ||
)); | ||
|
||
// regular mocks cannot be used here, because the method is static | ||
$comparator = new class extends Comparator { | ||
/** @var SchemaDiff */ | ||
public static $schemaDiff; | ||
|
||
public static function compareSchemas( | ||
Schema $fromSchema, | ||
Schema $toSchema | ||
): SchemaDiff { | ||
return self::$schemaDiff; | ||
} | ||
}; | ||
|
||
$comparator::$schemaDiff = $schemaDiff; | ||
|
||
$fromSchema->expects(self::once()) | ||
->method('getMigrateFromSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['UPDATE table SET value = 1']); | ||
$this->schemaManager->expects(self::once()) | ||
->method('createComparator') | ||
->willReturn($comparator); | ||
} else { | ||
$fromSchema->expects(self::once()) | ||
->method('getMigrateToSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['UPDATE table SET value = 2']); | ||
|
||
$fromSchema->expects(self::once()) | ||
->method('getMigrateFromSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['UPDATE table SET value = 1']); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to find a way to abide by "don't mock what you don't own" and reduce the big amount of mocking in this test, and gave up… I think it should be reworked, but I'm not sure how. |
||
|
||
$this->migrationSqlGenerator->expects(self::exactly(2)) | ||
->method('generate') | ||
|
@@ -115,7 +146,12 @@ static function ($name): bool { | |
->with('1234', 'test1', 'test2') | ||
->willReturn('path'); | ||
|
||
self::assertSame('path', $this->migrationDiffGenerator->generate('1234', '/table_name1/', true, 80)); | ||
self::assertSame('path', $this->migrationDiffGenerator->generate( | ||
'1234', | ||
'/table_name1/', | ||
true, | ||
80 | ||
)); | ||
} | ||
|
||
public function testGenerateFromEmptySchema(): void | ||
|
@@ -147,15 +183,42 @@ public function testGenerateFromEmptySchema(): void | |
$toSchema->expects(self::never()) | ||
->method('dropTable'); | ||
|
||
$emptySchema->expects(self::once()) | ||
->method('getMigrateToSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['CREATE TABLE table_name']); | ||
|
||
$emptySchema->expects(self::once()) | ||
->method('getMigrateFromSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['DROP TABLE table_name']); | ||
if (method_exists($this->schemaManager, 'createComparator')) { | ||
$schemaDiff = $this->createStub(SchemaDiff::class); | ||
$schemaDiff->method('toSql')->willReturn(self::onConsecutiveCalls( | ||
['CREATE TABLE table_name'], | ||
['DROP TABLE table_name'] | ||
)); | ||
|
||
// regular mocks cannot be used here, because the method is static | ||
$comparator = new class extends Comparator { | ||
/** @var SchemaDiff */ | ||
public static $schemaDiff; | ||
|
||
public static function compareSchemas( | ||
Schema $fromSchema, | ||
Schema $toSchema | ||
): SchemaDiff { | ||
return self::$schemaDiff; | ||
} | ||
}; | ||
|
||
$comparator::$schemaDiff = $schemaDiff; | ||
|
||
$this->schemaManager->expects(self::once()) | ||
->method('createComparator') | ||
->willReturn($comparator); | ||
} else { | ||
$emptySchema->expects(self::once()) | ||
->method('getMigrateToSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['CREATE TABLE table_name']); | ||
|
||
$emptySchema->expects(self::once()) | ||
->method('getMigrateFromSql') | ||
->with($toSchema, $this->platform) | ||
->willReturn(['DROP TABLE table_name']); | ||
} | ||
|
||
$this->migrationSqlGenerator->expects(self::exactly(2)) | ||
->method('generate') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I tried inlining the contents of this method to avoid the deprecation, but then I could no longer mock anything.