Symfony 4 Functional Form Test with CSRF Token and Session [英] Symfony 4 Functional Form Test with CSRF Token and Session

查看:43
本文介绍了Symfony 4 Functional Form Test with CSRF Token and Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有表单包的 symfony 4.4,并且想要进行一些功能测试.

I am using a symfony 4.4 with the form bundle and want to make some functional tests.

我的表单有多个步骤,我想执行一个完整的表单,直到出现成功消息.但是使用 csrf_protection:true 我什至无法进入第 2 页.如果我在测试环境中禁用它,我可以进入第 2 页.如果我将会话转储到第 2 页,我可以看到,它是空的.这是我的测试示例:

My form has multiple steps and I want to perform a complete form until the success message. But with csrf_protection:true I can't even get to page 2. If I disable it for the test environment I can get to page 2. If I dump my session at page 2, I can see, that it is empty. Here is an example of my test:

$client = static::createClient();
$client->request('GET', '/test');
$crawler = $client->submitForm('Next', ['name' => 'Max Mustermann']); // => lands on step2
$crawler = $client->submitForm('Next', ['email' => 'asdf@ase.de']); // => lands back on step 1 with emtpy form. So session is empty

有人知道这里有什么问题吗?这个页面说,这是必须的,但事实并非如此.https://symfony.com/doc/4.4/components/http_foundation/session_testing.html#functional-testing

Does anyone have an idea what is wrong here? This page says, that is have to work, but it doesn't. https://symfony.com/doc/4.4/components/http_foundation/session_testing.html#functional-testing

推荐答案

我建议您从客户的响应中检索表单,然后提交带有数据的表单,例如:

I suggest you to retrieve the form from the client's response then submit the form with the data, as example:

// Get the crawler
$crawler = $client->request('GET', '/test');

// Get the form
$form = $crawler->filter('form')->form();

// Fill the form. NB: double check your form structure/fields name
$form['name'] = 'Max Mustermann';
$form['email'] = 'asdf@ase.de';

// Submit the form.
$crawler = $this->client->submit($form);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());

这篇关于Symfony 4 Functional Form Test with CSRF Token and Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