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