脚本宝典收集整理的这篇文章主要介绍了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 \Doctrine\ORM\EntITyManager */ 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('Acme\DemoBundle\Entity\User')->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); } }
framework: test: ~ session: storage_id: session.storage.mock_file profiler: collect: false
这是怎么回事?
试图创建完整登录的测试中存在问题.
当前的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(我相信这个问题已经解决)相结合时提到了它.
以上是脚本宝典为你收集整理的php – “会话开始后无法设置会话ID.”同时测试表单全部内容,希望文章能够帮你解决php – “会话开始后无法设置会话ID.”同时测试表单所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。