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