Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,229 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList;
use PHPUnit\Framework\TestCase;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractChoiceListTest extends TestCase
{
/**
* @var \Symfony\Component\Form\ChoiceList\ChoiceListInterface
*/
protected $list;
/**
* @var array
*/
protected $choices;
/**
* @var array
*/
protected $values;
/**
* @var array
*/
protected $structuredValues;
/**
* @var array
*/
protected $keys;
/**
* @var mixed
*/
protected $choice1;
/**
* @var mixed
*/
protected $choice2;
/**
* @var mixed
*/
protected $choice3;
/**
* @var mixed
*/
protected $choice4;
/**
* @var string
*/
protected $value1;
/**
* @var string
*/
protected $value2;
/**
* @var string
*/
protected $value3;
/**
* @var string
*/
protected $value4;
/**
* @var string
*/
protected $key1;
/**
* @var string
*/
protected $key2;
/**
* @var string
*/
protected $key3;
/**
* @var string
*/
protected $key4;
protected function setUp()
{
parent::setUp();
$this->list = $this->createChoiceList();
$choices = $this->getChoices();
$this->values = $this->getValues();
$this->structuredValues = array_combine(array_keys($choices), $this->values);
$this->choices = array_combine($this->values, $choices);
$this->keys = array_combine($this->values, array_keys($choices));
// allow access to the individual entries without relying on their indices
reset($this->choices);
reset($this->values);
reset($this->keys);
for ($i = 1; $i <= 4; ++$i) {
$this->{'choice'.$i} = current($this->choices);
$this->{'value'.$i} = current($this->values);
$this->{'key'.$i} = current($this->keys);
next($this->choices);
next($this->values);
next($this->keys);
}
}
public function testGetChoices()
{
$this->assertSame($this->choices, $this->list->getChoices());
}
public function testGetValues()
{
$this->assertSame($this->values, $this->list->getValues());
}
public function testGetStructuredValues()
{
$this->assertSame($this->values, $this->list->getStructuredValues());
}
public function testGetOriginalKeys()
{
$this->assertSame($this->keys, $this->list->getOriginalKeys());
}
public function testGetChoicesForValues()
{
$values = array($this->value1, $this->value2);
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesKeys()
{
$values = array(5 => $this->value1, 8 => $this->value2);
$this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesPreservesOrder()
{
$values = array($this->value2, $this->value1);
$this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
$values = array($this->value1, $this->value2, 'foobar');
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
}
// https://github.com/symfony/symfony/issues/3446
public function testGetChoicesForValuesEmpty()
{
$this->assertSame(array(), $this->list->getChoicesForValues(array()));
}
public function testGetValuesForChoices()
{
$choices = array($this->choice1, $this->choice2);
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesKeys()
{
$choices = array(5 => $this->choice1, 8 => $this->choice2);
$this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesPreservesOrder()
{
$choices = array($this->choice2, $this->choice1);
$this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingChoices()
{
$choices = array($this->choice1, $this->choice2, 'foobar');
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesEmpty()
{
$this->assertSame(array(), $this->list->getValuesForChoices(array()));
}
public function testGetChoicesForValuesWithNull()
{
$values = $this->list->getValuesForChoices(array(null));
$this->assertNotEmpty($this->list->getChoicesForValues($values));
}
/**
* @return \Symfony\Component\Form\ChoiceList\ChoiceListInterface
*/
abstract protected function createChoiceList();
abstract protected function getChoices();
abstract protected function getValues();
}

View File

@@ -0,0 +1,166 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ArrayChoiceListTest extends AbstractChoiceListTest
{
private $object;
protected function setUp()
{
$this->object = new \stdClass();
parent::setUp();
}
protected function createChoiceList()
{
return new ArrayChoiceList($this->getChoices());
}
protected function getChoices()
{
return array(0, 1, 1.5, '1', 'a', false, true, $this->object, null);
}
protected function getValues()
{
return array('0', '1', '2', '3', '4', '5', '6', '7', '8');
}
public function testCreateChoiceListWithValueCallback()
{
$callback = function ($choice) {
return ':'.$choice;
};
$choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => 'bar', 10 => 'baz'), $callback);
$this->assertSame(array(':foo', ':bar', ':baz'), $choiceList->getValues());
$this->assertSame(array(':foo' => 'foo', ':bar' => 'bar', ':baz' => 'baz'), $choiceList->getChoices());
$this->assertSame(array(':foo' => 2, ':bar' => 7, ':baz' => 10), $choiceList->getOriginalKeys());
$this->assertSame(array(1 => 'foo', 2 => 'baz'), $choiceList->getChoicesForValues(array(1 => ':foo', 2 => ':baz')));
$this->assertSame(array(1 => ':foo', 2 => ':baz'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 'baz')));
}
public function testCreateChoiceListWithoutValueCallbackAndDuplicateFreeToStringChoices()
{
$choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => 'bar', 10 => 123));
$this->assertSame(array('foo', 'bar', '123'), $choiceList->getValues());
$this->assertSame(array('foo' => 'foo', 'bar' => 'bar', '123' => 123), $choiceList->getChoices());
$this->assertSame(array('foo' => 2, 'bar' => 7, '123' => 10), $choiceList->getOriginalKeys());
$this->assertSame(array(1 => 'foo', 2 => 123), $choiceList->getChoicesForValues(array(1 => 'foo', 2 => '123')));
$this->assertSame(array(1 => 'foo', 2 => '123'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 123)));
}
public function testCreateChoiceListWithoutValueCallbackAndToStringDuplicates()
{
$choiceList = new ArrayChoiceList(array(2 => 'foo', 7 => '123', 10 => 123));
$this->assertSame(array('0', '1', '2'), $choiceList->getValues());
$this->assertSame(array('0' => 'foo', '1' => '123', '2' => 123), $choiceList->getChoices());
$this->assertSame(array('0' => 2, '1' => 7, '2' => 10), $choiceList->getOriginalKeys());
$this->assertSame(array(1 => 'foo', 2 => 123), $choiceList->getChoicesForValues(array(1 => '0', 2 => '2')));
$this->assertSame(array(1 => '0', 2 => '2'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => 123)));
}
public function testCreateChoiceListWithoutValueCallbackAndMixedChoices()
{
$object = new \stdClass();
$choiceList = new ArrayChoiceList(array(2 => 'foo', 5 => array(7 => '123'), 10 => $object));
$this->assertSame(array('0', '1', '2'), $choiceList->getValues());
$this->assertSame(array('0' => 'foo', '1' => '123', '2' => $object), $choiceList->getChoices());
$this->assertSame(array('0' => 2, '1' => 7, '2' => 10), $choiceList->getOriginalKeys());
$this->assertSame(array(1 => 'foo', 2 => $object), $choiceList->getChoicesForValues(array(1 => '0', 2 => '2')));
$this->assertSame(array(1 => '0', 2 => '2'), $choiceList->getValuesForChoices(array(1 => 'foo', 2 => $object)));
}
public function testCreateChoiceListWithGroupedChoices()
{
$choiceList = new ArrayChoiceList(array(
'Group 1' => array('A' => 'a', 'B' => 'b'),
'Group 2' => array('C' => 'c', 'D' => 'd'),
));
$this->assertSame(array('a', 'b', 'c', 'd'), $choiceList->getValues());
$this->assertSame(array(
'Group 1' => array('A' => 'a', 'B' => 'b'),
'Group 2' => array('C' => 'c', 'D' => 'd'),
), $choiceList->getStructuredValues());
$this->assertSame(array('a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd'), $choiceList->getChoices());
$this->assertSame(array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'), $choiceList->getOriginalKeys());
$this->assertSame(array(1 => 'a', 2 => 'b'), $choiceList->getChoicesForValues(array(1 => 'a', 2 => 'b')));
$this->assertSame(array(1 => 'a', 2 => 'b'), $choiceList->getValuesForChoices(array(1 => 'a', 2 => 'b')));
}
public function testCompareChoicesByIdentityByDefault()
{
$callback = function ($choice) {
return $choice->value;
};
$obj1 = (object) array('value' => 'value1');
$obj2 = (object) array('value' => 'value2');
$choiceList = new ArrayChoiceList(array($obj1, $obj2), $callback);
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => $obj2)));
$this->assertSame(array(2 => 'value2'), $choiceList->getValuesForChoices(array(2 => (object) array('value' => 'value2'))));
}
public function testGetChoicesForValuesWithContainingNull()
{
$choiceList = new ArrayChoiceList(array('Null' => null));
$this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('0')));
}
public function testGetChoicesForValuesWithContainingFalseAndNull()
{
$choiceList = new ArrayChoiceList(array('False' => false, 'Null' => null));
$this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('1')));
$this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
}
public function testGetChoicesForValuesWithContainingEmptyStringAndNull()
{
$choiceList = new ArrayChoiceList(array('Empty String' => '', 'Null' => null));
$this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('0')));
$this->assertSame(array(0 => null), $choiceList->getChoicesForValues(array('1')));
}
public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans()
{
$choiceList = new ArrayChoiceList(array('Empty String' => '', 'True' => true, 'False' => false));
$this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
$this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1')));
$this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
}
public function testGetChoicesForValuesWithContainingEmptyStringAndFloats()
{
$choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5));
$this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
$this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3')));
$this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5')));
}
}

