ArrayChoiceLoader example

class ChoiceLoaderTest extends TestCase
{
    public function testSameFormTypeUseCachedLoader()
    {
        $choices = ['f' => 'foo', 'b' => 'bar', 'z' => 'baz'];
        $choiceList = new ArrayChoiceList($choices);

        $type = new FormType();
        $decorated = new CallbackChoiceLoader(static fn () => $choices);
        $loader1 = new ChoiceLoader($type$decorated);
        $loader2 = new ChoiceLoader($typenew ArrayChoiceLoader());

        $this->assertEquals($choiceList$loader1->loadChoiceList());
        $this->assertEquals($choiceList$loader2->loadChoiceList());

        $this->assertSame($choices$loader1->loadChoicesForValues($choices));
        $this->assertSame($choices$loader2->loadChoicesForValues($choices));

        $this->assertSame($choices$loader1->loadValuesForChoices($choices));
        $this->assertSame($choices$loader2->loadValuesForChoices($choices));
    }
}


        $this->assertSame(
            ['value 2' => $object2],
            $this->factory->createListFromChoices($choicesnew PropertyPath('property')new PropertyPath('filter'))->getChoices()
        );
    }

    public function testCreateFromLoaderPropertyPath()
    {
        $object = (object) ['property' => 'value'];
        $loader = new ArrayChoiceLoader([$object]);

        $this->assertSame(['value' => $object]$this->factory->createListFromLoader($loader, 'property')->getChoices());
    }

    public function testCreateFromLoaderFilterPropertyPath()
    {
        $object1 = (object) ['property' => 'value 1', 'filter' => false];
        $object2 = (object) ['property' => 'value 2', 'filter' => true];
        $choices = [
            'one' => $object1,
            'two' => $object2,
        ];
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()
    {
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

class FilterChoiceLoaderDecoratorTest extends TestCase
{
    public function testLoadChoiceList()
    {
        $filter = fn ($choice) => 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4))$filter);

        $this->assertEquals(new ArrayChoiceList([1 => 2, 3 => 4])$loader->loadChoiceList());
    }

    public function testLoadChoiceListWithGroupedChoices()
    {
        $filter = fn ($choice) => $choice < 9 && 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(['units' => range(1, 9), 'tens' => range(10, 90, 10)])$filter);

        $this->assertEquals(new ArrayChoiceList([
            
'Group 4' => [/* empty group should be filtered */],
            ]),
            null,
            fn ($choice) => null !== $choice
        );

        $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) {};
        
$list1 = $this->factory->createListFromChoices($choices, null, $closure1);
        $list2 = $this->factory->createListFromChoices($choices, null, $closure2);
        $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static fn () => $choices)function D) {}), null);

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

    public function testCreateFromLoaderSameLoader()
    {
        $loader = new ArrayChoiceLoader();
        $list1 = $this->factory->createListFromLoader($loader);
        $list2 = $this->factory->createListFromLoader($loader);

        $this->assertNotSame($list1$list2);
        $this->assertEquals(new LazyChoiceList($loader)$list1);
        $this->assertEquals(new LazyChoiceList($loader)$list2);
    }

    public function testCreateFromLoaderSameLoaderUseCache()
    {
        $type = new FormType();
        
Home | Imprint | This part of the site doesn't use cookies.