脚本宝典收集整理的这篇文章主要介绍了php – Symfony2功能测试无法访问的字段“_token”,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前不愿提交表格.
我正在尝试使用功能测试添加新的“日志”.
如果我尝试通过UI添加新日志,我会得到以下请求参数:
'WorkLOG' => array( 'submIT' => '','hours' => '8','minutes' => '0','note' => 'some text','_token' => '4l5oPcdCRzxDKKlJt_RG-B1342X52o0C187ZLLVWre4' );
但是当测试提交表单时,我得到相同的参数但没有令牌
'WorkLog' => array( 'submit' => '','note' => 'some text' );
我以为我可以通过在表单请求中添加’_token’字段来解决问题,但是当我运行然后再次测试时它给了我一个错误:
namespace App\AdminBundle\tests\Controller; use Liip\FunctionalTestBundle\Test\WeBTestCase; use Symfony\Bundle\FrameworkBundle\Client; use Symfony\component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\browserKit\Cookie; class LogControllerTest extends WebTestCase { PRivate $client; private $em; private $fixtures; public function SETUP() { $this->client = static::makeClient(); $this->em = $this->client->getContainer()->get('doctrine')->getManager(); $this->fixtures = $this->loaDFixtures(array( 'App\AdminBundle\DataFixtures\ORM\LoadUserData','App\AdminBundle\DataFixtures\ORM\LoadSubscriptionTyPEsData','App\AdminBundle\DataFixtures\ORM\LoadSubscriptionData','App\AdminBundle\DataFixtures\ORM\LoadWorkLogData',))->getReferenceRepository(); } public function testAddNewLog() { $accountId = $this->fixtures->getReference('userAccount')->getId(); // log in with admin account $this->login('adminAccount'); $crawler = $this->client->request('GET','/admin/worklog/account/'.$accountId.'/add'); $csrfToken = $this->client->getContainer()->get('form.csrf_provider')->generateCsrfToken('post_type'); $form = $crawler->selectButton('WorkLog_submit')->form(array( 'WorkLog' => array( 'hours' => '8','_token' => $csrfToken ),),'POST'); $crawler = $this->client->submit($form); } }
我的问题:如何使用令牌提交表单?
$crawler = $this->client->request('GET',$url); // retrieves the form token $token = $crawler->filter('[name="select_customer[_token]"]')->attr("value"); // makes the POST request $crawler = $this->client->request('POST',$url,array( 'select_customer' => array( '_token' => $token,'customerId' => $customerId,));
希望这可以帮助.
以上是脚本宝典为你收集整理的php – Symfony2功能测试无法访问的字段“_token”全部内容,希望文章能够帮你解决php – Symfony2功能测试无法访问的字段“_token”所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。