10000 [Form] Simplify DateIntervalType and add tests by HeahDude · Pull Request #29248 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Simplify DateIntervalType and add tests #29248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Simplify DateIntervalType and add tests
  • Loading branch information
HeahDude committed Nov 18, 2018
commit 0c44633c08aa0de22fca74a4df70a2e045ef1025
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class DateIntervalType extends AbstractType
'seconds',
);
private static $widgets = array(
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'integer' => 'Symfony\Component\Form\Extension\Core\Type\IntegerType',
'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'text' => TextType::class,
'integer' => IntegerType::class,
'choice' => ChoiceType::class,
);

/**
Expand Down Expand Up @@ -96,31 +96,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('single_text' === $options['widget']) {
$builder->addViewTransformer(new DateIntervalToStringTransformer($format));
} else {
$childOptions = array();
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childOptions[$part] = array(
$childOptions = array(
'error_bubbling' => true,
'label' => $options['labels'][$part],
// Append generic carry-along options
'required' => $options['required'],
'translation_domain' => $options['translation_domain'],
);
if ('choice' === $options['widget']) {
$childOptions[$part]['choice_translation_domain'] = false;
$childOptions[$part]['choices'] = $options[$part];
$childOptions[$part]['placeholder'] = $options['placeholder'][$part];
$childOptions['choice_translation_domain'] = false;
$childOptions['choices'] = $options[$part];
$childOptions['placeholder'] = $options['placeholder'][$part];
}
}
}
// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childOptions[$part][$passOpt] = $options[$passOpt];
}
}
}
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions[$part]);
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions);
if ('integer' === $options['widget']) {
$childForm->addModelTransformer(
new ReversedTransformer(
Expand All @@ -132,7 +122,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
}
if ($options['with_invert']) {
$builder->add('invert', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
$builder->add('invert', CheckboxType::class, array(
'label' => $options['labels']['invert'],
'error_bubbling' => true,
'required' => false,
Expand Down Expand Up @@ -180,6 +170,9 @@ public function configureOptions(OptionsResolver $resolver)
$compound = function (Options $options) {
return 'single_text' !== $options['widget'];
};
$emptyData = function (Options $options) {
return 'single_text' === $options['widget'] ? '' : array();
};

$placeholderDefault = function (Options $options) {
return $options['required'] ? null : '';
Expand Down Expand Up @@ -238,6 +231,7 @@ public function configureOptions(OptionsResolver $resolver)
// this option.
'data_class' => null,
'compound' => $compound,
'empty_data' => $emptyData,
'labels' => array(),
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,33 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}

/**
* @dataProvider provideEmptyData
*/
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'widget' => $widget,
'empty_data' => $emptyData,
));
$form->submit(null);

$this->assertSame($emptyData, $form->getViewData());
$this->assertEquals($expectedData, $form->getNormData());
$this->assertEquals($expectedData, $form->getData());
}

public function provideEmptyData()
{
$expectedData = \DateInterval::createFromDateString('6 years and 4 months');

return array(
'Simple field' => array('single_text', 'P6Y4M0D', $expectedData),
// Compound tests are broken for now
//'Compound text field' => array('text', array('years' => '06', 'months' => '04', 'days' => '00'), $expectedData),
//'Compound integer field' => array('integer', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
//'Compound choice field' => array('choice', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
);
}
}
0