1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
6
|
|
|
use Doctrine\DBAL\Schema\SchemaDiff; |
7
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
10
|
|
|
use Doctrine\DBAL\Schema\View; |
11
|
|
|
use Doctrine\Tests\DBAL\Mocks\MockPlatform; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class SchemaDiffTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testSchemaDiffToSql() |
17
|
|
|
{ |
18
|
|
|
$diff = $this->createSchemaDiff(); |
19
|
|
|
$platform = $this->createPlatform(true); |
20
|
|
|
|
21
|
|
|
$sql = $diff->toSql($platform); |
22
|
|
|
|
23
|
|
|
$expected = [ |
24
|
|
|
'create_schema', |
25
|
|
|
'drop_orphan_fk', |
26
|
|
|
'alter_seq', |
27
|
|
|
'drop_seq', |
28
|
|
|
'create_seq', |
29
|
|
|
'drop_view', |
30
|
|
|
'change_view_drop', |
31
|
|
|
'change_view_add', |
32
|
|
|
'create_view', |
33
|
|
|
'create_table', |
34
|
|
|
'create_foreign_key', |
35
|
|
|
'drop_table', |
36
|
|
|
'alter_table', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
self::assertEquals($expected, $sql); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSchemaDiffToSaveSql() |
43
|
|
|
{ |
44
|
|
|
$diff = $this->createSchemaDiff(); |
45
|
|
|
$platform = $this->createPlatform(false); |
46
|
|
|
|
47
|
|
|
$sql = $diff->toSaveSql($platform); |
48
|
|
|
|
49
|
|
|
$expected = [ |
50
|
|
|
'create_schema', |
51
|
|
|
'alter_seq', |
52
|
|
|
'create_seq', |
53
|
|
|
'change_view_drop', |
54
|
|
|
'change_view_add', |
55
|
|
|
'create_view', |
56
|
|
|
'create_table', |
57
|
|
|
'create_foreign_key', |
58
|
|
|
'alter_table', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
self::assertEquals($expected, $sql); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function createPlatform($unsafe = false) |
65
|
|
|
{ |
66
|
|
|
$platform = $this->createMock(MockPlatform::class); |
67
|
|
|
$platform->expects($this->exactly(1)) |
68
|
|
|
->method('getCreateSchemaSQL') |
69
|
|
|
->with('foo_ns') |
70
|
|
|
->will($this->returnValue('create_schema')); |
71
|
|
|
if ($unsafe) { |
72
|
|
|
$platform->expects($this->exactly(1)) |
73
|
|
|
->method('getDropSequenceSql') |
74
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
75
|
|
|
->will($this->returnValue('drop_seq')); |
76
|
|
|
} |
77
|
|
|
$platform->expects($this->exactly(1)) |
78
|
|
|
->method('getAlterSequenceSql') |
79
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
80
|
|
|
->will($this->returnValue('alter_seq')); |
81
|
|
|
$platform->expects($this->exactly(1)) |
82
|
|
|
->method('getCreateSequenceSql') |
83
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
84
|
|
|
->will($this->returnValue('create_seq')); |
85
|
|
|
if ($unsafe) { |
86
|
|
|
$platform->expects($this->exactly(2)) |
87
|
|
|
->method('getDropViewSQL') |
88
|
|
|
->with($this->logicalOr( |
89
|
|
|
$this->equalTo('bar_view'), |
90
|
|
|
$this->equalTo('baz_view') |
91
|
|
|
)) |
92
|
|
|
->will($this->returnCallback(static function ($arg) { |
93
|
|
|
return [ |
94
|
|
|
'bar_view' => 'drop_view', |
95
|
|
|
'baz_view' => 'change_view_drop', |
96
|
|
|
][$arg] ?? null; |
97
|
|
|
})); |
98
|
|
|
} else { |
99
|
|
|
$platform->expects($this->exactly(1)) |
100
|
|
|
->method('getDropViewSQL') |
101
|
|
|
->with($this->equalTo('baz_view')) |
102
|
|
|
->will($this->returnValue('change_view_drop')); |
103
|
|
|
} |
104
|
|
|
$platform->expects($this->exactly(2)) |
105
|
|
|
->method('getCreateViewSQL') |
106
|
|
|
->with($this->logicalOr( |
107
|
|
|
$this->equalTo('baz_view'), |
108
|
|
|
$this->equalTo('foo_view') |
109
|
|
|
), '') |
110
|
|
|
->will($this->returnCallback(static function ($arg) { |
111
|
|
|
return [ |
112
|
|
|
'baz_view' => 'change_view_add', |
113
|
|
|
'foo_view' => 'create_view', |
114
|
|
|
][$arg] ?? null; |
115
|
|
|
})); |
116
|
|
|
if ($unsafe) { |
117
|
|
|
$platform->expects($this->exactly(1)) |
118
|
|
|
->method('getDropTableSql') |
119
|
|
|
->with($this->isInstanceOf(Table::class)) |
120
|
|
|
->will($this->returnValue('drop_table')); |
121
|
|
|
} |
122
|
|
|
$platform->expects($this->exactly(1)) |
123
|
|
|
->method('getCreateTableSql') |
124
|
|
|
->with($this->isInstanceOf(Table::class)) |
125
|
|
|
->will($this->returnValue(['create_table'])); |
126
|
|
|
$platform->expects($this->exactly(1)) |
127
|
|
|
->method('getCreateForeignKeySQL') |
128
|
|
|
->with($this->isInstanceOf(ForeignKeyConstraint::class)) |
129
|
|
|
->will($this->returnValue('create_foreign_key')); |
130
|
|
|
$platform->expects($this->exactly(1)) |
131
|
|
|
->method('getAlterTableSql') |
132
|
|
|
->with($this->isInstanceOf(TableDiff::class)) |
133
|
|
|
->will($this->returnValue(['alter_table'])); |
134
|
|
|
if ($unsafe) { |
135
|
|
|
$platform->expects($this->exactly(1)) |
136
|
|
|
->method('getDropForeignKeySql') |
137
|
|
|
->with( |
138
|
|
|
$this->isInstanceOf(ForeignKeyConstraint::class), |
139
|
|
|
$this->isInstanceOf(Table::class) |
140
|
|
|
) |
141
|
|
|
->will($this->returnValue('drop_orphan_fk')); |
142
|
|
|
} |
143
|
|
|
$platform->expects($this->exactly(1)) |
144
|
|
|
->method('supportsSchemas') |
145
|
|
|
->will($this->returnValue(true)); |
146
|
|
|
$platform->expects($this->exactly(1)) |
147
|
|
|
->method('supportsSequences') |
148
|
|
|
->will($this->returnValue(true)); |
149
|
|
|
$platform->expects($this->exactly(1)) |
150
|
|
|
->method('supportsViews') |
151
|
|
|
->will($this->returnValue(true)); |
152
|
|
|
$platform->expects($this->exactly(2)) |
153
|
|
|
->method('supportsForeignKeyConstraints') |
154
|
|
|
->will($this->returnValue(true)); |
155
|
|
|
return $platform; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function createSchemaDiff() |
159
|
|
|
{ |
160
|
|
|
$diff = new SchemaDiff(); |
161
|
|
|
$diff->newNamespaces['foo_ns'] = 'foo_ns'; |
162
|
|
|
$diff->removedNamespaces['bar_ns'] = 'bar_ns'; |
163
|
|
|
$diff->changedSequences['foo_seq'] = new Sequence('foo_seq'); |
164
|
|
|
$diff->newSequences['bar_seq'] = new Sequence('bar_seq'); |
165
|
|
|
$diff->removedSequences['baz_seq'] = new Sequence('baz_seq'); |
166
|
|
|
$diff->newViews['foo_view'] = new View('foo_view', ''); |
167
|
|
|
$diff->removedViews['bar_view'] = new View('bar_view', ''); |
168
|
|
|
$diff->changedViews['baz_view'] = new View('baz_view', ''); |
169
|
|
|
$diff->newTables['foo_table'] = new Table('foo_table'); |
170
|
|
|
$diff->removedTables['bar_table'] = new Table('bar_table'); |
171
|
|
|
$diff->changedTables['baz_table'] = new TableDiff('baz_table'); |
172
|
|
|
$diff->newTables['foo_table']->addColumn('foreign_id', 'integer'); |
173
|
|
|
$diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']); |
174
|
|
|
$fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']); |
175
|
|
|
$fk->setLocalTable(new Table('local_table')); |
176
|
|
|
$diff->orphanedForeignKeys[] = $fk; |
177
|
|
|
return $diff; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|