View File

@@ -0,0 +1,532 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList\Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class CachingFactoryDecoratorTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $decoratedFactory;
/**
* @var CachingFactoryDecorator
*/
private $factory;
protected function setUp()
{
$this->decoratedFactory = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface')->getMock();
$this->factory = new CachingFactoryDecorator($this->decoratedFactory);
}
public function testCreateFromChoicesEmpty()
{
$list = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with(array())
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromChoices(array()));
$this->assertSame($list, $this->factory->createListFromChoices(array()));
}
public function testCreateFromChoicesComparesTraversableChoicesAsArray()
{
// The top-most traversable is converted to an array
$choices1 = new \ArrayIterator(array('A' => 'a'));
$choices2 = array('A' => 'a');
$list = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices2)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromChoices($choices1));
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
}
public function testCreateFromChoicesFlattensChoices()
{
$choices1 = array('key' => array('A' => 'a'));
$choices2 = array('A' => 'a');
$list = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices1)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromChoices($choices1));
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
}
/**
* @dataProvider provideSameChoices
*/
public function testCreateFromChoicesSameChoices($choice1, $choice2)
{
$choices1 = array($choice1);
$choices2 = array($choice2);
$list = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices1)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromChoices($choices1));
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
}
/**
* @dataProvider provideDistinguishedChoices
*/
public function testCreateFromChoicesDifferentChoices($choice1, $choice2)
{
$choices1 = array($choice1);
$choices2 = array($choice2);
$list1 = new \stdClass();
$list2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createListFromChoices')
->with($choices1)
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromChoices')
->with($choices2)
->will($this->returnValue($list2));
$this->assertSame($list1, $this->factory->createListFromChoices($choices1));
$this->assertSame($list2, $this->factory->createListFromChoices($choices2));
}
public function testCreateFromChoicesSameValueClosure()
{
$choices = array(1);
$list = new \stdClass();
$closure = function () {};
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $closure)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromChoices($choices, $closure));
$this->assertSame($list, $this->factory->createListFromChoices($choices, $closure));
}
public function testCreateFromChoicesDifferentValueClosure()
{
$choices = array(1);
$list1 = new \stdClass();
$list2 = new \stdClass();
$closure1 = function () {};
$closure2 = function () {};
$this->decoratedFactory->expects($this->at(0))
->method('createListFromChoices')
->with($choices, $closure1)
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromChoices')
->with($choices, $closure2)
->will($this->returnValue($list2));
$this->assertSame($list1, $this->factory->createListFromChoices($choices, $closure1));
$this->assertSame($list2, $this->factory->createListFromChoices($choices, $closure2));
}
public function testCreateFromLoaderSameLoader()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$list = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromLoader($loader));
$this->assertSame($list, $this->factory->createListFromLoader($loader));
}
public function testCreateFromLoaderDifferentLoader()
{
$loader1 = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$loader2 = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$list1 = new \stdClass();
$list2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createListFromLoader')
->with($loader1)
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromLoader')
->with($loader2)
->will($this->returnValue($list2));
$this->assertSame($list1, $this->factory->createListFromLoader($loader1));
$this->assertSame($list2, $this->factory->createListFromLoader($loader2));
}
public function testCreateFromLoaderSameValueClosure()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$list = new \stdClass();
$closure = function () {};
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, $closure)
->will($this->returnValue($list));
$this->assertSame($list, $this->factory->createListFromLoader($loader, $closure));
$this->assertSame($list, $this->factory->createListFromLoader($loader, $closure));
}
public function testCreateFromLoaderDifferentValueClosure()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$list1 = new \stdClass();
$list2 = new \stdClass();
$closure1 = function () {};
$closure2 = function () {};
$this->decoratedFactory->expects($this->at(0))
->method('createListFromLoader')
->with($loader, $closure1)
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromLoader')
->with($loader, $closure2)
->will($this->returnValue($list2));
$this->assertSame($list1, $this->factory->createListFromLoader($loader, $closure1));
$this->assertSame($list2, $this->factory->createListFromLoader($loader, $closure2));
}
public function testCreateViewSamePreferredChoices()
{
$preferred = array('a');
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $preferred)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, $preferred));
$this->assertSame($view, $this->factory->createView($list, $preferred));
}
public function testCreateViewDifferentPreferredChoices()
{
$preferred1 = array('a');
$preferred2 = array('b');
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, $preferred1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, $preferred2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, $preferred1));
$this->assertSame($view2, $this->factory->createView($list, $preferred2));
}
public function testCreateViewSamePreferredChoicesClosure()
{
$preferred = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $preferred)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, $preferred));
$this->assertSame($view, $this->factory->createView($list, $preferred));
}
public function testCreateViewDifferentPreferredChoicesClosure()
{
$preferred1 = function () {};
$preferred2 = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, $preferred1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, $preferred2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, $preferred1));
$this->assertSame($view2, $this->factory->createView($list, $preferred2));
}
public function testCreateViewSameLabelClosure()
{
$labels = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, $labels)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, null, $labels));
$this->assertSame($view, $this->factory->createView($list, null, $labels));
}
public function testCreateViewDifferentLabelClosure()
{
$labels1 = function () {};
$labels2 = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, null, $labels1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, null, $labels2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, null, $labels1));
$this->assertSame($view2, $this->factory->createView($list, null, $labels2));
}
public function testCreateViewSameIndexClosure()
{
$index = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, $index)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, null, null, $index));
$this->assertSame($view, $this->factory->createView($list, null, null, $index));
}
public function testCreateViewDifferentIndexClosure()
{
$index1 = function () {};
$index2 = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, null, null, $index1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, null, null, $index2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, null, null, $index1));
$this->assertSame($view2, $this->factory->createView($list, null, null, $index2));
}
public function testCreateViewSameGroupByClosure()
{
$groupBy = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, $groupBy)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, null, null, null, $groupBy));
$this->assertSame($view, $this->factory->createView($list, null, null, null, $groupBy));
}
public function testCreateViewDifferentGroupByClosure()
{
$groupBy1 = function () {};
$groupBy2 = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, null, null, null, $groupBy1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, null, null, null, $groupBy2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, null, null, null, $groupBy1));
$this->assertSame($view2, $this->factory->createView($list, null, null, null, $groupBy2));
}
public function testCreateViewSameAttributes()
{
$attr = array('class' => 'foobar');
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, $attr)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, null, null, null, null, $attr));
$this->assertSame($view, $this->factory->createView($list, null, null, null, null, $attr));
}
public function testCreateViewDifferentAttributes()
{
$attr1 = array('class' => 'foobar1');
$attr2 = array('class' => 'foobar2');
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, null, null, null, null, $attr1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, null, null, null, null, $attr2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, null, null, null, null, $attr1));
$this->assertSame($view2, $this->factory->createView($list, null, null, null, null, $attr2));
}
public function testCreateViewSameAttributesClosure()
{
$attr = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view = new \stdClass();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, $attr)
->will($this->returnValue($view));
$this->assertSame($view, $this->factory->createView($list, null, null, null, null, $attr));
$this->assertSame($view, $this->factory->createView($list, null, null, null, null, $attr));
}
public function testCreateViewDifferentAttributesClosure()
{
$attr1 = function () {};
$attr2 = function () {};
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$view1 = new \stdClass();
$view2 = new \stdClass();
$this->decoratedFactory->expects($this->at(0))
->method('createView')
->with($list, null, null, null, null, $attr1)
->will($this->returnValue($view1));
$this->decoratedFactory->expects($this->at(1))
->method('createView')
->with($list, null, null, null, null, $attr2)
->will($this->returnValue($view2));
$this->assertSame($view1, $this->factory->createView($list, null, null, null, null, $attr1));
$this->assertSame($view2, $this->factory->createView($list, null, null, null, null, $attr2));
}
public function provideSameChoices()
{
$object = (object) array('foo' => 'bar');
return array(
array(0, 0),
array('a', 'a'),
// https://github.com/symfony/symfony/issues/10409
array(chr(181).'meter', chr(181).'meter'), // UTF-8
array($object, $object),
);
}
public function provideDistinguishedChoices()
{
return array(
array(0, false),
array(0, null),
array(0, '0'),
array(0, ''),
array(1, true),
array(1, '1'),
array(1, 'a'),
array('', false),
array('', null),
array(false, null),
// Same properties, but not identical
array((object) array('foo' => 'bar'), (object) array('foo' => 'bar')),
);
}
public function provideSameKeyChoices()
{
// Only test types here that can be used as array keys
return array(
array(0, 0),
array(0, '0'),
array('a', 'a'),
array(chr(181).'meter', chr(181).'meter'),
);
}
public function provideDistinguishedKeyChoices()
{
// Only test types here that can be used as array keys
return array(
array(0, ''),
array(1, 'a'),
array('', 'a'),
);
}
}

