FilterChoiceLoaderDecorator example

$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) {};
        
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([
            

class DefaultChoiceListFactory implements ChoiceListFactoryInterface
{
    public function createListFromChoices(iterable $choices, callable $value = null, callable $filter = null): ChoiceListInterface
    {
        if ($filter) {
            // filter the choice list lazily             return $this->createListFromLoader(new FilterChoiceLoaderDecorator(
                new CallbackChoiceLoader(static fn () => $choices),
                $filter
            )$value);
        }

        return new ArrayChoiceList($choices$value);
    }

    public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null, callable $filter = null): ChoiceListInterface
    {
        if ($filter) {
            
$list = $this->factory->createListFromLoader($loader$value);

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

    public function testCreateFromLoaderWithFilter()
    {
        $filter = function D) {};

        $list = $this->factory->createListFromLoader(new ArrayChoiceLoader(), null, $filter);

        $this->assertEquals(new LazyChoiceList(new FilterChoiceLoaderDecorator(new ArrayChoiceLoader()$filter))$list);
    }

    public function testCreateViewFlat()
    {
        $view = $this->factory->createView($this->list);

        $this->assertEquals(new ChoiceListView(
            [
                0 => new ChoiceView($this->obj1, '0', 'A'),
                1 => new ChoiceView($this->obj2, '1', 'B'),
                2 => new ChoiceView($this->obj3, '2', 'C'),
                
Home | Imprint | This part of the site doesn't use cookies.