Completed
Pull Request — master (#6481)
by Luís
15:39
created

SchemaValidatorTest::testNavigationModelSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Tools;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\Tools\SchemaValidator;
7
use Doctrine\Tests\OrmTestCase;
8
9
class SchemaValidatorTest extends OrmTestCase
10
{
11
    /**
12
     * @var EntityManager
13
     */
14
    private $em = null;
15
16
    /**
17
     * @var SchemaValidator
18
     */
19
    private $validator = null;
20
21
    public function setUp()
22
    {
23
        $this->em = $this->_getTestEntityManager();
24
        $this->validator = new SchemaValidator($this->em);
25
    }
26
27
    /**
28
     * @dataProvider modelSetProvider
29
     */
30
    public function testCmsModelSet(string $path)
31
    {
32
        $this->em->getConfiguration()
33
                 ->getMetadataDriverImpl()
34
                 ->addPaths([$path]);
35
36
        $this->validator->validateMapping();
37
38
        $this->addToAssertionCount(1);
39
    }
40
41
    public function modelSetProvider(): array
42
    {
43
        return [
44
            'cms'        => [__DIR__ . '/../../Models/CMS'],
45
            'company'    => [__DIR__ . '/../../Models/Company'],
46
            'ecommerce'  => [__DIR__ . '/../../Models/ECommerce'],
47
            'forum'      => [__DIR__ . '/../../Models/Forum'],
48
            'navigation' => [__DIR__ . '/../../Models/Navigation'],
49
            'routing'    => [__DIR__ . '/../../Models/Routing'],
50
        ];
51
    }
52
53
    /**
54
     * @group DDC-1439
55
     */
56
    public function testInvalidManyToManyJoinColumnSchema()
57
    {
58
        $class1 = $this->em->getClassMetadata(InvalidEntity1::class);
59
        $class2 = $this->em->getClassMetadata(InvalidEntity2::class);
60
61
        $ce = $this->validator->validateClass($class1);
62
63
        $this->assertEquals(
64
            [
65
                "The inverse join columns of the many-to-many table 'Entity1Entity2' have to contain to ALL identifier columns of the target entity 'Doctrine\Tests\ORM\Tools\InvalidEntity2', however 'key4' are missing.",
66
                "The join columns of the many-to-many table 'Entity1Entity2' have to contain to ALL identifier columns of the source entity 'Doctrine\Tests\ORM\Tools\InvalidEntity1', however 'key2' are missing."
67
            ],
68
            $ce
69
        );
70
    }
71
72
    /**
73
     * @group DDC-1439
74
     */
75
    public function testInvalidToOneJoinColumnSchema()
76
    {
77
        $class1 = $this->em->getClassMetadata(InvalidEntity1::class);
78
        $class2 = $this->em->getClassMetadata(InvalidEntity2::class);
79
80
        $ce = $this->validator->validateClass($class2);
81
82
        $this->assertEquals(
83
            [
84
                "The referenced column name 'id' has to be a primary key column on the target entity class 'Doctrine\Tests\ORM\Tools\InvalidEntity1'.",
85
                "The join columns of the association 'assoc' have to match to ALL identifier columns of the target entity 'Doctrine\Tests\ORM\Tools\InvalidEntity1', however 'key1, key2' are missing."
86
            ],
87
            $ce
88
        );
89
    }
90
91
    /**
92
     * @group DDC-1587
93
     */
94
    public function testValidOneToOneAsIdentifierSchema()
95
    {
96
        $class1 = $this->em->getClassMetadata(DDC1587ValidEntity2::class);
97
        $class2 = $this->em->getClassMetadata(DDC1587ValidEntity1::class);
98
99
        $ce = $this->validator->validateClass($class1);
100
101
        $this->assertEquals([], $ce);
102
    }
103
104
    /**
105
     * @group DDC-1649
106
     */
107
    public function testInvalidTripleAssociationAsKeyMapping()
108
    {
109
        $classThree = $this->em->getClassMetadata(DDC1649Three::class);
110
        $ce = $this->validator->validateClass($classThree);
111
112
        $this->assertEquals(
113
            [
114
            "Cannot map association 'Doctrine\Tests\ORM\Tools\DDC1649Three#two as identifier, because the target entity 'Doctrine\Tests\ORM\Tools\DDC1649Two' also maps an association as identifier.",
115
            "The referenced column name 'id' has to be a primary key column on the target entity class 'Doctrine\Tests\ORM\Tools\DDC1649Two'."
116
            ], $ce);
117
    }
118
119
    /**
120
     * @group DDC-3274
121
     */
122
    public function testInvalidBiDirectionalRelationMappingMissingInversedByAttribute()
123
    {
124
        $class = $this->em->getClassMetadata(DDC3274One::class);
125
        $ce = $this->validator->validateClass($class);
126
127
        $this->assertEquals(
128
            [
129
                "The field Doctrine\Tests\ORM\Tools\DDC3274One#two is on the inverse side of a bi-directional " .
130
                "relationship, but the specified mappedBy association on the target-entity " .
131
                "Doctrine\Tests\ORM\Tools\DDC3274Two#one does not contain the required 'inversedBy=\"two\"' attribute."
132
            ],
133
            $ce
134
        );
135
    }
136
137
    /**
138
     * @group DDC-3322
139
     */
140
    public function testInvalidOrderByInvalidField()
141
    {
142
        $class = $this->em->getClassMetadata(DDC3322One::class);
143
        $ce = $this->validator->validateClass($class);
144
145
        $this->assertEquals(
146
            [
147
                "The association Doctrine\Tests\ORM\Tools\DDC3322One#invalidAssoc is ordered by a foreign field " .
148
                "invalidField that is not a field on the target entity Doctrine\Tests\ORM\Tools\DDC3322ValidEntity1."
149
            ],
150
            $ce
151
        );
152
    }
153
154
    /**
155
     * @group DDC-3322
156
     */
157
    public function testInvalidOrderByCollectionValuedAssociation()
158
    {
159
        $class = $this->em->getClassMetadata(DDC3322Two::class);
160
        $ce = $this->validator->validateClass($class);
161
162
        $this->assertEquals(
163
            [
164
                "The association Doctrine\Tests\ORM\Tools\DDC3322Two#invalidAssoc is ordered by a field oneToMany " .
165
                "on Doctrine\Tests\ORM\Tools\DDC3322ValidEntity1 that is a collection-valued association."
166
            ],
167
            $ce
168
        );
169
    }
170
171
    /**
172
     * @group DDC-3322
173
     */
174
    public function testInvalidOrderByAssociationInverseSide()
175
    {
176
        $class = $this->em->getClassMetadata(DDC3322Three::class);
177
        $ce = $this->validator->validateClass($class);
178
179
        $this->assertEquals(
180
            [
181
                "The association Doctrine\Tests\ORM\Tools\DDC3322Three#invalidAssoc is ordered by a field oneToOneInverse " .
182
                "on Doctrine\Tests\ORM\Tools\DDC3322ValidEntity1 that is the inverse side of an association."
183
            ],
184
            $ce
185
        );
186
    }
187
}
188
189
/**
190
 * @Entity
191
 */
192
class InvalidEntity1
193
{
194
    /**
195
     * @Id @Column
196
     */
197
    protected $key1;
198
    /**
199
     * @Id @Column
200
     */
201
    protected $key2;
202
    /**
203
     * @ManyToMany (targetEntity="InvalidEntity2")
204
     * @JoinTable (name="Entity1Entity2",
205
     *      joinColumns={@JoinColumn(name="key1", referencedColumnName="key1")},
206
     *      inverseJoinColumns={@JoinColumn(name="key3", referencedColumnName="key3")}
207
     *      )
208
     */
209
    protected $entity2;
210
}
211
212
/**
213
 * @Entity
214
 */
215
class InvalidEntity2
216
{
217
    /**
218
     * @Id @Column
219
     */
220
    protected $key3;
221
222
    /**
223
     * @Id @Column
224
     */
225
    protected $key4;
226
227
    /**
228
     * @ManyToOne(targetEntity="InvalidEntity1")
229
     */
230
    protected $assoc;
231
}
232
233
/**
234
 * @Entity(repositoryClass="Entity\Repository\Agent")
235
 * @Table(name="agent")
236
 */
237
class DDC1587ValidEntity1
238
{
239
    /**
240
     * @var int
241
     *
242
     * @Id @GeneratedValue
243
     * @Column(name="pk", type="integer")
244
     */
245
    private $pk;
246
247
    /**
248
     * @var string
249
     *
250
     * @Column(name="name", type="string", length=32)
251
     */
252
    private $name;
253
254
    /**
255
     * @var Identifier
256
     *
257
     * @OneToOne(targetEntity="DDC1587ValidEntity2", cascade={"all"}, mappedBy="agent")
258
     * @JoinColumn(name="pk", referencedColumnName="pk_agent")
259
     */
260
    private $identifier;
261
}
262
263
/**
264
 * @Entity
265
 * @Table
266
 */
267
class DDC1587ValidEntity2
268
{
269
    /**
270
     * @var DDC1587ValidEntity1
271
     *
272
     * @Id
273
     * @OneToOne(targetEntity="DDC1587ValidEntity1", inversedBy="identifier")
274
     * @JoinColumn(name="pk_agent", referencedColumnName="pk", nullable=false)
275
     */
276
    private $agent;
277
278
    /**
279
     * @var string
280
     *
281
     * @Column(name="num", type="string", length=16, nullable=true)
282
     */
283
    private $num;
284
}
285
286
/**
287
 * @Entity
288
 */
289
class DDC1649One
290
{
291
    /**
292
     * @Id @Column @GeneratedValue
293
     */
294
    public $id;
295
}
296
297
/**
298
 * @Entity
299
 */
300
class DDC1649Two
301
{
302
    /** @Id @ManyToOne(targetEntity="DDC1649One")@JoinColumn(name="id", referencedColumnName="id")  */
303
    public $one;
304
}
305
306
/**
307
 * @Entity
308
 */
309
class DDC1649Three
310
{
311
    /** @Id @ManyToOne(targetEntity="DDC1649Two") @JoinColumn(name="id",
312
     * referencedColumnName="id") */
313
    private $two;
314
}
315
316
/**
317
 * @Entity
318
 */
319
class DDC3274One
320
{
321
    /**
322
     * @Id @Column @GeneratedValue
323
     */
324
    private $id;
325
326
    /**
327
     * @OneToMany(targetEntity="DDC3274Two", mappedBy="one")
328
     */
329
    private $two;
330
}
331
332
/**
333
 * @Entity
334
 */
335
class DDC3274Two
336
{
337
    /**
338
     * @Id
339
     * @ManyToOne(targetEntity="DDC3274One")
340
     */
341
    private $one;
342
}
343
344
/**
345
 * @Entity
346
 */
347
class DDC3322ValidEntity1
348
{
349
    /**
350
     * @Id @Column @GeneratedValue
351
     */
352
    private $id;
353
354
    /**
355
     * @ManyToOne(targetEntity="DDC3322One", inversedBy="validAssoc")
356
     */
357
    private $oneValid;
358
359
    /**
360
     * @ManyToOne(targetEntity="DDC3322One", inversedBy="invalidAssoc")
361
     */
362
    private $oneInvalid;
363
364
    /**
365
     * @ManyToOne(targetEntity="DDC3322Two", inversedBy="validAssoc")
366
     */
367
    private $twoValid;
368
369
    /**
370
     * @ManyToOne(targetEntity="DDC3322Two", inversedBy="invalidAssoc")
371
     */
372
    private $twoInvalid;
373
374
    /**
375
     * @ManyToOne(targetEntity="DDC3322Three", inversedBy="validAssoc")
376
     */
377
    private $threeValid;
378
379
    /**
380
     * @ManyToOne(targetEntity="DDC3322Three", inversedBy="invalidAssoc")
381
     */
382
    private $threeInvalid;
383
384
    /**
385
     * @OneToMany(targetEntity="DDC3322ValidEntity2", mappedBy="manyToOne")
386
     */
387
    private $oneToMany;
388
389
    /**
390
     * @ManyToOne(targetEntity="DDC3322ValidEntity2", inversedBy="oneToMany")
391
     */
392
    private $manyToOne;
393
394
    /**
395
     * @OneToOne(targetEntity="DDC3322ValidEntity2", mappedBy="oneToOneOwning")
396
     */
397
    private $oneToOneInverse;
398
399
    /**
400
     * @OneToOne(targetEntity="DDC3322ValidEntity2", inversedBy="oneToOneInverse")
401
     */
402
    private $oneToOneOwning;
403
}
404
405
/**
406
 * @Entity
407
 */
408
class DDC3322ValidEntity2
409
{
410
    /**
411
     * @Id @Column @GeneratedValue
412
     */
413
    private $id;
414
415
    /**
416
     * @ManyToOne(targetEntity="DDC3322ValidEntity1", inversedBy="oneToMany")
417
     */
418
    private $manyToOne;
419
420
    /**
421
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="manyToOne")
422
     */
423
    private $oneToMany;
424
425
    /**
426
     * @OneToOne(targetEntity="DDC3322ValidEntity1", inversedBy="oneToOneInverse")
427
     */
428
    private $oneToOneOwning;
429
430
    /**
431
     * @OneToOne(targetEntity="DDC3322ValidEntity1", mappedBy="oneToOneOwning")
432
     */
433
    private $oneToOneInverse;
434
}
435
436
/**
437
 * @Entity
438
 */
439
class DDC3322One
440
{
441
    /**
442
     * @Id @Column @GeneratedValue
443
     */
444
    private $id;
445
446
    /**
447
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="oneValid")
448
     * @OrderBy({"id" = "ASC"})
449
     */
450
    private $validAssoc;
451
452
    /**
453
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="oneInvalid")
454
     * @OrderBy({"invalidField" = "ASC"})
455
     */
456
    private $invalidAssoc;
457
}
458
459
/**
460
 * @Entity
461
 */
462
class DDC3322Two
463
{
464
    /**
465
     * @Id @Column @GeneratedValue
466
     */
467
    private $id;
468
469
    /**
470
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="twoValid")
471
     * @OrderBy({"manyToOne" = "ASC"})
472
     */
473
    private $validAssoc;
474
475
    /**
476
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="twoInvalid")
477
     * @OrderBy({"oneToMany" = "ASC"})
478
     */
479
    private $invalidAssoc;
480
}
481
482
/**
483
 * @Entity
484
 */
485
class DDC3322Three
486
{
487
    /**
488
     * @Id @Column @GeneratedValue
489
     */
490
    private $id;
491
492
    /**
493
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="threeValid")
494
     * @OrderBy({"oneToOneOwning" = "ASC"})
495
     */
496
    private $validAssoc;
497
498
    /**
499
     * @OneToMany(targetEntity="DDC3322ValidEntity1", mappedBy="threeInvalid")
500
     * @OrderBy({"oneToOneInverse" = "ASC"})
501
     */
502
    private $invalidAssoc;
503
}
504