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