Merge branch '2.6' into 2.7

* 2.6:
  [PropertyAccessor] Added test to allow null value for a array
  [Yaml] Fixed #10597: Improved Yaml directive parsing
  [Form] Fixed check of violation constraint #12792
  [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:24 +01:00
commit 7a4c5fcd78
8 changed files with 71 additions and 8 deletions

View File

@ -66,7 +66,8 @@ class ValidationListener implements EventSubscriberInterface
foreach ($violations as $violation) {
// Allow the "invalid" constraint to be put onto
// non-synchronized forms
$allowNonSynchronized = $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
// ConstraintViolation::getConstraint() must not expect to provide a constraint as long as Symfony\Component\Validator\ExecutionContext exists (before 3.0)
$allowNonSynchronized = (null === $violation->getConstraint() || $violation->getConstraint() instanceof Form) && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
}

View File

@ -909,6 +909,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

@ -64,9 +64,9 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase
$this->params = array('foo' => 'bar');
}
private function getConstraintViolation($code = null)
private function getConstraintViolation($code = null, $constraint = null)
{
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form());
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, $constraint);
}
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
@ -93,7 +93,7 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase
// More specific mapping tests can be found in ViolationMapperTest
public function testMapViolation()
{
$violation = $this->getConstraintViolation();
$violation = $this->getConstraintViolation(null, new Form());
$form = $this->getForm('street');
$this->validator->expects($this->once())
@ -109,7 +109,28 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase
public function testMapViolationAllowsNonSyncIfInvalid()
{
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR);
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, new Form());
$form = $this->getForm('street');
$this->validator->expects($this->once())
->method('validate')
->will($this->returnValue(array($violation)));
$this->violationMapper->expects($this->once())
->method('mapViolation')
// pass true now
->with($violation, $form, true);
$this->listener->validateForm(new FormEvent($form, null));
}
public function testMapViolationAllowsNonSyncIfInvalidWithoutConstraintReference()
{
// constraint violations have no reference to the constraint if they are created by
// Symfony\Component\Validator\ExecutionContext
// which is deprecated in favor of
// Symfony\Component\Validator\Context\ExecutionContext
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, null);
$form = $this->getForm('street');
$this->validator->expects($this->once())

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