[英]Symfony2 functional test Unreachable field “_token”
我正在使用Liip功能測試包在Symfony中創建功能測試 。
我目前不願提交表格。
我正在嘗試使用功能測試添加新的“日志”。
如果我嘗試通過UI添加新日志,我會得到以下請求參數:
'WorkLog' => array(
'submit' => '',
'hours' => '8',
'minutes' => '0',
'note' => 'some text',
'_token' => '4l5oPcdCRzxDKKlJt_RG-B1342X52o0C187ZLLVWre4'
);
但是當測試提交表單時,我得到相同的參數但沒有令牌
'WorkLog' => array(
'submit' => '',
'hours' => '8',
'minutes' => '0',
'note' => 'some text'
);
我以為我可以通過在表單請求中添加'_token'字段來解決問題,但是當我運行然后再次測試時它給了我一個錯誤:
InvalidArgumentException:無法訪問的字段“_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',
'minutes' => '0',
'note' => 'some text',
'_token' => $csrfToken
),
), 'POST');
$crawler = $this->client->submit($form);
}
}
我的問題:如何使用令牌提交表單?
我不使用Liip Functional Test Bundle,但我通常以下列方式使用form和_token
:
$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,
),
));
希望這可以幫助。
幾個小時我遇到了一個非常類似的問題...我的方法有點不同。 當我在尋求幫助時,Stackoverflow檢測到可能重復,我找到了你的問題。 你的問題幫助我回答了類似的問題。
你這樣做:
$form = $crawler->selectButton('WorkLog_submit')->form(array(
'WorkLog' => array(
'hours' => '8',
'minutes' => '0',
'note' => 'some text',
'_token' => $csrfToken
),
), 'POST');
你試圖一步到位。 但這是不可能的,因為Liip功能包試圖用一些魔術方法設置數組數組並且崩潰。 我明白我們必須采取更多步驟:
我在我的代碼中使用它(你可以看到我不再使用Liip包):
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class GameControllerTest extends WebTestCase
{
public function testLoadGame(){
$client = static::createClient();
$crawler = $client->request('GET', '/loadGame');
$form = $crawler->selectButton('Load')->form();
$field = $form->get("load[uuid]");
$field->setValue($uuid1[0]);
$form->set($field);
$client->submit($form);
$response = $client->getResponse();
self::assertTrue($response->isRedirect('/game'));
}
}
所以我認為你的問題的解決方案是:
$form = $crawler->selectButton('WorkLog_submit')->form();
//dump($form) //uncomment this line to have a look on the array of array
$fieldToken = $form->get("WorkLog[_token]");
$fieldToken->setValue($csrfToken);
$form->set($fieldToken);
$client->submit($form);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.