【发布时间】:2014-11-17 15:32:17
【问题描述】:
我正在尝试在 Symfony 中进行一些功能测试,但目前我的会话遇到了问题。我执行了一段似乎可以工作的代码,但我的容器的会话中没有存储任何内容。
我有一个表格,您可以在其中设置数据。当您提交它时,它会检查值并将其存储在会话中。然后它重定向到另一个页面,需要这些值存储在会话中。
我测试的目的是检查会话。
<?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(), "correct response code");检查操作是否成功
标签: php symfony session testing