View File

@@ -0,0 +1,791 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList\Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
class DefaultChoiceListFactoryTest extends TestCase
{
private $obj1;
private $obj2;
private $obj3;
private $obj4;
private $list;
/**
* @var DefaultChoiceListFactory
*/
private $factory;
public function getValue($object)
{
return $object->value;
}
public function getScalarValue($choice)
{
switch ($choice) {
case 'a': return 'a';
case 'b': return 'b';
case 'c': return '1';
case 'd': return '2';
}
}
public function getLabel($object)
{
return $object->label;
}
public function getFormIndex($object)
{
return $object->index;
}
public function isPreferred($object)
{
return $this->obj2 === $object || $this->obj3 === $object;
}
public function getAttr($object)
{
return $object->attr;
}
public function getGroup($object)
{
return $this->obj1 === $object || $this->obj2 === $object ? 'Group 1' : 'Group 2';
}
public function getGroupAsObject($object)
{
return $this->obj1 === $object || $this->obj2 === $object
? new DefaultChoiceListFactoryTest_Castable('Group 1')
: new DefaultChoiceListFactoryTest_Castable('Group 2');
}
protected function setUp()
{
$this->obj1 = (object) array('label' => 'A', 'index' => 'w', 'value' => 'a', 'preferred' => false, 'group' => 'Group 1', 'attr' => array());
$this->obj2 = (object) array('label' => 'B', 'index' => 'x', 'value' => 'b', 'preferred' => true, 'group' => 'Group 1', 'attr' => array('attr1' => 'value1'));
$this->obj3 = (object) array('label' => 'C', 'index' => 'y', 'value' => 1, 'preferred' => true, 'group' => 'Group 2', 'attr' => array('attr2' => 'value2'));
$this->obj4 = (object) array('label' => 'D', 'index' => 'z', 'value' => 2, 'preferred' => false, 'group' => 'Group 2', 'attr' => array());
$this->list = new ArrayChoiceList(
array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4)
);
$this->factory = new DefaultChoiceListFactory();
}
public function testCreateFromChoicesEmpty()
{
$list = $this->factory->createListFromChoices(array());
$this->assertSame(array(), $list->getChoices());
$this->assertSame(array(), $list->getValues());
}
public function testCreateFromChoicesFlat()
{
$list = $this->factory->createListFromChoices(
array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4)
);
$this->assertObjectListWithGeneratedValues($list);
}
public function testCreateFromChoicesFlatTraversable()
{
$list = $this->factory->createListFromChoices(
new \ArrayIterator(array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4))
);
$this->assertObjectListWithGeneratedValues($list);
}
public function testCreateFromChoicesFlatValuesAsCallable()
{
$list = $this->factory->createListFromChoices(
array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4),
array($this, 'getValue')
);
$this->assertObjectListWithCustomValues($list);
}
public function testCreateFromChoicesFlatValuesAsClosure()
{
$list = $this->factory->createListFromChoices(
array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4),
function ($object) { return $object->value; }
);
$this->assertObjectListWithCustomValues($list);
}
public function testCreateFromChoicesGrouped()
{
$list = $this->factory->createListFromChoices(
array(
'Group 1' => array('A' => $this->obj1, 'B' => $this->obj2),
'Group 2' => array('C' => $this->obj3, 'D' => $this->obj4),
)
);
$this->assertObjectListWithGeneratedValues($list);
}
public function testCreateFromChoicesGroupedTraversable()
{
$list = $this->factory->createListFromChoices(
new \ArrayIterator(array(
'Group 1' => array('A' => $this->obj1, 'B' => $this->obj2),
'Group 2' => array('C' => $this->obj3, 'D' => $this->obj4),
))
);
$this->assertObjectListWithGeneratedValues($list);
}
public function testCreateFromChoicesGroupedValuesAsCallable()
{
$list = $this->factory->createListFromChoices(
array(
'Group 1' => array('A' => $this->obj1, 'B' => $this->obj2),
'Group 2' => array('C' => $this->obj3, 'D' => $this->obj4),
),
array($this, 'getValue')
);
$this->assertObjectListWithCustomValues($list);
}
public function testCreateFromChoicesGroupedValuesAsClosure()
{
$list = $this->factory->createListFromChoices(
array(
'Group 1' => array('A' => $this->obj1, 'B' => $this->obj2),
'Group 2' => array('C' => $this->obj3, 'D' => $this->obj4),
),
function ($object) { return $object->value; }
);
$this->assertObjectListWithCustomValues($list);
}
public function testCreateFromLoader()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$list = $this->factory->createListFromLoader($loader);
$this->assertEquals(new LazyChoiceList($loader), $list);
}
public function testCreateFromLoaderWithValues()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$value = function () {};
$list = $this->factory->createListFromLoader($loader, $value);
$this->assertEquals(new LazyChoiceList($loader, $value), $list);
}
public function testCreateViewFlat()
{
$view = $this->factory->createView($this->list);
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView($this->obj1, '0', 'A'),
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array()
), $view);
}
public function testCreateViewFlatPreferredChoices()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3)
);
$this->assertFlatView($view);
}
public function testCreateViewFlatPreferredChoicesEmptyArray()
{
$view = $this->factory->createView(
$this->list,
array()
);
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView($this->obj1, '0', 'A'),
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array()
), $view);
}
public function testCreateViewFlatPreferredChoicesAsCallable()
{
$view = $this->factory->createView(
$this->list,
array($this, 'isPreferred')
);
$this->assertFlatView($view);
}
public function testCreateViewFlatPreferredChoicesAsClosure()
{
$obj2 = $this->obj2;
$obj3 = $this->obj3;
$view = $this->factory->createView(
$this->list,
function ($object) use ($obj2, $obj3) {
return $obj2 === $object || $obj3 === $object;
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatPreferredChoicesClosureReceivesKey()
{
$view = $this->factory->createView(
$this->list,
function ($object, $key) {
return 'B' === $key || 'C' === $key;
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatPreferredChoicesClosureReceivesValue()
{
$view = $this->factory->createView(
$this->list,
function ($object, $key, $value) {
return '1' === $value || '2' === $value;
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatLabelAsCallable()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
array($this, 'getLabel')
);
$this->assertFlatView($view);
}
public function testCreateViewFlatLabelAsClosure()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
function ($object) {
return $object->label;
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatLabelClosureReceivesKey()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
function ($object, $key) {
return $key;
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatLabelClosureReceivesValue()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
function ($object, $key, $value) {
switch ($value) {
case '0': return 'A';
case '1': return 'B';
case '2': return 'C';
case '3': return 'D';
}
}
);
$this->assertFlatView($view);
}
public function testCreateViewFlatIndexAsCallable()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
array($this, 'getFormIndex')
);
$this->assertFlatViewWithCustomIndices($view);
}
public function testCreateViewFlatIndexAsClosure()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
function ($object) {
return $object->index;
}
);
$this->assertFlatViewWithCustomIndices($view);
}
public function testCreateViewFlatIndexClosureReceivesKey()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
function ($object, $key) {
switch ($key) {
case 'A': return 'w';
case 'B': return 'x';
case 'C': return 'y';
case 'D': return 'z';
}
}
);
$this->assertFlatViewWithCustomIndices($view);
}
public function testCreateViewFlatIndexClosureReceivesValue()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
function ($object, $key, $value) {
switch ($value) {
case '0': return 'w';
case '1': return 'x';
case '2': return 'y';
case '3': return 'z';
}
}
);
$this->assertFlatViewWithCustomIndices($view);
}
public function testCreateViewFlatGroupByOriginalStructure()
{
$list = new ArrayChoiceList(array(
'Group 1' => array('A' => $this->obj1, 'B' => $this->obj2),
'Group 2' => array('C' => $this->obj3, 'D' => $this->obj4),
'Group empty' => array(),
));
$view = $this->factory->createView(
$list,
array($this->obj2, $this->obj3)
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatGroupByEmpty()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
array() // ignored
);
$this->assertFlatView($view);
}
public function testCreateViewFlatGroupByAsCallable()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
array($this, 'getGroup')
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
array($this, 'getGroupAsObject')
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatGroupByAsClosure()
{
$obj1 = $this->obj1;
$obj2 = $this->obj2;
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
function ($object) use ($obj1, $obj2) {
return $obj1 === $object || $obj2 === $object ? 'Group 1' : 'Group 2';
}
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatGroupByClosureReceivesKey()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
function ($object, $key) {
return 'A' === $key || 'B' === $key ? 'Group 1' : 'Group 2';
}
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatGroupByClosureReceivesValue()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
function ($object, $key, $value) {
return '0' === $value || '1' === $value ? 'Group 1' : 'Group 2';
}
);
$this->assertGroupedView($view);
}
public function testCreateViewFlatAttrAsArray()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
array(
'B' => array('attr1' => 'value1'),
'C' => array('attr2' => 'value2'),
)
);
$this->assertFlatViewWithAttr($view);
}
public function testCreateViewFlatAttrEmpty()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
array()
);
$this->assertFlatView($view);
}
public function testCreateViewFlatAttrAsCallable()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
array($this, 'getAttr')
);
$this->assertFlatViewWithAttr($view);
}
public function testCreateViewFlatAttrAsClosure()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
function ($object) {
return $object->attr;
}
);
$this->assertFlatViewWithAttr($view);
}
public function testCreateViewFlatAttrClosureReceivesKey()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
function ($object, $key) {
switch ($key) {
case 'B': return array('attr1' => 'value1');
case 'C': return array('attr2' => 'value2');
default: return array();
}
}
);
$this->assertFlatViewWithAttr($view);
}
public function testCreateViewFlatAttrClosureReceivesValue()
{
$view = $this->factory->createView(
$this->list,
array($this->obj2, $this->obj3),
null, // label
null, // index
null, // group
function ($object, $key, $value) {
switch ($value) {
case '1': return array('attr1' => 'value1');
case '2': return array('attr2' => 'value2');
default: return array();
}
}
);
$this->assertFlatViewWithAttr($view);
}
private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
{
$this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());
$this->assertSame(array(
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
), $list->getChoices());
$this->assertSame(array(
'a' => 'A',
'b' => 'B',
'c' => 'C',
'd' => 'D',
), $list->getOriginalKeys());
}
private function assertObjectListWithGeneratedValues(ChoiceListInterface $list)
{
$this->assertSame(array('0', '1', '2', '3'), $list->getValues());
$this->assertSame(array(
0 => $this->obj1,
1 => $this->obj2,
2 => $this->obj3,
3 => $this->obj4,
), $list->getChoices());
$this->assertSame(array(
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
), $list->getOriginalKeys());
}
private function assertScalarListWithCustomValues(ChoiceListInterface $list)
{
$this->assertSame(array('a', 'b', '1', '2'), $list->getValues());
$this->assertSame(array(
'a' => 'a',
'b' => 'b',
1 => 'c',
2 => 'd',
), $list->getChoices());
$this->assertSame(array(
'a' => 'A',
'b' => 'B',
1 => 'C',
2 => 'D',
), $list->getOriginalKeys());
}
private function assertObjectListWithCustomValues(ChoiceListInterface $list)
{
$this->assertSame(array('a', 'b', '1', '2'), $list->getValues());
$this->assertSame(array(
'a' => $this->obj1,
'b' => $this->obj2,
1 => $this->obj3,
2 => $this->obj4,
), $list->getChoices());
$this->assertSame(array(
'a' => 'A',
'b' => 'B',
1 => 'C',
2 => 'D',
), $list->getOriginalKeys());
}
private function assertFlatView($view)
{
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView($this->obj1, '0', 'A'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array(
1 => new ChoiceView($this->obj2, '1', 'B'),
2 => new ChoiceView($this->obj3, '2', 'C'),
)
), $view);
}
private function assertFlatViewWithCustomIndices($view)
{
$this->assertEquals(new ChoiceListView(
array(
'w' => new ChoiceView($this->obj1, '0', 'A'),
'z' => new ChoiceView($this->obj4, '3', 'D'),
), array(
'x' => new ChoiceView($this->obj2, '1', 'B'),
'y' => new ChoiceView($this->obj3, '2', 'C'),
)
), $view);
}
private function assertFlatViewWithAttr($view)
{
$this->assertEquals(new ChoiceListView(
array(
0 => new ChoiceView($this->obj1, '0', 'A'),
3 => new ChoiceView($this->obj4, '3', 'D'),
), array(
1 => new ChoiceView(
$this->obj2,
'1',
'B',
array('attr1' => 'value1')
),
2 => new ChoiceView(
$this->obj3,
'2',
'C',
array('attr2' => 'value2')
),
)
), $view);
}
private function assertGroupedView($view)
{
$this->assertEquals(new ChoiceListView(
array(
'Group 1' => new ChoiceGroupView(
'Group 1',
array(0 => new ChoiceView($this->obj1, '0', 'A'))
),
'Group 2' => new ChoiceGroupView(
'Group 2',
array(3 => new ChoiceView($this->obj4, '3', 'D'))
),
), array(
'Group 1' => new ChoiceGroupView(
'Group 1',
array(1 => new ChoiceView($this->obj2, '1', 'B'))
),
'Group 2' => new ChoiceGroupView(
'Group 2',
array(2 => new ChoiceView($this->obj3, '2', 'C'))
),
)
), $view);
}
}
class DefaultChoiceListFactoryTest_Castable
{
private $property;
public function __construct($property)
{
$this->property = $property;
}
public function __toString()
{
return $this->property;
}
}

