测试:Crawler DateTimeType:date_widet和time_widget

时间:2018-01-19 10:21:28

标签: symfony unit-testing domcrawler

我有一个由2个字段呈现的DateTimeWidget:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('datetime', DateTimeType::class, [
            'date_widget' => 'single_text',
            'date_format' => 'dd/MM/yyyy',
            'time_widget' => 'single_text',
        ]);

根据需要生成2个字段小部件:

<input type="text" id="form_datetime_date" name="form[datetime][date]">
<input type="text" id="form_datetime_time" name="form[datetime][time]">

使用用户界面完美无缺。

在我的功能测试期间,我想提交具有此输入的表单。

我试过

$form = $crawler->selectButton('my_submit')->form();
$form['form']['datetime'];

$form = $crawler->selectButton('my_submit')->form();
$form['form']['datetime']['date'] = new \DateTime('tomorrow');
$form['form']['datetime']['time'] = new \DateTime('tomorrow');

写下这个问题我找到了解决方案,如果它可以帮助某人,我就把它放在这里

1 个答案:

答案 0 :(得分:0)

必须单独填写文本

$form->disableValidation();
$values = $form->getPhpValues();

$form['form']['datetime'] = [
    'date'=> (new \DateTime('tomorrow'))->format('d/m/Y'),
    'time'=> (new \DateTime('tomorrow'))->format('H:i'),
];

$crawler = $client->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles());