Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaValidator: Changing mapping of BIGINT to string|int #11399

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use function in_array;
use function interface_exists;
use function is_a;
use function method_exists;
use function sprintf;

/**
Expand All @@ -49,9 +50,9 @@
class SchemaValidator
{
/**
* It maps built-in Doctrine types to PHP types
* Map built-in Doctrine DBAL 3 types to PHP types
*/
private const BUILTIN_TYPES_MAP = [
private const BUILTIN_TYPES_MAP_DBAL3 = [
AsciiStringType::class => 'string',
BigIntType::class => 'string',
BooleanType::class => 'bool',
Expand All @@ -66,6 +67,24 @@ class SchemaValidator
TextType::class => 'string',
];

/**
* Map built-in Doctrine DBAL 4+ types to PHP types
*/
private const BUILTIN_TYPES_MAP = [
AsciiStringType::class => 'string',
BigIntType::class => 'string|int',
BooleanType::class => 'bool',
DecimalType::class => 'string',
FloatType::class => 'float',
GuidType::class => 'string',
IntegerType::class => 'int',
JsonType::class => 'array',
SimpleArrayType::class => 'array',
SmallIntType::class => 'int',
StringType::class => 'string',
TextType::class => 'string',
];

public function __construct(
private readonly EntityManagerInterface $em,
private readonly bool $validatePropertyTypes = true,
Expand Down Expand Up @@ -436,6 +455,10 @@ private function findBuiltInType(Type $type): string|null
{
$typeName = $type::class;

return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
return self::BUILTIN_TYPES_MAP_DBAL3[$typeName] ?? null;
} else { // DBAL 4+
return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
}
}
}
81 changes: 81 additions & 0 deletions tests/Tests/ORM/Tools/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\BigIntType;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorMap;
Expand All @@ -30,6 +32,8 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

use function method_exists;

class SchemaValidatorTest extends OrmTestCase
{
private EntityManagerInterface|null $em = null;
Expand Down Expand Up @@ -228,6 +232,47 @@ public function testInvalidAssociationTowardsMappedSuperclass(): void
$ce,
);
}

public function testBigintMappedToStringInt(): void
{
$class = $this->em->getClassMetadata(BigintMappedToStringInt::class);
$ce = $this->validator->validateClass($class);

$this->assertEquals([], $ce); // Same for DBAL 3 and 4+
}

public function testBigintMappedToInt(): void
{
$class = $this->em->getClassMetadata(BigintMappedToInt::class);
$ce = $this->validator->validateClass($class);

if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
$this->assertEquals(
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToInt#bigint' has the property type 'int' that differs from the metadata field type 'string' returned by the 'bigint' DBAL type."],
$ce,
);
} else { // DBAL 4+
$this->assertEquals(
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToInt#bigint' has the property type 'int' that differs from the metadata field type 'string|int' returned by the 'bigint' DBAL type."],
$ce,
);
}
}

public function testBigintMappedToString(): void
{
$class = $this->em->getClassMetadata(BigintMappedToString::class);
$ce = $this->validator->validateClass($class);

if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
$this->assertEquals([], $ce);
} else { // DBAL 4+
$this->assertEquals(
["The field 'Doctrine\Tests\ORM\Tools\BigintMappedToString#bigint' has the property type 'string' that differs from the metadata field type 'string|int' returned by the 'bigint' DBAL type."],
$ce,
);
}
}
}

#[MappedSuperclass]
Expand Down Expand Up @@ -547,3 +592,39 @@ class InvalidMappedSuperClass
#[ManyToMany(targetEntity: 'InvalidMappedSuperClass', mappedBy: 'invalid')]
private $selfWhatever;
}

#[Entity]
class BigintMappedToStringInt
{
#[Id]
#[Column]
#[GeneratedValue]
private int $id;

#[Column(type: Types::BIGINT)]
private string|int $bigint;
}

#[Entity]
class BigintMappedToInt
{
#[Id]
#[Column]
#[GeneratedValue]
private int $id;

#[Column(type: Types::BIGINT)]
private int $bigint;
}

#[Entity]
class BigintMappedToString
{
#[Id]
#[Column]
#[GeneratedValue]
private int $id;

#[Column(type: Types::BIGINT)]
private string $bigint;
}
Loading