Merge branch '2.5' into 2.6

* 2.5:
  [PropertyAccessor] Added test to allow null value for a array
  [Yaml] Fixed #10597: Improved Yaml directive parsing
  [Form] Set a child type to text if added to the form without a type.
This commit is contained in:
Fabien Potencier 2015-01-09 19:07:16 +01:00
commit aed787878f
6 changed files with 44 additions and 3 deletions

View File

@ -894,6 +894,10 @@ class Form implements \IteratorAggregate, FormInterface
// Never initialize child forms automatically
$options['auto_initialize'] = false;
if (null === $type && null === $this->config->getDataClass()) {
$type = 'text';
}
if (null === $type) {
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
} else {

View File

@ -205,6 +205,22 @@ class CompoundFormTest extends AbstractFormTest
$this->assertSame(array(0 => $child), $this->form->all());
}
public function testAddWithoutType()
{
$child = $this->getBuilder('foo')->getForm();
$this->factory->expects($this->once())
->method('createNamed')
->with('foo', 'text')
->will($this->returnValue($child));
$this->form->add('foo');
$this->assertTrue($this->form->has('foo'));
$this->assertSame($this->form, $child->getParent());
$this->assertSame(array('foo' => $child), $this->form->all());
}
public function testAddUsingNameButNoType()
{
$this->form = $this->getBuilder('name', null, '\stdClass')

View File

@ -226,10 +226,14 @@ class PropertyAccessor implements PropertyAccessorInterface
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
$isArrayAccess = is_array($objectOrArray) || $objectOrArray instanceof \ArrayAccess;
// Create missing nested arrays on demand
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
if ($isIndex &&
(
($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property])) ||
(is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))
)
) {
if (!$ignoreInvalidIndices) {
if (!is_array($objectOrArray)) {
if (!$objectOrArray instanceof \Traversable) {

View File

@ -277,6 +277,12 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
}
public function testGetValueWhenArrayValueIsNull()
{
$this->propertyAccessor = new PropertyAccessor(false, true);
$this->assertNull($this->propertyAccessor->getValue(array('index' => array('nullable' => null)), '[index][nullable]'));
}
/**
* @dataProvider getValidPropertyPaths
*/

View File

@ -623,7 +623,7 @@ class Parser
// strip YAML header
$count = 0;
$value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#su', '', $value, -1, $count);
$value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
$this->offset += $count;
// remove leading comments

View File

@ -697,6 +697,17 @@ map_in_map: { foo: { bar: *var } }
EOF
));
}
public function testYamlDirective()
{
$yaml = <<<EOF
%YAML 1.2
---
foo: 1
bar: 2
EOF;
$this->assertEquals(array('foo' => 1, 'bar' => 2), $this->parser->parse($yaml));
}
}
class B