minor #29179 [Form] Hardened test suite for empty data (HeahDude)
This PR was merged into the 2.8 branch.
Discussion
----------
[Form] Hardened test suite for empty data
| Q | A
| ------------- | ---
| Branch? | 2.8
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #
| License | MIT
| Doc PR | ~
Finally the continuation of #21877, giving some more love to 2.8 before it is not maintained anymore <3.
Commits
-------
b0dab6257b
[Form] Hardened test suite for empty data
This commit is contained in:
commit
fa1cd6b4fb
@ -1543,4 +1543,43 @@ class EntityTypeTest extends BaseTypeTest
|
|||||||
$this->assertEquals(array(), $form->getNormData());
|
$this->assertEquals(array(), $form->getNormData());
|
||||||
$this->assertSame(array(), $form->getViewData(), 'View data is always an array');
|
$this->assertSame(array(), $form->getViewData(), 'View data is always an array');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
|
||||||
|
{
|
||||||
|
$emptyData = '1';
|
||||||
|
$entity1 = new SingleIntIdEntity(1, 'Foo');
|
||||||
|
$this->persist(array($entity1));
|
||||||
|
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'em' => 'default',
|
||||||
|
'class' => self::SINGLE_IDENT_CLASS,
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($entity1, $form->getNormData());
|
||||||
|
$this->assertSame($entity1, $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullMultipleUsesDefaultEmptyData()
|
||||||
|
{
|
||||||
|
$emptyData = array('1');
|
||||||
|
$entity1 = new SingleIntIdEntity(1, 'Foo');
|
||||||
|
$this->persist(array($entity1));
|
||||||
|
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'em' => 'default',
|
||||||
|
'class' => self::SINGLE_IDENT_CLASS,
|
||||||
|
'multiple' => true,
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$collection = new ArrayCollection(array($entity1));
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertEquals($collection, $form->getNormData());
|
||||||
|
$this->assertEquals($collection, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,6 +145,28 @@ abstract class BaseTypeTest extends TypeTestCase
|
|||||||
$this->assertSame($view, $form->getViewData());
|
$this->assertSame($view, $form->getViewData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
|
||||||
|
{
|
||||||
|
$builder = $this->factory->createBuilder($this->getTestedType());
|
||||||
|
|
||||||
|
if ($builder->getCompound()) {
|
||||||
|
$emptyData = array();
|
||||||
|
foreach ($builder as $field) {
|
||||||
|
// empty children should map null (model data) in the compound view data
|
||||||
|
$emptyData[$field->getName()] = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// simple fields share the view and the model format, unless they use a transformer
|
||||||
|
$expectedData = $emptyData;
|
||||||
|
}
|
||||||
|
|
||||||
|
$form = $builder->setEmptyData($emptyData)->getForm()->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
protected function getTestedType()
|
protected function getTestedType()
|
||||||
{
|
{
|
||||||
return static::TESTED_TYPE;
|
return static::TESTED_TYPE;
|
||||||
|
@ -32,4 +32,16 @@ class ButtonTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
|
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Symfony\Component\Form\Exception\BadMethodCallException
|
||||||
|
* @expectedExceptionMessage Buttons do not support empty data.
|
||||||
|
*
|
||||||
|
* @param string $emptyData
|
||||||
|
* @param null $expectedData
|
||||||
|
*/
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,4 +187,17 @@ class CheckboxTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull(false, false, null);
|
parent::testSubmitNull(false, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = true)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
// view data is transformed to the string true value
|
||||||
|
$this->assertSame('1', $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -684,19 +684,20 @@ class ChoiceTypeTest extends BaseTypeTest
|
|||||||
$this->assertTrue($form->isSynchronized());
|
$this->assertTrue($form->isSynchronized());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubmitSingleChoiceWithEmptyData()
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
|
||||||
{
|
{
|
||||||
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
'multiple' => false,
|
'multiple' => false,
|
||||||
'expanded' => false,
|
'expanded' => false,
|
||||||
'choices' => array('test'),
|
// empty data must match string choice value
|
||||||
|
'choices' => array($emptyData),
|
||||||
'choices_as_values' => true,
|
'choices_as_values' => true,
|
||||||
'empty_data' => 'test',
|
'empty_data' => $emptyData,
|
||||||
));
|
));
|
||||||
|
|
||||||
$form->submit(null);
|
$form->submit(null);
|
||||||
|
|
||||||
$this->assertSame('test', $form->getData());
|
$this->assertSame($emptyData, $form->getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSubmitSingleChoiceWithEmptyDataAndInitialData()
|
public function testSubmitSingleChoiceWithEmptyDataAndInitialData()
|
||||||
|
@ -410,4 +410,10 @@ class CollectionTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull(array(), array(), array());
|
parent::testSubmitNull(array(), array(), array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = array())
|
||||||
|
{
|
||||||
|
// resize form listener always set an empty array
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,4 +66,9 @@ class CountryTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'FR', $expectedData = 'FR')
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,4 +49,9 @@ class CurrencyTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'EUR', $expectedData = 'EUR')
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -615,4 +615,20 @@ class DateTimeTypeTest extends BaseTypeTest
|
|||||||
$this->assertNull($form->getNormData());
|
$this->assertNull($form->getNormData());
|
||||||
$this->assertSame('', $form->getViewData());
|
$this->assertSame('', $form->getViewData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
// view transformer writes back empty strings in the view data
|
||||||
|
$this->assertSame(
|
||||||
|
array('date' => array('year' => '', 'month' => '', 'day' => ''), 'time' => array('hour' => '', 'minute' => '')),
|
||||||
|
$form->getViewData()
|
||||||
|
);
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1003,4 +1003,33 @@ class DateTypeTest extends BaseTypeTest
|
|||||||
$this->assertNull($form->getNormData());
|
$this->assertNull($form->getNormData());
|
||||||
$this->assertSame('', $form->getViewData());
|
$this->assertSame('', $form->getViewData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
// view transformer write back empty strings in the view data
|
||||||
|
$this->assertSame(array('year' => '', 'month' => '', 'day' => ''), $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSingleTextSubmitNullUsesDefaultEmptyData()
|
||||||
|
{
|
||||||
|
$emptyData = '2018-11-11';
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'widget' => 'single_text',
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$date = new \DateTime($emptyData);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertEquals($date, $form->getNormData());
|
||||||
|
$this->assertEquals($date, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,6 +227,11 @@ class FormTypeTest extends BaseTypeTest
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = array())
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
|
|
||||||
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
|
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
|
||||||
{
|
{
|
||||||
$form = $this->factory->createBuilder(static::TESTED_TYPE, null, array(
|
$form = $this->factory->createBuilder(static::TESTED_TYPE, null, array(
|
||||||
|
@ -48,4 +48,16 @@ class IntegerTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 10)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,4 +59,9 @@ class LanguageTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedData = 'en')
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,4 +49,9 @@ class LocaleTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedData = 'en')
|
||||||
|
{
|
||||||
|
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,4 +80,16 @@ class MoneyTypeTest extends BaseTypeTest
|
|||||||
|
|
||||||
$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
|
$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10.00', $expectedData = 10.0)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,4 +73,16 @@ class NumberTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 10.0)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -797,4 +797,17 @@ class TimeTypeTest extends BaseTypeTest
|
|||||||
|
|
||||||
parent::testSubmitNull($expected, $norm, $view);
|
parent::testSubmitNull($expected, $norm, $view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
// view transformer write back empty strings in the view data
|
||||||
|
$this->assertSame(array('hour' => '', 'minute' => ''), $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,4 +43,16 @@ class TimezoneTypeTest extends BaseTypeTest
|
|||||||
{
|
{
|
||||||
parent::testSubmitNull($expected, $norm, '');
|
parent::testSubmitNull($expected, $norm, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'Africa/Kinshasa', $expectedData = 'Africa/Kinshasa')
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
$this->assertSame($emptyData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,4 +100,17 @@ class UrlTypeTest extends TextTypeTest
|
|||||||
|
|
||||||
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
|
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
|
||||||
|
{
|
||||||
|
$form = $this->factory->create(static::TESTED_TYPE, null, array(
|
||||||
|
'empty_data' => $emptyData,
|
||||||
|
));
|
||||||
|
$form->submit(null);
|
||||||
|
|
||||||
|
// listener normalizes data on submit
|
||||||
|
$this->assertSame($expectedData, $form->getViewData());
|
||||||
|
$this->assertSame($expectedData, $form->getNormData());
|
||||||
|
$this->assertSame($expectedData, $form->getData());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user