From f62a865d9399b4eda156111da74b4b9af8f89cc5 Mon Sep 17 00:00:00 2001 From: Guillaume Sainthillier Date: Mon, 14 Dec 2020 17:19:21 +0100 Subject: [PATCH] [DoctrineBridge] Guess correct form types for DATE_IMMUTABLE and DATETIME_IMMUTABLE --- Form/DoctrineOrmTypeGuesser.php | 2 +- Tests/Form/DoctrineOrmTypeGuesserTest.php | 28 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Form/DoctrineOrmTypeGuesser.php b/Form/DoctrineOrmTypeGuesser.php index d222659e..231b5064 100644 --- a/Form/DoctrineOrmTypeGuesser.php +++ b/Form/DoctrineOrmTypeGuesser.php @@ -61,7 +61,7 @@ public function guessType(string $class, string $property) case Types::DATETIMETZ_MUTABLE: case 'vardatetime': return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE); - case Types::DATE_IMMUTABLE: + case Types::DATETIME_IMMUTABLE: case Types::DATETIMETZ_IMMUTABLE: return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE); case Types::DATEINTERVAL: diff --git a/Tests/Form/DoctrineOrmTypeGuesserTest.php b/Tests/Form/DoctrineOrmTypeGuesserTest.php index 50f222d8..458e835d 100644 --- a/Tests/Form/DoctrineOrmTypeGuesserTest.php +++ b/Tests/Form/DoctrineOrmTypeGuesserTest.php @@ -11,16 +11,44 @@ namespace Symfony\Bridge\Doctrine\Tests\Form; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ObjectManager; use PHPUnit\Framework\TestCase; use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser; use Symfony\Component\Form\Guess\Guess; +use Symfony\Component\Form\Guess\TypeGuess; use Symfony\Component\Form\Guess\ValueGuess; class DoctrineOrmTypeGuesserTest extends TestCase { + /** + * @dataProvider requiredType + */ + public function testTypeGuesser(string $type, $expected) + { + $classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock(); + $classMetadata->fieldMappings['field'] = true; + $classMetadata->expects($this->once())->method('getTypeOfField')->with('field')->willReturn($type); + + $this->assertEquals($expected, $this->getGuesser($classMetadata)->guessType('TestEntity', 'field')); + } + + public function requiredType() + { + yield [Types::DATE_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)]; + yield [Types::DATE_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE)]; + + yield [Types::TIME_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)]; + yield [Types::TIME_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE)]; + + yield [Types::DATETIME_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)]; + yield [Types::DATETIMETZ_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)]; + yield [Types::DATETIME_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE)]; + yield [Types::DATETIMETZ_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE)]; + } + /** * @dataProvider requiredProvider */