View File

@@ -0,0 +1,483 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList\Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\PropertyAccess\PropertyPath;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PropertyAccessDecoratorTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $decoratedFactory;
/**
* @var PropertyAccessDecorator
*/
private $factory;
protected function setUp()
{
$this->decoratedFactory = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface')->getMock();
$this->factory = new PropertyAccessDecorator($this->decoratedFactory);
}
public function testCreateFromChoicesPropertyPath()
{
$choices = array((object) array('property' => 'value'));
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($choices, $callback) {
return array_map($callback, $choices);
}));
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, 'property'));
}
public function testCreateFromChoicesPropertyPathInstance()
{
$choices = array((object) array('property' => 'value'));
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($choices, $callback) {
return array_map($callback, $choices);
}));
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, new PropertyPath('property')));
}
/**
* @group legacy
*/
public function testCreateFromChoicesPropertyPathWithCallableString()
{
$choices = array('foo' => 'bar');
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createListFromChoices($choices, 'end'));
}
public function testCreateFromLoaderPropertyPath()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($loader, $callback) {
return $callback((object) array('property' => 'value'));
}));
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'property'));
}
/**
* @group legacy
*/
public function testCreateFromLoaderPropertyPathWithCallableString()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createListFromLoader($loader, 'end'));
}
// https://github.com/symfony/symfony/issues/5494
public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable()
{
$choices = array(null);
$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($choices, $callback) {
return array_map($callback, $choices);
}));
$this->assertSame(array(null), $this->factory->createListFromChoices($choices, 'property'));
}
// https://github.com/symfony/symfony/issues/5494
public function testCreateFromChoiceLoaderAssumeNullIfValuePropertyPathUnreadable()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($loader, $callback) {
return $callback(null);
}));
$this->assertNull($this->factory->createListFromLoader($loader, 'property'));
}
public function testCreateFromLoaderPropertyPathInstance()
{
$loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($loader, $callback) {
return $callback((object) array('property' => 'value'));
}));
$this->assertSame('value', $this->factory->createListFromLoader($loader, new PropertyPath('property')));
}
public function testCreateViewPreferredChoicesAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred) {
return $preferred((object) array('property' => true));
}));
$this->assertTrue($this->factory->createView(
$list,
'property'
));
}
/**
* @group legacy
*/
public function testCreateViewPreferredChoicesAsPropertyPathWithCallableString()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createView(
$list,
'end'
));
}
public function testCreateViewPreferredChoicesAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred) {
return $preferred((object) array('property' => true));
}));
$this->assertTrue($this->factory->createView(
$list,
new PropertyPath('property')
));
}
// https://github.com/symfony/symfony/issues/5494
public function testCreateViewAssumeNullIfPreferredChoicesPropertyPathUnreadable()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred) {
return $preferred((object) array('category' => null));
}));
$this->assertFalse($this->factory->createView(
$list,
'category.preferred'
));
}
public function testCreateViewLabelsAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label) {
return $label((object) array('property' => 'label'));
}));
$this->assertSame('label', $this->factory->createView(
$list,
null, // preferred choices
'property'
));
}
/**
* @group legacy
*/
public function testCreateViewLabelsAsPropertyPathWithCallableString()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
'end'
));
}
public function testCreateViewLabelsAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label) {
return $label((object) array('property' => 'label'));
}));
$this->assertSame('label', $this->factory->createView(
$list,
null, // preferred choices
new PropertyPath('property')
));
}
public function testCreateViewIndicesAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
return $index((object) array('property' => 'index'));
}));
$this->assertSame('index', $this->factory->createView(
$list,
null, // preferred choices
null, // label
'property'
));
}
/**
* @group legacy
*/
public function testCreateViewIndicesAsPropertyPathWithCallableString()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
'end'
));
}
public function testCreateViewIndicesAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
return $index((object) array('property' => 'index'));
}));
$this->assertSame('index', $this->factory->createView(
$list,
null, // preferred choices
null, // label
new PropertyPath('property')
));
}
public function testCreateViewGroupsAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
return $groupBy((object) array('property' => 'group'));
}));
$this->assertSame('group', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
'property'
));
}
/**
* @group legacy
*/
public function testCreateViewGroupsAsPropertyPathWithCallableString()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
'end'
));
}
public function testCreateViewGroupsAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
return $groupBy((object) array('property' => 'group'));
}));
$this->assertSame('group', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
new PropertyPath('property')
));
}
// https://github.com/symfony/symfony/issues/5494
public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
return $groupBy((object) array('group' => null));
}));
$this->assertNull($this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
'group.name'
));
}
public function testCreateViewAttrAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
return $attr((object) array('property' => 'attr'));
}));
$this->assertSame('attr', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
null, // groups
'property'
));
}
/**
* @group legacy
*/
public function testCreateViewAttrAsPropertyPathWithCallableString()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, 'end')
->willReturn('RESULT');
$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // inde
null, // groups
'end'
));
}
public function testCreateViewAttrAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, $this->isInstanceOf('\Closure'))
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
return $attr((object) array('property' => 'attr'));
}));
$this->assertSame('attr', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
null, // groups
new PropertyPath('property')
));
}
}

