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
|
|
|
|
14
|
|
|
class SchemaDiffTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testSchemaDiffToSql() : void |
17
|
|
|
{ |
18
|
|
|
$diff = $this->createSchemaDiff(); |
19
|
|
|
$platform = $this->createPlatform(true); |
20
|
|
|
|
21
|
|
|
$sql = $diff->toSql($platform); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$expected = ['create_schema', 'drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table']; |
24
|
|
|
|
25
|
|
|
self::assertEquals($expected, $sql); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testSchemaDiffToSaveSql() : void |
29
|
|
|
{ |
30
|
|
|
$diff = $this->createSchemaDiff(); |
31
|
|
|
$platform = $this->createPlatform(false); |
32
|
|
|
|
33
|
|
|
$sql = $diff->toSaveSql($platform); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$expected = ['create_schema', 'alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table']; |
36
|
|
|
|
37
|
|
|
self::assertEquals($expected, $sql); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return AbstractPlatform|MockObject |
42
|
|
|
*/ |
43
|
|
|
private function createPlatform(bool $unsafe) |
44
|
|
|
{ |
45
|
|
|
/** @var AbstractPlatform|MockObject $platform */ |
46
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
47
|
|
|
$platform->expects($this->exactly(1)) |
|
|
|
|
48
|
|
|
->method('getCreateSchemaSQL') |
49
|
|
|
->with('foo_ns') |
50
|
|
|
->will($this->returnValue('create_schema')); |
51
|
|
|
if ($unsafe) { |
52
|
|
|
$platform->expects($this->exactly(1)) |
53
|
|
|
->method('getDropSequenceSql') |
54
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
55
|
|
|
->will($this->returnValue('drop_seq')); |
56
|
|
|
} |
57
|
|
|
$platform->expects($this->exactly(1)) |
58
|
|
|
->method('getAlterSequenceSql') |
59
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
60
|
|
|
->will($this->returnValue('alter_seq')); |
61
|
|
|
$platform->expects($this->exactly(1)) |
62
|
|
|
->method('getCreateSequenceSql') |
63
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
64
|
|
|
->will($this->returnValue('create_seq')); |
65
|
|
|
if ($unsafe) { |
66
|
|
|
$platform->expects($this->exactly(1)) |
67
|
|
|
->method('getDropTableSql') |
68
|
|
|
->with($this->isInstanceOf(Table::class)) |
69
|
|
|
->will($this->returnValue('drop_table')); |
70
|
|
|
} |
71
|
|
|
$platform->expects($this->exactly(1)) |
72
|
|
|
->method('getCreateTableSql') |
73
|
|
|
->with($this->isInstanceOf(Table::class)) |
74
|
|
|
->will($this->returnValue(['create_table'])); |
75
|
|
|
$platform->expects($this->exactly(1)) |
76
|
|
|
->method('getCreateForeignKeySQL') |
77
|
|
|
->with($this->isInstanceOf(ForeignKeyConstraint::class)) |
78
|
|
|
->will($this->returnValue('create_foreign_key')); |
79
|
|
|
$platform->expects($this->exactly(1)) |
80
|
|
|
->method('getAlterTableSql') |
81
|
|
|
->with($this->isInstanceOf(TableDiff::class)) |
82
|
|
|
->will($this->returnValue(['alter_table'])); |
83
|
|
|
if ($unsafe) { |
84
|
|
|
$platform->expects($this->exactly(1)) |
85
|
|
|
->method('getDropForeignKeySql') |
86
|
|
|
->with( |
87
|
|
|
$this->isInstanceOf(ForeignKeyConstraint::class), |
88
|
|
|
$this->isInstanceOf(Table::class) |
89
|
|
|
) |
90
|
|
|
->will($this->returnValue('drop_orphan_fk')); |
91
|
|
|
} |
92
|
|
|
$platform->expects($this->exactly(1)) |
93
|
|
|
->method('supportsSchemas') |
94
|
|
|
->will($this->returnValue(true)); |
95
|
|
|
$platform->expects($this->exactly(1)) |
96
|
|
|
->method('supportsSequences') |
97
|
|
|
->will($this->returnValue(true)); |
98
|
|
|
$platform->expects($this->exactly(2)) |
99
|
|
|
->method('supportsForeignKeyConstraints') |
100
|
|
|
->will($this->returnValue(true)); |
101
|
|
|
|
102
|
|
|
return $platform; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function createSchemaDiff() : SchemaDiff |
106
|
|
|
{ |
107
|
|
|
$diff = new SchemaDiff(); |
108
|
|
|
$diff->newNamespaces['foo_ns'] = 'foo_ns'; |
109
|
|
|
$diff->removedNamespaces['bar_ns'] = 'bar_ns'; |
110
|
|
|
$diff->changedSequences['foo_seq'] = new Sequence('foo_seq'); |
111
|
|
|
$diff->newSequences['bar_seq'] = new Sequence('bar_seq'); |
112
|
|
|
$diff->removedSequences['baz_seq'] = new Sequence('baz_seq'); |
113
|
|
|
$diff->newTables['foo_table'] = new Table('foo_table'); |
114
|
|
|
$diff->removedTables['bar_table'] = new Table('bar_table'); |
115
|
|
|
$diff->changedTables['baz_table'] = new TableDiff('baz_table'); |
116
|
|
|
$diff->newTables['foo_table']->addColumn('foreign_id', 'integer'); |
117
|
|
|
$diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']); |
118
|
|
|
$fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']); |
119
|
|
|
$fk->setLocalTable(new Table('local_table')); |
120
|
|
|
$diff->orphanedForeignKeys[] = $fk; |
121
|
|
|
|
122
|
|
|
return $diff; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.