我正在为我的symfony3应用程序编写功能测试,当我尝试通过抓取提交按钮来测试发送表单时,我遇到了一个问题。
我要测试的方法:
$personalDataForm->handleRequest($request);
if ($personalDataForm->isValid()) {
return $this->handlePersonalForm($personalDataForm, $request);
}
return $this->render('members/personal-form.html.twig', [
'personalDataForm' => $personalDataForm->createView(),
]);
功能测试:
$client = static::createClient();
$client->getCookieJar()->set($this->cookie);
$client->followRedirects();
$container = $client->getContainer();
$container->set('session', $this->session);
$crawler = $client->request('GET', '/');
$form = $crawler->selectButton('Save')->form();
$client->submit($form);
$this->session
声明:
$this->session = new Session(new MockArraySessionStorage(), new AttributeBag(), new FlashBag())
抓取工具正在发送请求,但我遇到错误Failed to start the session because headers have already been sent by "phar:///usr/local/bin/phpunit/phpunit/Util/Printer.php" at line 134.
当我查看测试结果时,我发现我的测试未通过$personalDataForm->isValid()
条件。
我认为问题在于,在提交表单后,应用程序正在使用Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage
来存储会话而不是模拟会话。我在提交表格时如何通过模拟会话?