View File

@@ -0,0 +1,208 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class LazyChoiceListTest extends TestCase
{
/**
* @var LazyChoiceList
*/
private $list;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $loadedList;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $loader;
private $value;
protected function setUp()
{
$this->loadedList = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
$this->loader = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')->getMock();
$this->value = function () {};
$this->list = new LazyChoiceList($this->loader, $this->value);
}
public function testGetChoiceLoadersLoadsLoadedListOnFirstCall()
{
$this->loader->expects($this->exactly(2))
->method('loadChoiceList')
->with($this->value)
->will($this->returnValue($this->loadedList));
// The same list is returned by the loader
$this->loadedList->expects($this->exactly(2))
->method('getChoices')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getChoices());
$this->assertSame('RESULT', $this->list->getChoices());
}
/**
* @group legacy
*/
public function testGetChoicesUsesLoadedListWhenLoaderDoesNotCacheChoiceListOnFirstCall()
{
$this->loader->expects($this->at(0))
->method('loadChoiceList')
->with($this->value)
->willReturn($this->loadedList);
$this->loader->expects($this->at(1))
->method('loadChoiceList')
->with($this->value)
->willReturn(new ArrayChoiceList(array('a', 'b')));
// The same list is returned by the lazy choice list
$this->loadedList->expects($this->exactly(2))
->method('getChoices')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getChoices());
$this->assertSame('RESULT', $this->list->getChoices());
}
public function testGetValuesLoadsLoadedListOnFirstCall()
{
$this->loader->expects($this->exactly(2))
->method('loadChoiceList')
->with($this->value)
->will($this->returnValue($this->loadedList));
// The same list is returned by the loader
$this->loadedList->expects($this->exactly(2))
->method('getValues')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getValues());
$this->assertSame('RESULT', $this->list->getValues());
}
public function testGetStructuredValuesLoadsLoadedListOnFirstCall()
{
$this->loader->expects($this->exactly(2))
->method('loadChoiceList')
->with($this->value)
->will($this->returnValue($this->loadedList));
// The same list is returned by the loader
$this->loadedList->expects($this->exactly(2))
->method('getStructuredValues')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getStructuredValues());
$this->assertSame('RESULT', $this->list->getStructuredValues());
}
public function testGetOriginalKeysLoadsLoadedListOnFirstCall()
{
$this->loader->expects($this->exactly(2))
->method('loadChoiceList')
->with($this->value)
->will($this->returnValue($this->loadedList));
// The same list is returned by the loader
$this->loadedList->expects($this->exactly(2))
->method('getOriginalKeys')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getOriginalKeys());
$this->assertSame('RESULT', $this->list->getOriginalKeys());
}
public function testGetChoicesForValuesForwardsCallIfListNotLoaded()
{
$this->loader->expects($this->exactly(2))
->method('loadChoicesForValues')
->with(array('a', 'b'))
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
$this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
}
public function testGetChoicesForValuesUsesLoadedList()
{
$this->loader->expects($this->exactly(3))
->method('loadChoiceList')
->with($this->value)
// For BC, the same choice loaded list is returned 3 times
// It should only twice in 4.0
->will($this->returnValue($this->loadedList));
$this->loader->expects($this->never())
->method('loadChoicesForValues');
$this->loadedList->expects($this->exactly(2))
->method('getChoicesForValues')
->with(array('a', 'b'))
->will($this->returnValue('RESULT'));
// load choice list
$this->list->getChoices();
$this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
$this->assertSame('RESULT', $this->list->getChoicesForValues(array('a', 'b')));
}
/**
* @group legacy
*/
public function testGetValuesForChoicesForwardsCallIfListNotLoaded()
{
$this->loader->expects($this->exactly(2))
->method('loadValuesForChoices')
->with(array('a', 'b'))
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
$this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
}
public function testGetValuesForChoicesUsesLoadedList()
{
$this->loader->expects($this->exactly(3))
->method('loadChoiceList')
->with($this->value)
// For BC, the same choice loaded list is returned 3 times
// It should only twice in 4.0
->will($this->returnValue($this->loadedList));
$this->loader->expects($this->never())
->method('loadValuesForChoices');
$this->loadedList->expects($this->exactly(2))
->method('getValuesForChoices')
->with(array('a', 'b'))
->will($this->returnValue('RESULT'));
// load choice list
$this->list->getChoices();
$this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
$this->assertSame('RESULT', $this->list->getValuesForChoices(array('a', 'b')));
}
}

