Completed
Pull Request — master (#6500)
by Mathew
17:08
created

testInvalidBiDirectionalRelationMappingMissingInversedByAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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