|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
6
|
|
|
use Doctrine\ORM\Tools\ToolEvents; |
|
7
|
|
|
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs; |
|
8
|
|
|
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs; |
|
9
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
10
|
|
|
|
|
11
|
|
|
class SchemaToolTest extends OrmTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testAddUniqueIndexForUniqueFieldAnnotation() |
|
14
|
|
|
{ |
|
15
|
|
|
$em = $this->_getTestEntityManager(); |
|
16
|
|
|
$schemaTool = new SchemaTool($em); |
|
17
|
|
|
|
|
18
|
|
|
$classes = array( |
|
19
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'), |
|
20
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'), |
|
21
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'), |
|
22
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'), |
|
23
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'), |
|
24
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'), |
|
25
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue($schema->hasTable('cms_users'), "Table cms_users should exist."); |
|
31
|
|
|
$this->assertTrue($schema->getTable('cms_users')->columnsAreIndexed(array('username')), "username column should be indexed."); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testAnnotationOptionsAttribute() |
|
35
|
|
|
{ |
|
36
|
|
|
$em = $this->_getTestEntityManager(); |
|
37
|
|
|
$schemaTool = new SchemaTool($em); |
|
38
|
|
|
|
|
39
|
|
|
$classes = array( |
|
40
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\TestEntityWithAnnotationOptionsAttribute'), |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
44
|
|
|
|
|
45
|
|
|
$expected = array('foo' => 'bar', 'baz' => array('key' => 'val')); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getOptions(), "options annotation are passed to the tables options"); |
|
48
|
|
|
$this->assertEquals($expected, $schema->getTable('TestEntityWithAnnotationOptionsAttribute')->getColumn('test')->getCustomSchemaOptions(), "options annotation are passed to the columns customSchemaOptions"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @group DDC-200 |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testPassColumnDefinitionToJoinColumn() |
|
55
|
|
|
{ |
|
56
|
|
|
$customColumnDef = "MEDIUMINT(6) UNSIGNED NOT NULL"; |
|
57
|
|
|
|
|
58
|
|
|
$em = $this->_getTestEntityManager(); |
|
59
|
|
|
$schemaTool = new SchemaTool($em); |
|
60
|
|
|
|
|
61
|
|
|
$avatar = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumAvatar'); |
|
62
|
|
|
$avatar->fieldMappings['id']['columnDefinition'] = $customColumnDef; |
|
63
|
|
|
$user = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumUser'); |
|
64
|
|
|
|
|
65
|
|
|
$classes = array($avatar, $user); |
|
66
|
|
|
|
|
67
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertTrue($schema->hasTable('forum_users')); |
|
70
|
|
|
$table = $schema->getTable("forum_users"); |
|
71
|
|
|
$this->assertTrue($table->hasColumn('avatar_id')); |
|
72
|
|
|
$this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @group DDC-283 |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testPostGenerateEvents() |
|
79
|
|
|
{ |
|
80
|
|
|
$listener = new GenerateSchemaEventListener(); |
|
81
|
|
|
|
|
82
|
|
|
$em = $this->_getTestEntityManager(); |
|
83
|
|
|
$em->getEventManager()->addEventListener( |
|
84
|
|
|
array(ToolEvents::postGenerateSchemaTable, ToolEvents::postGenerateSchema), $listener |
|
85
|
|
|
); |
|
86
|
|
|
$schemaTool = new SchemaTool($em); |
|
87
|
|
|
|
|
88
|
|
|
$classes = array( |
|
89
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'), |
|
90
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle'), |
|
91
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsComment'), |
|
92
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsEmployee'), |
|
93
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup'), |
|
94
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'), |
|
95
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals(count($classes), $listener->tableCalls); |
|
101
|
|
|
$this->assertTrue($listener->schemaCalled); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testNullDefaultNotAddedToCustomSchemaOptions() |
|
105
|
|
|
{ |
|
106
|
|
|
$em = $this->_getTestEntityManager(); |
|
107
|
|
|
$schemaTool = new SchemaTool($em); |
|
108
|
|
|
|
|
109
|
|
|
$classes = array( |
|
110
|
|
|
$em->getClassMetadata('Doctrine\Tests\Models\NullDefault\NullDefaultColumn'), |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$customSchemaOptions = $schemaTool->getSchemaFromMetadata($classes) |
|
114
|
|
|
->getTable('NullDefaultColumn') |
|
115
|
|
|
->getColumn('nullDefault') |
|
116
|
|
|
->getCustomSchemaOptions(); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertSame(array(), $customSchemaOptions); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @group DDC-3671 |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation() |
|
125
|
|
|
{ |
|
126
|
|
|
$em = $this->_getTestEntityManager(); |
|
127
|
|
|
$schemaTool = new SchemaTool($em); |
|
128
|
|
|
$classes = [ |
|
129
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\UniqueConstraintAnnotationModel'), |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertTrue($schema->hasTable('unique_constraint_annotation_table')); |
|
135
|
|
|
$table = $schema->getTable('unique_constraint_annotation_table'); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals(2, count($table->getIndexes())); |
|
138
|
|
|
$this->assertTrue($table->hasIndex('primary')); |
|
139
|
|
|
$this->assertTrue($table->hasIndex('uniq_hash')); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testRemoveUniqueIndexOverruledByPrimaryKey() |
|
143
|
|
|
{ |
|
144
|
|
|
$em = $this->_getTestEntityManager(); |
|
145
|
|
|
$schemaTool = new SchemaTool($em); |
|
146
|
|
|
$classes = [ |
|
147
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\FirstEntity'), |
|
148
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\SecondEntity') |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertTrue($schema->hasTable('first_entity'), "Table first_entity should exist."); |
|
154
|
|
|
|
|
155
|
|
|
$indexes = $schema->getTable('first_entity')->getIndexes(); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertCount(1, $indexes, "there should be only one index"); |
|
158
|
|
|
$this->assertTrue(current($indexes)->isPrimary(), "index should be primary"); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testUpdateSchemaSqlWithManyToManyRelationshipM2MEntityAtTheBottom() |
|
162
|
|
|
{ |
|
163
|
|
|
$em = $this->_getTestEntityManager(); |
|
164
|
|
|
$schemaTool = new SchemaTool($em); |
|
165
|
|
|
$classes = [ |
|
166
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\Author'), |
|
167
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\Book'), |
|
168
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\AuthorBook'), |
|
169
|
|
|
]; |
|
170
|
|
|
|
|
171
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
172
|
|
|
$this->assertTrue($schema->hasTable('author'), "Table author should exist."); |
|
173
|
|
|
$this->assertTrue($schema->hasTable('book'), "Table book should exist."); |
|
174
|
|
|
$this->assertTrue($schema->hasTable('author_book'), "Table author_book should exist."); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testUpdateSchemaSqlWithManyToManyRelationshipOrderedByM2mEntityAtTheTop() |
|
178
|
|
|
{ |
|
179
|
|
|
$em = $this->_getTestEntityManager(); |
|
180
|
|
|
$schemaTool = new SchemaTool($em); |
|
181
|
|
|
$classes = [ |
|
182
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\AuthorBook'), |
|
183
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\Author'), |
|
184
|
|
|
$em->getClassMetadata(__NAMESPACE__ . '\\Book') |
|
185
|
|
|
]; |
|
186
|
|
|
|
|
187
|
|
|
$schema = $schemaTool->getSchemaFromMetadata($classes); |
|
188
|
|
|
$this->assertTrue($schema->hasTable('author'), "Table author should exist."); |
|
189
|
|
|
$this->assertTrue($schema->hasTable('book'), "Table book should exist."); |
|
190
|
|
|
$this->assertTrue($schema->hasTable('author_book'), "Table author_book should exist."); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @Entity |
|
196
|
|
|
* @Table(options={"foo": "bar", "baz": {"key": "val"}}) |
|
197
|
|
|
*/ |
|
198
|
|
|
class TestEntityWithAnnotationOptionsAttribute |
|
199
|
|
|
{ |
|
200
|
|
|
/** @Id @Column */ |
|
201
|
|
|
private $id; |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @Column(type="string", options={"foo": "bar", "baz": {"key": "val"}}) |
|
205
|
|
|
*/ |
|
206
|
|
|
private $test; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
class GenerateSchemaEventListener |
|
210
|
|
|
{ |
|
211
|
|
|
public $tableCalls = 0; |
|
212
|
|
|
public $schemaCalled = false; |
|
213
|
|
|
|
|
214
|
|
|
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->tableCalls++; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->schemaCalled = true; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @Entity |
|
227
|
|
|
* @Table(name="unique_constraint_annotation_table", uniqueConstraints={ |
|
228
|
|
|
* @UniqueConstraint(name="uniq_hash", columns={"hash"}) |
|
229
|
|
|
* }) |
|
230
|
|
|
*/ |
|
231
|
|
|
class UniqueConstraintAnnotationModel |
|
232
|
|
|
{ |
|
233
|
|
|
/** @Id @Column */ |
|
234
|
|
|
private $id; |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @Column(name="hash", type="string", length=8, nullable=false, unique=true) |
|
238
|
|
|
*/ |
|
239
|
|
|
private $hash; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @Entity |
|
244
|
|
|
* @Table(name="first_entity") |
|
245
|
|
|
*/ |
|
246
|
|
|
class FirstEntity |
|
247
|
|
|
{ |
|
248
|
|
|
/** |
|
249
|
|
|
* @Id |
|
250
|
|
|
* @Column(name="id") |
|
251
|
|
|
*/ |
|
252
|
|
|
public $id; |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @OneToOne(targetEntity="SecondEntity") |
|
256
|
|
|
* @JoinColumn(name="id", referencedColumnName="fist_entity_id") |
|
257
|
|
|
*/ |
|
258
|
|
|
public $secondEntity; |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @Column(name="name") |
|
262
|
|
|
*/ |
|
263
|
|
|
public $name; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @Entity |
|
268
|
|
|
* @Table(name="second_entity") |
|
269
|
|
|
*/ |
|
270
|
|
|
class SecondEntity |
|
271
|
|
|
{ |
|
272
|
|
|
/** |
|
273
|
|
|
* @Id |
|
274
|
|
|
* @Column(name="fist_entity_id") |
|
275
|
|
|
*/ |
|
276
|
|
|
public $fist_entity_id; |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @Column(name="name") |
|
280
|
|
|
*/ |
|
281
|
|
|
public $name; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @Entity |
|
286
|
|
|
* @Table(name="author") |
|
287
|
|
|
*/ |
|
288
|
|
|
class Author |
|
289
|
|
|
{ |
|
290
|
|
|
/** |
|
291
|
|
|
* @Id |
|
292
|
|
|
* @Column(name="id") |
|
293
|
|
|
*/ |
|
294
|
|
|
public $id; |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @Column(name="name") |
|
298
|
|
|
*/ |
|
299
|
|
|
public $name; |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @ORM\ManyToMany(targetEntity="AuthorBook", mappedBy="books") |
|
303
|
|
|
* @ORM\JoinTable(name="author_book", |
|
304
|
|
|
* joinColumns={ |
|
305
|
|
|
* @ORM\JoinColumn(name="book_id", referencedColumnName="id") |
|
306
|
|
|
* }, |
|
307
|
|
|
* inverseJoinColumns={ |
|
308
|
|
|
* @ORM\JoinColumn(name="author_id", referencedColumnName="id") |
|
309
|
|
|
* } |
|
310
|
|
|
* ) |
|
311
|
|
|
*/ |
|
312
|
|
|
public $books; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @Entity |
|
317
|
|
|
* @Table(name="book") |
|
318
|
|
|
*/ |
|
319
|
|
|
class Book |
|
320
|
|
|
{ |
|
321
|
|
|
/** |
|
322
|
|
|
* @Id |
|
323
|
|
|
* @Column(name="id") |
|
324
|
|
|
*/ |
|
325
|
|
|
public $id; |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @Column(name="title") |
|
329
|
|
|
*/ |
|
330
|
|
|
public $title; |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @ManyToMany(targetEntity="AuthorBook", inversedBy="authors") |
|
334
|
|
|
* @JoinTable(name="author_book", |
|
335
|
|
|
* joinColumns={ |
|
336
|
|
|
* @JoinColumn(name="author_id", referencedColumnName="id") |
|
337
|
|
|
* }, |
|
338
|
|
|
* inverseJoinColumns={ |
|
339
|
|
|
* @JoinColumn(name="book_id", referencedColumnName="id") |
|
340
|
|
|
* } |
|
341
|
|
|
* ) |
|
342
|
|
|
*/ |
|
343
|
|
|
public $authors; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @Entity |
|
348
|
|
|
* @Table(name="author_book") |
|
349
|
|
|
*/ |
|
350
|
|
|
class AuthorBook |
|
351
|
|
|
{ |
|
352
|
|
|
/** |
|
353
|
|
|
* @Id |
|
354
|
|
|
* @Column(name="id") |
|
355
|
|
|
*/ |
|
356
|
|
|
public $id; |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @OneToMany(targetEntity="Author", mappedBy ="authors") |
|
360
|
|
|
* @JoinColumn(name="author_id", referencedColumnName="id") |
|
361
|
|
|
*/ |
|
362
|
|
|
public $author; |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* @OneToMany(targetEntity="Book", mappedBy = "books") |
|
366
|
|
|
* @JoinColumn(name="book_id", referencedColumnName="id") |
|
367
|
|
|
*/ |
|
368
|
|
|
public $book; |
|
369
|
|
|
} |