Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting default value to ArrayNode is not working #19

Open
tom10271 opened this issue Sep 4, 2019 · 1 comment
Open

Setting default value to ArrayNode is not working #19

tom10271 opened this issue Sep 4, 2019 · 1 comment
Labels
Bug Confirmed bugs or bugfixes.

Comments

@tom10271
Copy link

tom10271 commented Sep 4, 2019

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

@tom10271
Copy link
Author

tom10271 commented Sep 4, 2019

The problem should be Symfony will resolve $option to [] for ArrayNode, so in public function create(array $options = []): ProductInterface, $options['product_options'] is [] already making setDefault callback will not run.

@lchrusciel lchrusciel added the Bug Confirmed bugs or bugfixes. label Sep 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bugs or bugfixes.
Projects
None yet
Development

No branches or pull requests

2 participants