|
12 | 12 | namespace Symfony\Component\Form\Tests\Extension\Validator; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 16 | +use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
| 17 | +use Symfony\Component\Form\Extension\Core\Type\DateType; |
| 18 | +use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
| 19 | +use Symfony\Component\Form\Extension\Core\Type\NumberType; |
| 20 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
15 | 21 | use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser; |
16 | 22 | use Symfony\Component\Form\Guess\Guess; |
| 23 | +use Symfony\Component\Form\Guess\TypeGuess; |
17 | 24 | use Symfony\Component\Form\Guess\ValueGuess; |
| 25 | +use Symfony\Component\Validator\Constraint; |
18 | 26 | use Symfony\Component\Validator\Constraints\Email; |
19 | 27 | use Symfony\Component\Validator\Constraints\File; |
20 | 28 | use Symfony\Component\Validator\Constraints\IsTrue; |
@@ -60,6 +68,35 @@ protected function setUp(): void |
60 | 68 | $this->guesser = new ValidatorTypeGuesser($this->metadataFactory); |
61 | 69 | } |
62 | 70 |
|
| 71 | + /** |
| 72 | + * @dataProvider guessTypeProvider |
| 73 | + */ |
| 74 | + public function testGuessType(Constraint $constraint, TypeGuess $guess) |
| 75 | + { |
| 76 | + $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint); |
| 77 | + |
| 78 | + $this->assertEquals($guess, $this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY)); |
| 79 | + } |
| 80 | + |
| 81 | + public function guessTypeProvider() |
| 82 | + { |
| 83 | + return [ |
| 84 | + [new Type('array'), new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 85 | + [new Type('bool'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 86 | + [new Type('boolean'), new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 87 | + [new Type('double'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 88 | + [new Type('float'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 89 | + [new Type('numeric'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 90 | + [new Type('real'), new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 91 | + [new Type('int'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 92 | + [new Type('integer'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 93 | + [new Type('long'), new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 94 | + [new Type('string'), new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)], |
| 95 | + [new Type(\DateTime::class), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 96 | + [new Type('\DateTime'), new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE)], |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
63 | 100 | public function guessRequiredProvider() |
64 | 101 | { |
65 | 102 | $allowEmptyString = property_exists(Length::class, 'allowEmptyString') ? ['allowEmptyString' => true] : []; |
|
0 commit comments