View File

@@ -0,0 +1,102 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class CallbackChoiceLoaderTest extends TestCase
{
/**
* @var \Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader
*/
private static $loader;
/**
* @var callable
*/
private static $value;
/**
* @var array
*/
private static $choices;
/**
* @var string[]
*/
private static $choiceValues;
/**
* @var \Symfony\Component\Form\ChoiceList\LazyChoiceList
*/
private static $lazyChoiceList;
public static function setUpBeforeClass()
{
self::$loader = new CallbackChoiceLoader(function () {
return self::$choices;
});
self::$value = function ($choice) {
return isset($choice->value) ? $choice->value : null;
};
self::$choices = array(
(object) array('value' => 'choice_one'),
(object) array('value' => 'choice_two'),
);
self::$choiceValues = array('choice_one', 'choice_two');
self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
}
public function testLoadChoiceList()
{
$this->assertInstanceOf('\Symfony\Component\Form\ChoiceList\ChoiceListInterface', self::$loader->loadChoiceList(self::$value));
}
public function testLoadChoiceListOnlyOnce()
{
$loadedChoiceList = self::$loader->loadChoiceList(self::$value);
$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
}
public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
{
$this->assertSame(
self::$loader->loadChoicesForValues(self::$choiceValues, self::$value),
self::$lazyChoiceList->getChoicesForValues(self::$choiceValues),
'Choice list should not be reloaded.'
);
}
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
{
$this->assertSame(
self::$loader->loadValuesForChoices(self::$choices, self::$value),
self::$lazyChoiceList->getValuesForChoices(self::$choices),
'Choice list should not be reloaded.'
);
}
public static function tearDownAfterClass()
{
self::$loader = null;
self::$value = null;
self::$choices = array();
self::$choiceValues = array();
self::$lazyChoiceList = null;
}
}