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,43 @@
<?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\Extension\DataCollector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\DataCollector\DataCollectorExtension;
class DataCollectorExtensionTest extends TestCase
{
/**
* @var DataCollectorExtension
*/
private $extension;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dataCollector;
protected function setUp()
{
$this->dataCollector = $this->getMockBuilder('Symfony\Component\Form\Extension\DataCollector\FormDataCollectorInterface')->getMock();
$this->extension = new DataCollectorExtension($this->dataCollector);
}
public function testLoadTypeExtensions()
{
$typeExtensions = $this->extension->getTypeExtensions('Symfony\Component\Form\Extension\Core\Type\FormType');
$this->assertInternalType('array', $typeExtensions);
$this->assertCount(1, $typeExtensions);
$this->assertInstanceOf('Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension', array_shift($typeExtensions));
}
}

View File

@@ -0,0 +1,706 @@
<?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\Extension\DataCollector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\DataCollector\FormDataCollector;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
class FormDataCollectorTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dataExtractor;
/**
* @var FormDataCollector
*/
private $dataCollector;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dispatcher;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $factory;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dataMapper;
/**
* @var Form
*/
private $form;
/**
* @var Form
*/
private $childForm;
/**
* @var FormView
*/
private $view;
/**
* @var FormView
*/
private $childView;
protected function setUp()
{
$this->dataExtractor = $this->getMockBuilder('Symfony\Component\Form\Extension\DataCollector\FormDataExtractorInterface')->getMock();
$this->dataCollector = new FormDataCollector($this->dataExtractor);
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
$this->dataMapper = $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock();
$this->form = $this->createForm('name');
$this->childForm = $this->createForm('child');
$this->view = new FormView();
$this->childView = new FormView();
}
public function testBuildPreliminaryFormTree()
{
$this->form->add($this->childForm);
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($this->form)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($this->childForm)
->will($this->returnValue(array('config' => 'bar')));
$this->dataExtractor->expects($this->at(2))
->method('extractDefaultData')
->with($this->form)
->will($this->returnValue(array('default_data' => 'foo')));
$this->dataExtractor->expects($this->at(3))
->method('extractDefaultData')
->with($this->childForm)
->will($this->returnValue(array('default_data' => 'bar')));
$this->dataExtractor->expects($this->at(4))
->method('extractSubmittedData')
->with($this->form)
->will($this->returnValue(array('submitted_data' => 'foo')));
$this->dataExtractor->expects($this->at(5))
->method('extractSubmittedData')
->with($this->childForm)
->will($this->returnValue(array('submitted_data' => 'bar')));
$this->dataCollector->collectConfiguration($this->form);
$this->dataCollector->collectDefaultData($this->form);
$this->dataCollector->collectSubmittedData($this->form);
$this->dataCollector->buildPreliminaryFormTree($this->form);
$childFormData = array(
'config' => 'bar',
'default_data' => 'bar',
'submitted_data' => 'bar',
'children' => array(),
);
$formData = array(
'config' => 'foo',
'default_data' => 'foo',
'submitted_data' => 'foo',
'has_children_error' => false,
'children' => array(
'child' => $childFormData,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
spl_object_hash($this->childForm) => $childFormData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testBuildMultiplePreliminaryFormTrees()
{
$form1 = $this->createForm('form1');
$form2 = $this->createForm('form2');
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($form1)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($form2)
->will($this->returnValue(array('config' => 'bar')));
$this->dataCollector->collectConfiguration($form1);
$this->dataCollector->collectConfiguration($form2);
$this->dataCollector->buildPreliminaryFormTree($form1);
$form1Data = array(
'config' => 'foo',
'children' => array(),
);
$this->assertSame(array(
'forms' => array(
'form1' => $form1Data,
),
'forms_by_hash' => array(
spl_object_hash($form1) => $form1Data,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
$this->dataCollector->buildPreliminaryFormTree($form2);
$form2Data = array(
'config' => 'bar',
'children' => array(),
);
$this->assertSame(array(
'forms' => array(
'form1' => $form1Data,
'form2' => $form2Data,
),
'forms_by_hash' => array(
spl_object_hash($form1) => $form1Data,
spl_object_hash($form2) => $form2Data,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testBuildSamePreliminaryFormTreeMultipleTimes()
{
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($this->form)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractDefaultData')
->with($this->form)
->will($this->returnValue(array('default_data' => 'foo')));
$this->dataCollector->collectConfiguration($this->form);
$this->dataCollector->buildPreliminaryFormTree($this->form);
$formData = array(
'config' => 'foo',
'children' => array(),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
$this->dataCollector->collectDefaultData($this->form);
$this->dataCollector->buildPreliminaryFormTree($this->form);
$formData = array(
'config' => 'foo',
'default_data' => 'foo',
'children' => array(),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testBuildPreliminaryFormTreeWithoutCollectingAnyData()
{
$this->dataCollector->buildPreliminaryFormTree($this->form);
$formData = array(
'children' => array(),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testBuildFinalFormTree()
{
$this->form->add($this->childForm);
$this->view->children['child'] = $this->childView;
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($this->form)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($this->childForm)
->will($this->returnValue(array('config' => 'bar')));
$this->dataExtractor->expects($this->at(2))
->method('extractDefaultData')
->with($this->form)
->will($this->returnValue(array('default_data' => 'foo')));
$this->dataExtractor->expects($this->at(3))
->method('extractDefaultData')
->with($this->childForm)
->will($this->returnValue(array('default_data' => 'bar')));
$this->dataExtractor->expects($this->at(4))
->method('extractSubmittedData')
->with($this->form)
->will($this->returnValue(array('submitted_data' => 'foo')));
$this->dataExtractor->expects($this->at(5))
->method('extractSubmittedData')
->with($this->childForm)
->will($this->returnValue(array('submitted_data' => 'bar')));
$this->dataExtractor->expects($this->at(6))
->method('extractViewVariables')
->with($this->view)
->will($this->returnValue(array('view_vars' => 'foo')));
$this->dataExtractor->expects($this->at(7))
->method('extractViewVariables')
->with($this->childView)
->will($this->returnValue(array('view_vars' => 'bar')));
$this->dataCollector->collectConfiguration($this->form);
$this->dataCollector->collectDefaultData($this->form);
$this->dataCollector->collectSubmittedData($this->form);
$this->dataCollector->collectViewVariables($this->view);
$this->dataCollector->buildFinalFormTree($this->form, $this->view);
$childFormData = array(
'view_vars' => 'bar',
'config' => 'bar',
'default_data' => 'bar',
'submitted_data' => 'bar',
'children' => array(),
);
$formData = array(
'view_vars' => 'foo',
'config' => 'foo',
'default_data' => 'foo',
'submitted_data' => 'foo',
'has_children_error' => false,
'children' => array(
'child' => $childFormData,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
spl_object_hash($this->childForm) => $childFormData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testSerializeWithFormAddedMultipleTimes()
{
$form1 = $this->createForm('form1');
$form2 = $this->createForm('form2');
$child1 = $this->createForm('child1');
$form1View = new FormView();
$form2View = new FormView();
$child1View = new FormView();
$child1View->vars['is_selected'] = function ($choice, array $values) {
return in_array($choice, $values, true);
};
$form1->add($child1);
$form2->add($child1);
$form1View->children['child1'] = $child1View;
$form2View->children['child1'] = $child1View;
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($form1)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($child1)
->will($this->returnValue(array('config' => 'bar')));
$this->dataExtractor->expects($this->at(2))
->method('extractDefaultData')
->with($form1)
->will($this->returnValue(array('default_data' => 'foo')));
$this->dataExtractor->expects($this->at(3))
->method('extractDefaultData')
->with($child1)
->will($this->returnValue(array('default_data' => 'bar')));
$this->dataExtractor->expects($this->at(4))
->method('extractSubmittedData')
->with($form1)
->will($this->returnValue(array('submitted_data' => 'foo')));
$this->dataExtractor->expects($this->at(5))
->method('extractSubmittedData')
->with($child1)
->will($this->returnValue(array('submitted_data' => 'bar')));
$this->dataExtractor->expects($this->at(6))
->method('extractViewVariables')
->with($form1View)
->will($this->returnValue(array('view_vars' => 'foo')));
$this->dataExtractor->expects($this->at(7))
->method('extractViewVariables')
->with($child1View)
->will($this->returnValue(array('view_vars' => $child1View->vars)));
$this->dataExtractor->expects($this->at(8))
->method('extractConfiguration')
->with($form2)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(9))
->method('extractConfiguration')
->with($child1)
->will($this->returnValue(array('config' => 'bar')));
$this->dataExtractor->expects($this->at(10))
->method('extractDefaultData')
->with($form2)
->will($this->returnValue(array('default_data' => 'foo')));
$this->dataExtractor->expects($this->at(11))
->method('extractDefaultData')
->with($child1)
->will($this->returnValue(array('default_data' => 'bar')));
$this->dataExtractor->expects($this->at(12))
->method('extractSubmittedData')
->with($form2)
->will($this->returnValue(array('submitted_data' => 'foo')));
$this->dataExtractor->expects($this->at(13))
->method('extractSubmittedData')
->with($child1)
->will($this->returnValue(array('submitted_data' => 'bar')));
$this->dataExtractor->expects($this->at(14))
->method('extractViewVariables')
->with($form2View)
->will($this->returnValue(array('view_vars' => 'foo')));
$this->dataExtractor->expects($this->at(15))
->method('extractViewVariables')
->with($child1View)
->will($this->returnValue(array('view_vars' => $child1View->vars)));
$this->dataCollector->collectConfiguration($form1);
$this->dataCollector->collectDefaultData($form1);
$this->dataCollector->collectSubmittedData($form1);
$this->dataCollector->collectViewVariables($form1View);
$this->dataCollector->buildFinalFormTree($form1, $form1View);
$this->dataCollector->collectConfiguration($form2);
$this->dataCollector->collectDefaultData($form2);
$this->dataCollector->collectSubmittedData($form2);
$this->dataCollector->collectViewVariables($form2View);
$this->dataCollector->buildFinalFormTree($form2, $form2View);
$this->dataCollector->serialize();
}
public function testFinalFormReliesOnFormViewStructure()
{
$this->form->add($child1 = $this->createForm('first'));
$this->form->add($child2 = $this->createForm('second'));
$this->view->children['second'] = $this->childView;
$this->dataCollector->buildPreliminaryFormTree($this->form);
$child1Data = array(
'children' => array(),
);
$child2Data = array(
'children' => array(),
);
$formData = array(
'children' => array(
'first' => $child1Data,
'second' => $child2Data,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
spl_object_hash($child1) => $child1Data,
spl_object_hash($child2) => $child2Data,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
$this->dataCollector->buildFinalFormTree($this->form, $this->view);
$formData = array(
'children' => array(
// "first" not present in FormView
'second' => $child2Data,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
spl_object_hash($child1) => $child1Data,
spl_object_hash($child2) => $child2Data,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testChildViewsCanBeWithoutCorrespondingChildForms()
{
// don't add $this->childForm to $this->form!
$this->view->children['child'] = $this->childView;
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($this->form)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($this->childForm)
->will($this->returnValue(array('config' => 'bar')));
// explicitly call collectConfiguration(), since $this->childForm is not
// contained in the form tree
$this->dataCollector->collectConfiguration($this->form);
$this->dataCollector->collectConfiguration($this->childForm);
$this->dataCollector->buildFinalFormTree($this->form, $this->view);
$childFormData = array(
// no "config" key
'children' => array(),
);
$formData = array(
'config' => 'foo',
'children' => array(
'child' => $childFormData,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
// no child entry
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testChildViewsWithoutCorrespondingChildFormsMayBeExplicitlyAssociated()
{
// don't add $this->childForm to $this->form!
$this->view->children['child'] = $this->childView;
// but associate the two
$this->dataCollector->associateFormWithView($this->childForm, $this->childView);
$this->dataExtractor->expects($this->at(0))
->method('extractConfiguration')
->with($this->form)
->will($this->returnValue(array('config' => 'foo')));
$this->dataExtractor->expects($this->at(1))
->method('extractConfiguration')
->with($this->childForm)
->will($this->returnValue(array('config' => 'bar')));
// explicitly call collectConfiguration(), since $this->childForm is not
// contained in the form tree
$this->dataCollector->collectConfiguration($this->form);
$this->dataCollector->collectConfiguration($this->childForm);
$this->dataCollector->buildFinalFormTree($this->form, $this->view);
$childFormData = array(
'config' => 'bar',
'children' => array(),
);
$formData = array(
'config' => 'foo',
'children' => array(
'child' => $childFormData,
),
);
$this->assertSame(array(
'forms' => array(
'name' => $formData,
),
'forms_by_hash' => array(
spl_object_hash($this->form) => $formData,
spl_object_hash($this->childForm) => $childFormData,
),
'nb_errors' => 0,
), $this->dataCollector->getData());
}
public function testCollectSubmittedDataCountsErrors()
{
$form1 = $this->createForm('form1');
$childForm1 = $this->createForm('child1');
$form2 = $this->createForm('form2');
$form1->add($childForm1);
$this->dataExtractor
->method('extractConfiguration')
->will($this->returnValue(array()));
$this->dataExtractor
->method('extractDefaultData')
->will($this->returnValue(array()));
$this->dataExtractor->expects($this->at(4))
->method('extractSubmittedData')
->with($form1)
->will($this->returnValue(array('errors' => array('foo'))));
$this->dataExtractor->expects($this->at(5))
->method('extractSubmittedData')
->with($childForm1)
->will($this->returnValue(array('errors' => array('bar', 'bam'))));
$this->dataExtractor->expects($this->at(8))
->method('extractSubmittedData')
->with($form2)
->will($this->returnValue(array('errors' => array('baz'))));
$this->dataCollector->collectSubmittedData($form1);
$data = $this->dataCollector->getData();
$this->assertSame(3, $data['nb_errors']);
$this->dataCollector->collectSubmittedData($form2);
$data = $this->dataCollector->getData();
$this->assertSame(4, $data['nb_errors']);
}
public function testCollectSubmittedDataExpandedFormsErrors()
{
$child1Form = $this->createForm('child1');
$child11Form = $this->createForm('child11');
$child2Form = $this->createForm('child2');
$child21Form = $this->createForm('child21');
$child1Form->add($child11Form);
$child2Form->add($child21Form);
$this->form->add($child1Form);
$this->form->add($child2Form);
$this->dataExtractor
->method('extractConfiguration')
->will($this->returnValue(array()));
$this->dataExtractor
->method('extractDefaultData')
->will($this->returnValue(array()));
$this->dataExtractor->expects($this->at(10))
->method('extractSubmittedData')
->with($this->form)
->will($this->returnValue(array('errors' => array())));
$this->dataExtractor->expects($this->at(11))
->method('extractSubmittedData')
->with($child1Form)
->will($this->returnValue(array('errors' => array())));
$this->dataExtractor->expects($this->at(12))
->method('extractSubmittedData')
->with($child11Form)
->will($this->returnValue(array('errors' => array('foo'))));
$this->dataExtractor->expects($this->at(13))
->method('extractSubmittedData')
->with($child2Form)
->will($this->returnValue(array('errors' => array())));
$this->dataExtractor->expects($this->at(14))
->method('extractSubmittedData')
->with($child21Form)
->will($this->returnValue(array('errors' => array())));
$this->dataCollector->collectSubmittedData($this->form);
$this->dataCollector->buildPreliminaryFormTree($this->form);
$data = $this->dataCollector->getData();
$formData = $data['forms']['name'];
$child1Data = $formData['children']['child1'];
$child11Data = $child1Data['children']['child11'];
$child2Data = $formData['children']['child2'];
$child21Data = $child2Data['children']['child21'];
$this->assertTrue($formData['has_children_error']);
$this->assertTrue($child1Data['has_children_error']);
$this->assertFalse(isset($child11Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
$this->assertFalse($child2Data['has_children_error']);
$this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
}
private function createForm($name)
{
$builder = new FormBuilder($name, null, $this->dispatcher, $this->factory);
$builder->setCompound(true);
$builder->setDataMapper($this->dataMapper);
return $builder->getForm();
}
}

View File

@@ -0,0 +1,423 @@
<?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\Extension\DataCollector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\DataCollector\FormDataExtractor;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormDataExtractorTest extends TestCase
{
use VarDumperTestTrait;
/**
* @var FormDataExtractor
*/
private $dataExtractor;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dispatcher;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $factory;
protected function setUp()
{
$this->dataExtractor = new FormDataExtractor();
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
}
public function testExtractConfiguration()
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type->expects($this->any())
->method('getInnerType')
->will($this->returnValue(new \stdClass()));
$form = $this->createBuilder('name')
->setType($type)
->getForm();
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type_class' => 'stdClass',
'synchronized' => true,
'passed_options' => array(),
'resolved_options' => array(),
), $this->dataExtractor->extractConfiguration($form));
}
public function testExtractConfigurationSortsPassedOptions()
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type->expects($this->any())
->method('getInnerType')
->will($this->returnValue(new \stdClass()));
$options = array(
'b' => 'foo',
'a' => 'bar',
'c' => 'baz',
);
$form = $this->createBuilder('name')
->setType($type)
// passed options are stored in an attribute by
// ResolvedTypeDataCollectorProxy
->setAttribute('data_collector/passed_options', $options)
->getForm();
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type_class' => 'stdClass',
'synchronized' => true,
'passed_options' => array(
'a' => 'bar',
'b' => 'foo',
'c' => 'baz',
),
'resolved_options' => array(),
), $this->dataExtractor->extractConfiguration($form));
}
public function testExtractConfigurationSortsResolvedOptions()
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type->expects($this->any())
->method('getInnerType')
->will($this->returnValue(new \stdClass()));
$options = array(
'b' => 'foo',
'a' => 'bar',
'c' => 'baz',
);
$form = $this->createBuilder('name', $options)
->setType($type)
->getForm();
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type_class' => 'stdClass',
'synchronized' => true,
'passed_options' => array(),
'resolved_options' => array(
'a' => 'bar',
'b' => 'foo',
'c' => 'baz',
),
), $this->dataExtractor->extractConfiguration($form));
}
public function testExtractConfigurationBuildsIdRecursively()
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type->expects($this->any())
->method('getInnerType')
->will($this->returnValue(new \stdClass()));
$grandParent = $this->createBuilder('grandParent')
->setCompound(true)
->setDataMapper($this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock())
->getForm();
$parent = $this->createBuilder('parent')
->setCompound(true)
->setDataMapper($this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock())
->getForm();
$form = $this->createBuilder('name')
->setType($type)
->getForm();
$grandParent->add($parent);
$parent->add($form);
$this->assertSame(array(
'id' => 'grandParent_parent_name',
'name' => 'name',
'type_class' => 'stdClass',
'synchronized' => true,
'passed_options' => array(),
'resolved_options' => array(),
), $this->dataExtractor->extractConfiguration($form));
}
public function testExtractDefaultData()
{
$form = $this->createBuilder('name')->getForm();
$form->setData('Foobar');
$this->assertSame(array(
'default_data' => array(
'norm' => 'Foobar',
),
'submitted_data' => array(),
), $this->dataExtractor->extractDefaultData($form));
}
public function testExtractDefaultDataStoresModelDataIfDifferent()
{
$form = $this->createBuilder('name')
->addModelTransformer(new FixedDataTransformer(array(
'Foo' => 'Bar',
)))
->getForm();
$form->setData('Foo');
$this->assertSame(array(
'default_data' => array(
'norm' => 'Bar',
'model' => 'Foo',
),
'submitted_data' => array(),
), $this->dataExtractor->extractDefaultData($form));
}
public function testExtractDefaultDataStoresViewDataIfDifferent()
{
$form = $this->createBuilder('name')
->addViewTransformer(new FixedDataTransformer(array(
'Foo' => 'Bar',
)))
->getForm();
$form->setData('Foo');
$this->assertSame(array(
'default_data' => array(
'norm' => 'Foo',
'view' => 'Bar',
),
'submitted_data' => array(),
), $this->dataExtractor->extractDefaultData($form));
}
public function testExtractSubmittedData()
{
$form = $this->createBuilder('name')->getForm();
$form->submit('Foobar');
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Foobar',
),
'errors' => array(),
'synchronized' => true,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractSubmittedDataStoresModelDataIfDifferent()
{
$form = $this->createBuilder('name')
->addModelTransformer(new FixedDataTransformer(array(
'Foo' => 'Bar',
'' => '',
)))
->getForm();
$form->submit('Bar');
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Bar',
'model' => 'Foo',
),
'errors' => array(),
'synchronized' => true,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractSubmittedDataStoresViewDataIfDifferent()
{
$form = $this->createBuilder('name')
->addViewTransformer(new FixedDataTransformer(array(
'Foo' => 'Bar',
'' => '',
)))
->getForm();
$form->submit('Bar');
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Foo',
'view' => 'Bar',
),
'errors' => array(),
'synchronized' => true,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractSubmittedDataStoresErrors()
{
$form = $this->createBuilder('name')->getForm();
$form->submit('Foobar');
$form->addError(new FormError('Invalid!'));
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Foobar',
),
'errors' => array(
array('message' => 'Invalid!', 'origin' => spl_object_hash($form), 'trace' => array()),
),
'synchronized' => true,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractSubmittedDataStoresErrorOrigin()
{
$form = $this->createBuilder('name')->getForm();
$error = new FormError('Invalid!');
$error->setOrigin($form);
$form->submit('Foobar');
$form->addError($error);
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Foobar',
),
'errors' => array(
array('message' => 'Invalid!', 'origin' => spl_object_hash($form), 'trace' => array()),
),
'synchronized' => true,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractSubmittedDataStoresErrorCause()
{
$form = $this->createBuilder('name')->getForm();
$exception = new \Exception();
$violation = new ConstraintViolation('Foo', 'Foo', array(), 'Root', 'property.path', 'Invalid!', null, null, null, $exception);
$form->submit('Foobar');
$form->addError(new FormError('Invalid!', null, array(), null, $violation));
$origin = spl_object_hash($form);
$this->assertDumpMatchesFormat(<<<EODUMP
array:3 [
"submitted_data" => array:1 [
"norm" => "Foobar"
]
"errors" => array:1 [
0 => array:3 [
"message" => "Invalid!"
"origin" => "$origin"
"trace" => array:2 [
0 => Symfony\Component\Validator\ConstraintViolation {
-message: "Foo"
-messageTemplate: "Foo"
-parameters: []
-plural: null
-root: "Root"
-propertyPath: "property.path"
-invalidValue: "Invalid!"
-constraint: null
-code: null
-cause: Exception {%A}
}
1 => Exception {#1}
]
]
]
"synchronized" => true
]
EODUMP
,
$this->dataExtractor->extractSubmittedData($form)
);
}
public function testExtractSubmittedDataRemembersIfNonSynchronized()
{
$form = $this->createBuilder('name')
->addModelTransformer(new CallbackTransformer(
function () {},
function () {
throw new TransformationFailedException('Fail!');
}
))
->getForm();
$form->submit('Foobar');
$this->assertSame(array(
'submitted_data' => array(
'norm' => 'Foobar',
'model' => null,
),
'errors' => array(),
'synchronized' => false,
), $this->dataExtractor->extractSubmittedData($form));
}
public function testExtractViewVariables()
{
$view = new FormView();
$view->vars = array(
'b' => 'foo',
'a' => 'bar',
'c' => 'baz',
'id' => 'foo_bar',
'name' => 'bar',
);
$this->assertSame(array(
'id' => 'foo_bar',
'name' => 'bar',
'view_vars' => array(
'a' => 'bar',
'b' => 'foo',
'c' => 'baz',
'id' => 'foo_bar',
'name' => 'bar',
),
), $this->dataExtractor->extractViewVariables($view));
}
/**
* @param string $name
* @param array $options
*
* @return FormBuilder
*/
private function createBuilder($name, array $options = array())
{
return new FormBuilder($name, null, $this->dispatcher, $this->factory, $options);
}
}

View File

@@ -0,0 +1,49 @@
<?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\Extension\DataCollector\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension;
class DataCollectorTypeExtensionTest extends TestCase
{
/**
* @var DataCollectorTypeExtension
*/
private $extension;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $dataCollector;
protected function setUp()
{
$this->dataCollector = $this->getMockBuilder('Symfony\Component\Form\Extension\DataCollector\FormDataCollectorInterface')->getMock();
$this->extension = new DataCollectorTypeExtension($this->dataCollector);
}
public function testGetExtendedType()
{
$this->assertEquals('Symfony\Component\Form\Extension\Core\Type\FormType', $this->extension->getExtendedType());
}
public function testBuildForm()
{
$builder = $this->getMockBuilder('Symfony\Component\Form\Test\FormBuilderInterface')->getMock();
$builder->expects($this->atLeastOnce())
->method('addEventSubscriber')
->with($this->isInstanceOf('Symfony\Component\Form\Extension\DataCollector\EventListener\DataCollectorListener'));
$this->extension->buildForm($builder, array());
}
}