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
|
|
|
|
16
|
|
|
class SchemaDiffTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testSchemaDiffToSql() : void |
19
|
|
|
{ |
20
|
|
|
$diff = $this->createSchemaDiff(); |
21
|
|
|
$platform = $this->createPlatform(true); |
22
|
|
|
|
23
|
|
|
$sql = $diff->toSql($platform); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$expected = ['create_schema', 'drop_orphan_fk', 'alter_seq', 'drop_seq', 'create_seq', 'create_table', 'create_foreign_key', 'drop_table', 'alter_table']; |
26
|
|
|
|
27
|
|
|
self::assertEquals($expected, $sql); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testSchemaDiffToSaveSql() : void |
31
|
|
|
{ |
32
|
|
|
$diff = $this->createSchemaDiff(); |
33
|
|
|
$platform = $this->createPlatform(false); |
34
|
|
|
|
35
|
|
|
$sql = $diff->toSaveSql($platform); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$expected = ['create_schema', 'alter_seq', 'create_seq', 'create_table', 'create_foreign_key', 'alter_table']; |
38
|
|
|
|
39
|
|
|
self::assertEquals($expected, $sql); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return AbstractPlatform|MockObject |
44
|
|
|
*/ |
45
|
|
|
private function createPlatform(bool $unsafe) |
46
|
|
|
{ |
47
|
|
|
/** @var AbstractPlatform|MockObject $platform */ |
48
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
49
|
|
|
$platform->expects($this->exactly(1)) |
|
|
|
|
50
|
|
|
->method('getCreateSchemaSQL') |
51
|
|
|
->with('foo_ns') |
52
|
|
|
->will($this->returnValue('create_schema')); |
53
|
|
|
if ($unsafe) { |
54
|
|
|
$platform->expects($this->exactly(1)) |
55
|
|
|
->method('getDropSequenceSql') |
56
|
|
|
->with($this->isInstanceOf(Sequence::class)) |
57
|
|
|
->will($this->returnValue('drop_seq')); |
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
|
|
|
$platform->expects($this->exactly(1)) |
74
|
|
|
->method('getCreateTableSql') |
75
|
|
|
->with($this->isInstanceOf(Table::class)) |
76
|
|
|
->will($this->returnValue(['create_table'])); |
77
|
|
|
$platform->expects($this->exactly(1)) |
78
|
|
|
->method('getCreateForeignKeySQL') |
79
|
|
|
->with($this->isInstanceOf(ForeignKeyConstraint::class)) |
80
|
|
|
->will($this->returnValue('create_foreign_key')); |
81
|
|
|
$platform->expects($this->exactly(1)) |
82
|
|
|
->method('getAlterTableSql') |
83
|
|
|
->with($this->isInstanceOf(TableDiff::class)) |
84
|
|
|
->will($this->returnValue(['alter_table'])); |
85
|
|
|
if ($unsafe) { |
86
|
|
|
$platform->expects($this->exactly(1)) |
87
|
|
|
->method('getDropForeignKeySql') |
88
|
|
|
->with( |
89
|
|
|
$this->isInstanceOf(ForeignKeyConstraint::class), |
90
|
|
|
$this->isInstanceOf(Table::class) |
91
|
|
|
) |
92
|
|
|
->will($this->returnValue('drop_orphan_fk')); |
93
|
|
|
} |
94
|
|
|
$platform->expects($this->exactly(1)) |
95
|
|
|
->method('supportsSchemas') |
96
|
|
|
->will($this->returnValue(true)); |
97
|
|
|
$platform->expects($this->exactly(1)) |
98
|
|
|
->method('supportsSequences') |
99
|
|
|
->will($this->returnValue(true)); |
100
|
|
|
$platform->expects($this->exactly(2)) |
101
|
|
|
->method('supportsForeignKeyConstraints') |
102
|
|
|
->will($this->returnValue(true)); |
103
|
|
|
|
104
|
|
|
return $platform; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function createSchemaDiff() : SchemaDiff |
108
|
|
|
{ |
109
|
|
|
$diff = new SchemaDiff(); |
110
|
|
|
$diff->newNamespaces['foo_ns'] = 'foo_ns'; |
111
|
|
|
$diff->removedNamespaces['bar_ns'] = 'bar_ns'; |
112
|
|
|
$diff->changedSequences[] = new Sequence('foo_seq'); |
113
|
|
|
$diff->newSequences[] = new Sequence('bar_seq'); |
114
|
|
|
$diff->removedSequences[] = new Sequence('baz_seq'); |
115
|
|
|
$diff->newTables['foo_table'] = new Table('foo_table'); |
116
|
|
|
$diff->removedTables['bar_table'] = new Table('bar_table'); |
117
|
|
|
$diff->changedTables['baz_table'] = new TableDiff('baz_table'); |
118
|
|
|
$diff->newTables['foo_table']->addColumn('foreign_id', 'integer'); |
119
|
|
|
$diff->newTables['foo_table']->addForeignKeyConstraint('foreign_table', ['foreign_id'], ['id']); |
120
|
|
|
$fk = new ForeignKeyConstraint(['id'], 'foreign_table', ['id']); |
121
|
|
|
$fk->setLocalTable(new Table('local_table')); |
122
|
|
|
$diff->orphanedForeignKeys[] = $fk; |
123
|
|
|
|
124
|
|
|
return $diff; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.