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