4

我正在使用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);
    }
}

我的问题:如何提交带有令牌的表单?

4

2 回答 2

3

我不使用 Liip 功能测试包,但我通常使用表单并_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,
        ),
    ));

希望这可以帮助。

于 2015-11-20T16:37:22.583 回答
0

几个小时我遇到了一个非常相似的问题......我的方法有点不同。当我寻求帮助时,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);
于 2016-12-13T21:51:27.943 回答