Skip to content

Setting default value to ArrayNode is not working #19

@tom10271

Description

@tom10271

Symfony: 4.3.2
Sylius: 1.5.2
PHP: 7.3.7

Consider this fixture

<?php

declare(strict_types=1);

namespace HBX\Fixture;

use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class ProductFixture extends AbstractResourceFixture
{
    public function getName(): string
    {
        return 'product';
    }

    protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
    {
        $resourceNode
            ->arrayNode('product_options')->scalarPrototype()->end()->end()
        ;
    }
}

In ProductExampleFactory

<?php

namespace App\Fixture\Factory;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductExampleFactory extends SyliusProductExampleFactory
{
    protected function configureOptions(OptionsResolver $resolver): void
    {
        parent::configureOptions($resolver);
        
        $resolver
            // ...

            // Never run
            ->setDefault('product_options', function (Options $options): array {
                $productOptions = $this->productOptionRepository->findAll();

                return [$productOptions[rand(0, count($productOptions) - 1)]];
            })

            // Actually even you set the default value direct, the resolved value is still an empty array only
            // ->setDefault('product_options', ['Wow'])
        ;
    }
}

I am not sure it is a SyliusFixtureBundle's bug or Symfony 4.3.3's bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConfirmed bugs or bugfixes.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions