1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Schema\Visitor; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
7
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
12
|
|
|
|
13
|
|
|
class CreateSchemaSqlCollectorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var AbstractPlatform|PHPUnit_Framework_MockObject_MockObject */ |
16
|
|
|
private $platformMock; |
17
|
|
|
|
18
|
|
|
/** @var CreateSchemaSqlCollector */ |
19
|
|
|
private $visitor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->platformMock = $this->getMockBuilder(AbstractPlatform::class) |
29
|
|
|
->setMethods( |
30
|
|
|
[ |
31
|
|
|
'getCreateForeignKeySQL', |
32
|
|
|
'getCreateSchemaSQL', |
33
|
|
|
'getCreateSequenceSQL', |
34
|
|
|
'getCreateTableSQL', |
35
|
|
|
'supportsForeignKeyConstraints', |
36
|
|
|
'getCreateViewSQL', |
37
|
|
|
'supportsSchemas', |
38
|
|
|
] |
39
|
|
|
) |
40
|
|
|
->getMockForAbstractClass(); |
41
|
|
|
$this->visitor = new CreateSchemaSqlCollector($this->platformMock); |
42
|
|
|
|
43
|
|
|
foreach (['getCreateSchemaSQL', 'getCreateTableSQL', 'getCreateForeignKeySQL', 'getCreateSequenceSQL', 'getCreateViewSQL'] as $method) { |
44
|
|
|
$this->platformMock->expects($this->any()) |
45
|
|
|
->method($method) |
46
|
|
|
->will($this->returnValue('foo')); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAcceptsNamespace() |
51
|
|
|
{ |
52
|
|
|
$this->platformMock->expects($this->at(0)) |
53
|
|
|
->method('supportsSchemas') |
54
|
|
|
->will($this->returnValue(false)); |
55
|
|
|
|
56
|
|
|
$this->platformMock->expects($this->at(1)) |
57
|
|
|
->method('supportsSchemas') |
58
|
|
|
->will($this->returnValue(true)); |
59
|
|
|
|
60
|
|
|
$this->visitor->acceptNamespace('foo'); |
61
|
|
|
|
62
|
|
|
self::assertEmpty($this->visitor->getQueries()); |
63
|
|
|
|
64
|
|
|
$this->visitor->acceptNamespace('foo'); |
65
|
|
|
|
66
|
|
|
self::assertSame(['foo'], $this->visitor->getQueries()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAcceptsTable() |
70
|
|
|
{ |
71
|
|
|
$table = $this->createTableMock(); |
72
|
|
|
|
73
|
|
|
$this->visitor->acceptTable($table); |
74
|
|
|
|
75
|
|
|
self::assertSame(['foo'], $this->visitor->getQueries()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testAcceptsForeignKey() |
79
|
|
|
{ |
80
|
|
|
$this->platformMock->expects($this->at(0)) |
81
|
|
|
->method('supportsForeignKeyConstraints') |
82
|
|
|
->will($this->returnValue(false)); |
83
|
|
|
|
84
|
|
|
$this->platformMock->expects($this->at(1)) |
85
|
|
|
->method('supportsForeignKeyConstraints') |
86
|
|
|
->will($this->returnValue(true)); |
87
|
|
|
|
88
|
|
|
$table = $this->createTableMock(); |
89
|
|
|
$foreignKey = $this->createForeignKeyConstraintMock(); |
90
|
|
|
|
91
|
|
|
$this->visitor->acceptForeignKey($table, $foreignKey); |
92
|
|
|
|
93
|
|
|
self::assertEmpty($this->visitor->getQueries()); |
94
|
|
|
|
95
|
|
|
$this->visitor->acceptForeignKey($table, $foreignKey); |
96
|
|
|
|
97
|
|
|
self::assertSame(['foo'], $this->visitor->getQueries()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testAcceptsSequences() |
101
|
|
|
{ |
102
|
|
|
$sequence = $this->createSequenceMock(); |
103
|
|
|
|
104
|
|
|
$this->visitor->acceptSequence($sequence); |
105
|
|
|
|
106
|
|
|
self::assertSame(['foo'], $this->visitor->getQueries()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testAcceptsView() |
110
|
|
|
{ |
111
|
|
|
$view = $this->createViewMock(); |
112
|
|
|
|
113
|
|
|
$this->visitor->acceptView($view); |
114
|
|
|
|
115
|
|
|
$this->assertSame(array('foo'), $this->visitor->getQueries()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testResetsQueries() |
119
|
|
|
{ |
120
|
|
|
foreach (['supportsSchemas', 'supportsForeignKeyConstraints'] as $method) { |
121
|
|
|
$this->platformMock->expects($this->any()) |
122
|
|
|
->method($method) |
123
|
|
|
->will($this->returnValue(true)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$table = $this->createTableMock(); |
127
|
|
|
$foreignKey = $this->createForeignKeyConstraintMock(); |
128
|
|
|
$sequence = $this->createSequenceMock(); |
129
|
|
|
$view = $this->createViewMock(); |
130
|
|
|
|
131
|
|
|
$this->visitor->acceptNamespace('foo'); |
132
|
|
|
$this->visitor->acceptTable($table); |
133
|
|
|
$this->visitor->acceptForeignKey($table, $foreignKey); |
134
|
|
|
$this->visitor->acceptSequence($sequence); |
135
|
|
|
$this->visitor->acceptView($view); |
136
|
|
|
|
137
|
|
|
self::assertNotEmpty($this->visitor->getQueries()); |
138
|
|
|
|
139
|
|
|
$this->visitor->resetQueries(); |
140
|
|
|
|
141
|
|
|
self::assertEmpty($this->visitor->getQueries()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return ForeignKeyConstraint|PHPUnit_Framework_MockObject_MockObject |
146
|
|
|
*/ |
147
|
|
|
private function createForeignKeyConstraintMock() |
148
|
|
|
{ |
149
|
|
|
return $this->getMockBuilder(ForeignKeyConstraint::class) |
150
|
|
|
->disableOriginalConstructor() |
151
|
|
|
->getMock(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Sequence|PHPUnit_Framework_MockObject_MockObject |
156
|
|
|
*/ |
157
|
|
|
private function createSequenceMock() |
158
|
|
|
{ |
159
|
|
|
return $this->getMockBuilder(Sequence::class) |
160
|
|
|
->disableOriginalConstructor() |
161
|
|
|
->getMock(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return Table|PHPUnit_Framework_MockObject_MockObject |
166
|
|
|
*/ |
167
|
|
|
private function createTableMock() |
168
|
|
|
{ |
169
|
|
|
return $this->getMockBuilder(Table::class) |
170
|
|
|
->disableOriginalConstructor() |
171
|
|
|
->getMock(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return \Doctrine\DBAL\Schema\View|\PHPUnit_Framework_MockObject_MockObject |
176
|
|
|
*/ |
177
|
|
|
private function createViewMock() |
178
|
|
|
{ |
179
|
|
|
return $this->getMockBuilder('Doctrine\DBAL\Schema\View') |
180
|
|
|
->disableOriginalConstructor() |
181
|
|
|
->getMock(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|