guessRequired example

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)];
    }

    public function testRequiredGuesserSimpleFieldNotNullable()
    {
        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->fieldMappings['field'] = true;
        $classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(false);

        $this->assertEquals(new ValueGuess(true, Guess::HIGH_CONFIDENCE)$this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
    }

    public function testRequiredGuesserSimpleFieldNullable()
    {
        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->fieldMappings['field'] = true;
        $classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(true);

        $this->assertEquals(new ValueGuess(false, Guess::MEDIUM_CONFIDENCE)$this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
    }

    
$this->guessers = array_merge([], ...$tmpGuessers);
    }

    public function guessType(string $class, string $property): ?TypeGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessType($class$property));
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessRequired($class$property));
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessMaxLength($class$property));
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessPattern($class$property));
    }

    
/** * @dataProvider guessRequiredProvider */
    public function testGuessRequired($constraint$guess)
    {
        // add distracting constraint         $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Email());

        // add constraint under test         $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);

        $this->assertEquals($guess$this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
    }

    public function testGuessRequiredReturnsFalseForUnmappedProperties()
    {
        $this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE)$this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
    }

    public function testGuessMaxLengthForConstraintWithMaxValue()
    {
        $constraint = new Length(['max' => '2']);

        
return $builder;
    }

    public function createBuilderForProperty(string $class, string $property, mixed $data = null, array $options = []): FormBuilderInterface
    {
        if (null === $guesser = $this->registry->getTypeGuesser()) {
            return $this->createNamedBuilder($property, TextType::class$data$options);
        }

        $typeGuess = $guesser->guessType($class$property);
        $maxLengthGuess = $guesser->guessMaxLength($class$property);
        $requiredGuess = $guesser->guessRequired($class$property);
        $patternGuess = $guesser->guessPattern($class$property);

        $type = $typeGuess ? $typeGuess->getType() : TextType::class;

        $maxLength = $maxLengthGuess?->getValue();
        $pattern = $patternGuess?->getValue();

        if (null !== $pattern) {
            $options = array_replace_recursive(['attr' => ['pattern' => $pattern]]$options);
        }

        
Home | Imprint | This part of the site doesn't use cookies.