-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds metadata field type validation against Entity property type
This avoid situations where you might map a decimal to a float, when it should really be mapped to a string. This is a big pitfall because in that situation, the ORM will consider the field changed every time.
- Loading branch information
1 parent
a2d2e17
commit 0f67ba2
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/GH10661Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\GH10661; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Tools\SchemaValidator; | ||
use Doctrine\Tests\OrmTestCase; | ||
|
||
/** | ||
* @requires PHP >= 7.4 | ||
*/ | ||
final class GH10661Test extends OrmTestCase | ||
{ | ||
/** @var EntityManagerInterface */ | ||
private $em; | ||
|
||
/** @var SchemaValidator */ | ||
private $validator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->em = $this->getTestEntityManager(); | ||
$this->validator = new SchemaValidator($this->em); | ||
} | ||
|
||
public function testMetadataFieldTypeNotCoherentWithEntityPropertyType(): void | ||
{ | ||
$class = $this->em->getClassMetadata(InvalidEntity::class); | ||
$ce = $this->validator->validateClass($class); | ||
|
||
self::assertEquals( | ||
["The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidEntity#property1' has the property type 'float' that differs from the metadata field type 'string' returned by the 'decimal' DBAL type."], | ||
$ce | ||
); | ||
} | ||
|
||
public function testMetadataFieldTypeNotCoherentWithEntityPropertyTypeWithInheritance(): void | ||
{ | ||
$class = $this->em->getClassMetadata(InvalidChildEntity::class); | ||
$ce = $this->validator->validateClass($class); | ||
|
||
self::assertEquals( | ||
[ | ||
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidChildEntity#property1' has the property type 'float' that differs from the metadata field type 'string' returned by the 'decimal' DBAL type.", | ||
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidChildEntity#property2' has the property type 'int' that differs from the metadata field type 'string' returned by the 'string' DBAL type.", | ||
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH10661\InvalidChildEntity#anotherProperty' has the property type 'string' that differs from the metadata field type 'bool' returned by the 'boolean' DBAL type.", | ||
], | ||
$ce | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/InvalidChildEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\GH10661; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
|
||
/** @Entity */ | ||
class InvalidChildEntity extends InvalidEntity | ||
{ | ||
/** @Column(type="string") */ | ||
protected int $property2; | ||
|
||
/** | ||
* @Column(type="boolean") | ||
*/ | ||
private string $anotherProperty; | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10661/InvalidEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket\GH10661; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
|
||
/** @Entity */ | ||
class InvalidEntity | ||
{ | ||
/** | ||
* @var int | ||
* @Id | ||
* @Column | ||
*/ | ||
protected $key; | ||
|
||
/** | ||
* @Column(type="decimal") | ||
*/ | ||
protected float $property1; | ||
} |