|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
7
|
|
|
use Doctrine\DBAL\Schema\SchemaDiff; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
10
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use function assert; |
|
14
|
|
|
|
|
15
|
|
|
class SchemaDiffTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testSchemaDiffToSql() : void |
|
18
|
|
|
{ |
|
19
|
|
|
$diff = $this->createSchemaDiff(); |
|
20
|
|
|
$platform = $this->createPlatform(true); |
|
21
|
|
|
|
|
22
|
|
|
$sql = $diff->toSql($platform); |
|
23
|
|
|
|
|
24
|
|
|
$expected = ['create_schema', 'drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table']; |
|
25
|
|
|
|
|
26
|
|
|
self::assertEquals($expected, $sql); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testSchemaDiffToSaveSql() : void |
|
30
|
|
|
{ |
|
31
|
|
|
$diff = $this->createSchemaDiff(); |
|
32
|
|
|
$platform = $this->createPlatform(false); |
|
33
|
|
|
|
|
34
|
|
|
$sql = $diff->toSaveSql($platform); |
|
35
|
|
|
|
|
36
|
|
|
$expected = ['create_schema', 'alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table']; |
|
37
|
|
|
|
|
38
|
|
|
self::assertEquals($expected, $sql); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return AbstractPlatform|MockObject |
|
43
|
|
|
*/ |
|
44
|
|
|
private function createPlatform(bool $unsafe) |
|
45
|
|
|
{ |
|
46
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
|
47
|
|
|
assert($platform instanceof AbstractPlatform || $platform instanceof MockObject); |
|
48
|
|
|
$platform->expects($this->exactly(1)) |
|
49
|
|
|
->method('getCreateSchemaSQL') |
|
50
|
|
|
->with('foo_ns') |
|
51
|
|
|
->will($this->returnValue('create_schema')); |
|
52
|
|
|
if ($unsafe) { |
|
53
|
|
|
$platform->expects($this->exactly(1)) |
|
54
|
|
|
->method('getDropSequenceSql') |
|
55
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
|
56
|
|
|
->will($this->returnValue('drop_seq')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$platform->expects($this->exactly(1)) |
|
60
|
|
|
->method('getAlterSequenceSql') |
|
61
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
|
62
|
|
|
->will($this->returnValue('alter_seq')); |
|
63
|
|
|
$platform->expects($this->exactly(1)) |
|
64
|
|
|
->method('getCreateSequenceSql') |
|
65
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
|
66
|
|
|
->will($this->returnValue('create_seq')); |
|
67
|
|
|
if ($unsafe) { |
|
68
|
|
|
$platform->expects($this->exactly(1)) |
|
69
|
|
|
->method('getDropTableSql') |
|
70
|
|
|
->with($this->isInstanceOf(Table::class)) |
|
71
|
|
|
->will($this->returnValue('drop_table')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$platform->expects($this->exactly(1)) |
|
75
|
|
|
->method('getCreateTableSql') |
|
76
|
|
|
->with($this->isInstanceOf(Table::class)) |
|
77
|
|
|
->will($this->returnValue(['create_table'])); |
|
78
|
|
|
$platform->expects($this->exactly(1)) |
|
79
|
|
|
->method('getCreateForeignKeySQL') |
|
80
|
|
|
->with($this->isInstanceOf(ForeignKeyConstraint::class)) |
|
81
|
|
|
->will($this->returnValue('create_foreign_key')); |
|
82
|
|
|
$platform->expects($this->exactly(1)) |
|
83
|
|
|
->method('getAlterTableSql') |
|
84
|
|
|
->with($this->isInstanceOf(TableDiff::class)) |
|
85
|
|
|
->will($this->returnValue(['alter_table'])); |
|
86
|
|
|
if ($unsafe) { |
|
87
|
|
|
$platform->expects($this->exactly(1)) |
|
88
|
|
|
->method('getDropForeignKeySql') |
|
89
|
|
|
->with( |
|
90
|
|
|
$this->isInstanceOf(ForeignKeyConstraint::class), |
|
91
|
|
|
$this->isInstanceOf(Table::class) |
|
92
|
|
|
) |
|
93
|
|
|
->will($this->returnValue('drop_orphan_fk')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$platform->expects($this->exactly(1)) |
|
97
|
|
|
->method('supportsSchemas') |
|
98
|
|
|
->will($this->returnValue(true)); |
|
99
|
|
|
$platform->expects($this->exactly(1)) |
|
100
|
|
|
->method('supportsSequences') |
|
101
|
|
|
->will($this->returnValue(true)); |
|
102
|
|
|
$platform->expects($this->exactly(2)) |
|
103
|
|
|
->method('supportsForeignKeyConstraints') |
|
104
|
|
|
->will($this->returnValue(true)); |
|
105
|
|
|
|
|
106
|
|
|
return $platform; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function createSchemaDiff() : SchemaDiff |
|
110
|
|
|
{ |
|
111
|
|
|
$diff = new SchemaDiff(); |
|
112
|
|
|
$diff->newNamespaces['foo_ns'] = 'foo_ns'; |
|
113
|
|
|
$diff->removedNamespaces['bar_ns'] = 'bar_ns'; |
|
114
|
|
|
$diff->changedSequences['foo_seq'] = new Sequence('foo_seq'); |
|
115
|
|
|
$diff->newSequences['bar_seq'] = new Sequence('bar_seq'); |
|
116
|
|
|
$diff->removedSequences['baz_seq'] = new Sequence('baz_seq'); |
|
117
|
|
|
$diff->newTables['foo_table'] = new Table('foo_table'); |
|
118
|
|
|
$diff->removedTables['bar_table'] = new Table('bar_table'); |
|
119
|
|
|
$diff->changedTables['baz_table'] = new TableDiff('baz_table'); |
|
120
|
|
|
$diff->newTables['foo_table']->addColumn('foreign_id', 'integer'); |
|
121
|
|
|
$diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']); |
|
122
|
|
|
$fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']); |
|
123
|
|
|
$fk->setLocalTable(new Table('local_table')); |
|
124
|
|
|
$diff->orphanedForeignKeys[] = $fk; |
|
125
|
|
|
|
|
126
|
|
|
return $diff; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|