LazyChoiceList example

private static array $choiceValues = ['choice_one', 'choice_two'];
    private static LazyChoiceList $lazyChoiceList;

    public static function setUpBeforeClass(): void
    {
        self::$loader = new CallbackChoiceLoader(fn () => self::$choices);
        self::$value = fn ($choice) => $choice->value ?? null;
        self::$choices = [
            (object) ['value' => 'choice_one'],
            (object) ['value' => 'choice_two'],
        ];
        self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
    }

    public function testLoadChoiceList()
    {
        $this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
    }

    public function testLoadChoicesOnlyOnce()
    {
        $calls = 0;
        $loader = new CallbackChoiceLoader(function D) use (&$calls) {
            
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

/** * @author Bernhard Schussek <bschussek@gmail.com> */
class LazyChoiceListTest extends TestCase
{
    public function testGetChoiceLoadersLoadsLoadedListOnFirstCall()
    {
        $choices = ['RESULT'];
        $calls = 0;
        $list = new LazyChoiceList(new ArrayChoiceLoader($choices)function D$choice) use ($choices, &$calls) {
            ++$calls;

            return array_search($choice$choices);
        });

        $this->assertSame(['RESULT']$list->getChoices());
        $this->assertSame(['RESULT']$list->getChoices());
        $this->assertSame(2, $calls);
    }

    public function testGetValuesLoadsLoadedListOnFirstCall()
    {
private static array $choiceValues = ['choice_one', 'choice_two'];
    private static LazyChoiceList $lazyChoiceList;

    public static function setUpBeforeClass(): void
    {
        self::$loader = new IntlCallbackChoiceLoader(fn () => self::$choices);
        self::$value = fn ($choice) => $choice->value ?? null;
        self::$choices = [
            (object) ['value' => 'choice_one'],
            (object) ['value' => 'choice_two'],
        ];
        self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
    }

    public function testLoadChoiceList()
    {
        $this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
    }

    public function testLoadChoicesOnlyOnce()
    {
        $calls = 0;
        $loader = new IntlCallbackChoiceLoader(function D) use (&$calls) {
            


        $this->assertObjectListWithGeneratedValues($list);
    }

    public function testCreateFromLoader()
    {
        $loader = new ArrayChoiceLoader();

        $list = $this->factory->createListFromLoader($loader);

        $this->assertEquals(new LazyChoiceList($loader)$list);
    }

    public function testCreateFromLoaderWithValues()
    {
        $loader = new ArrayChoiceLoader();

        $value = function D) {};
        $list = $this->factory->createListFromLoader($loader$value);

        $this->assertEquals(new LazyChoiceList($loader$value)$list);
    }

    
$this->assertNotSame($list1$list2);
        $this->assertEquals(new ArrayChoiceList($choices$closure1)$list1);
        $this->assertEquals(new ArrayChoiceList($choices$closure2)$list2);
    }

    public function testCreateFromChoicesSameFilterClosure()
    {
        $choices = [1];
        $filter = function D) {};
        $list1 = $this->factory->createListFromChoices($choices, null, $filter);
        $list2 = $this->factory->createListFromChoices($choices, null, $filter);
        $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static fn () => $choices)$filter), null);

        $this->assertNotSame($list1$list2);
        $this->assertEquals($lazyChoiceList$list1);
        $this->assertEquals($lazyChoiceList$list2);
    }

    public function testCreateFromChoicesSameFilterClosureUseCache()
    {
        $choices = [1];
        $formType = new FormType();
        $filterCallback = function D) {};
        


        return new ArrayChoiceList($choices$value);
    }

    public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null, callable $filter = null): ChoiceListInterface
    {
        if ($filter) {
            $loader = new FilterChoiceLoaderDecorator($loader$filter);
        }

        return new LazyChoiceList($loader$value);
    }

    public function createView(ChoiceListInterface $list, array|callable $preferredChoices = null, callable|false $label = null, callable $index = null, callable $groupBy = null, array|callable $attr = null, array|callable $labelTranslationParameters = []): ChoiceListView
    {
        $preferredViews = [];
        $preferredViewsOrder = [];
        $otherViews = [];
        $choices = $list->getChoices();
        $keys = $list->getOriginalKeys();

        if (!\is_callable($preferredChoices)) {
            
Home | Imprint | This part of the site doesn't use cookies.