php – “会话开始后无法设置会话ID.”同时测试表单
我正在为我的应用程序编写单元测试.我编写了一个函数来登录不同的用户(测试用户级别)和一个函数来生成有效或无效的表单数据(以测试我的表单处理).
当测试提交表单时,它会抛出异常: Uncaught PHP Exception LogicException: "Cannot set session ID after the session has started." 我正在使用Symfony 2.6.4.我找不到有关此错误消息的任何有用信息.该测试不久前完成了. class ControllerTest extends WebTestCase { public $client = null; public $route = 'home/'; /** * @var DoctrineORMEntityManager */ public $em; public function setUp() { self::bootKernel(); $this->client = static::createClient(); $this->em = static::$kernel->getContainer() ->get('doctrine') ->getManager() ; } public function logIn($role = 'admin') { if ($role === 'admin') { $userId = 20; } elseif ($role === 'user') { $userId = 29; } $user = $this->em->getRepository('AcmeDemoBundleEntityUser')->find($userId); $session = $this->client->getContainer()->get('session'); $firewall = 'main'; $token = new UsernamePasswordToken($user,$user->getPassword(),$firewall); $session->set('_security_'.$firewall,serialize($token)); $session->save(); $cookie = new Cookie($session->getName(),$session->getId()); $this->client->getCookieJar()->set($cookie); } public function getFormData($valid = true) { //function to generate (in)valid formdata } public function getFormRequest($data,$url) { return $this->client->request( 'POST',$url,$data,[],[ 'CONTENT_TYPE' => 'application/json','HTTP_X-Requested-With' => 'XMLHttpRequest',] ); } //works OK public function testNewScenario() { $url = $this->baseurl . 'new'; $this->logIn('admin'); $crawler = $this->client->request('GET',$url); $this->assertEquals(200,$this->client->getResponse()->getStatusCode(),"Unexpected HTTP status code for GET " . $url); } public function testValidNewScenario() { $this->logIn('admin'); $validData = $this->getFormData(true); //this function throws the exception $this->getFormRequest($validData,$this->baseurl); $this->assertEquals(200,"Unexpected HTTP status code for POST " . $this->baseurl); } } 这是我的config_test.yml的相关部分: framework: test: ~ session: storage_id: session.storage.mock_file profiler: collect: false 这是怎么回事? 解决方法
我不知道这是否仍然是OP的一个问题,因为这是一个老帖子,但同样的问题让我在圈子中跑来跑去寻找出路的最佳时间3小时.并且看到似乎根本没有任何解决方案.这是一个可能的.
试图创建完整登录的测试中存在问题. 当前的symfony文档声明它首选在您的测试中使用basic_http身份验证,但如果像我一样,您需要测试访问级别,则需要遵循this方法. 当我们尝试设置cookieJar时,似乎会出现问题.这(对我来说)总是犯错误.
事实证明,解决方案相当简单.将cookie集代码包装在检查当前会话ID的条件中. if( !$this->session->getId() ) { $this->cookie = new Cookie( $this->session->getName(),$this->session->getId() ); $this->client->getCookieJar()->set( $this->cookie ); // <--- this is the problem line } 还值得注意的是,调用$this-> session-> invalidate()并不能解决问题. 我希望这可以帮助某人并节省他们一些时间. 这对Symfony2.1产生了影响(没有升级的机会),但是我已经看到2.6与FOSFacebookBundle(我相信这个问题已经解决)相结合时提到了它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |