我试图在Symfony中进行一些功能测试,我目前正面临着会话问题。我执行了一段代码,这似乎正在工作,但没有任何内容存储在我的容器的会话中。Symfony2功能测试会话持久性
我有一个表单设置数据。提交时,它会检查值并将其存储在会话中。然后它重定向到需要存储这些值的另一个页面。
我测试的目的是检查会话。
<?php
namespace Acme\TestBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
class FrontControllerTest extends WebTestCase
{
public function testForm()
{
$client = static::createClient();
$session = $client->getContainer()->get('session');
$crawler = $client->request('GET', '/setParameters');
$form = $crawler->selectButton('Submit')->form();
$form['myForm[firstname]']->setValue('Ben');
$form['myForm[lastname]']->setValue('H');
$client->submit($form);
//I tested and my controller is fully going through the submit handler
//which check the values and save it into the session
//Things are 100% sure there. Then it redirects to another page which check those values.
$values = $client->getContainer()->get('session')->get('parameters'); //NULL
$this->assertEquals($values['firstname'], 'Ben'); //false
$this->assertEquals($values['lastname'], 'H'); //false
}
}
其实它根本不工作,好像我不能在会话中存储任何内容并检索它。
有人可以帮我吗?谢谢。
在检查会话之前,我建议你用'$ this-> assertEquals(302,$ client-> getResponse() - > getStatusCode(),“正确的响应代码”)来检查操作的成功与否;' – Matteo 2014-09-23 12:41:13