OptionsResolverIntrospector example

if ($info = $optionsResolver->getInfo($option)) {
            $definition = [
                'info' => $info,
            ];
        }

        $definition += [
            'required' => $optionsResolver->isRequired($option),
            'deprecated' => $optionsResolver->isDeprecated($option),
        ];

        $introspector = new OptionsResolverIntrospector($optionsResolver);

        $map = [
            'default' => 'getDefault',
            'lazy' => 'getLazyClosures',
            'allowedTypes' => 'getAllowedTypes',
            'allowedValues' => 'getAllowedValues',
            'normalizers' => 'getNormalizers',
            'deprecation' => 'getDeprecation',
        ];

        foreach ($map as $key => $method) {
            
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class OptionsResolverIntrospectorTest extends TestCase
{
    public function testGetDefault()
    {
        $resolver = new OptionsResolver();
        $resolver->setDefault($option = 'foo', 'bar');

        $debug = new OptionsResolverIntrospector($resolver);
        $this->assertSame('bar', $debug->getDefault($option));
    }

    public function testGetDefaultNull()
    {
        $resolver = new OptionsResolver();
        $resolver->setDefault($option = 'foo', null);

        $debug = new OptionsResolverIntrospector($resolver);
        $this->assertNull($debug->getDefault($option));
    }

    
public function testResolveOptionsDefinedByOptionConfigurator()
    {
        $this->resolver->define('foo')
            ->required()
            ->deprecated('vendor/package', '1.1')
            ->default('bar')
            ->allowedTypes('string', 'bool')
            ->allowedValues('bar', 'zab')
            ->normalize(static fn (Options $options$value) => $value)
            ->info('info message')
        ;
        $introspector = new OptionsResolverIntrospector($this->resolver);

        $this->assertTrue(true, $this->resolver->isDefined('foo'));
        $this->assertTrue(true, $this->resolver->isDeprecated('foo'));
        $this->assertTrue(true, $this->resolver->hasDefault('foo'));
        $this->assertSame('bar', $introspector->getDefault('foo'));
        $this->assertSame(['string', 'bool']$introspector->getAllowedTypes('foo'));
        $this->assertSame(['bar', 'zab']$introspector->getAllowedValues('foo'));
        $this->assertCount(1, $introspector->getNormalizers('foo'));
        $this->assertSame('info message', $this->resolver->getInfo('foo'));
    }

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