如何在PHPUnit中获取表单对象

时间:2017-03-21 14:24:13

标签: php symfony phpunit symfony-forms domcrawler

我正在使用PHPUnit进行测试。

但是它显示错误。

whitebear$ phpunit -c app src/Acme/MemberBundle/Tests/Controller/DefaultControllerTest.php 
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.

E

Time: 1.18 seconds, Memory: 90.25MB

There was 1 error:

1) Acme\MemberBundle\Tests\Controller\DefaultControllerTest::testIndex
InvalidArgumentException: The current node list is empty.

我认为' selectButton'。

有问题
$crawlerLogin = $client->request('GET', '/login');
$form = $crawlerLogin->selectButton('submit')->form();
$form['_username'] = 'myuser';
$form['_password'] = 'mypass'; 
$crawler = $client->submit($form);

这些是用于由php生成的测试的html。

<form action="/wisdom/web/app_dev.php/login_check" method="post">
            <input type="hidden" name="_csrf_token" value="fEIiYQbeYn-qt-siza3GKybh3RFSp5eOh-Nas5hyyh4" />

    <label for="username">Username</label>
    <input type="text" id="username" name="_username" value="" required="required" />

    <label for="password">Password</label>
    <input type="password" id="password" name="_password" required="required" />

    <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
    <label for="remember_me">Remember me</label>

    <input type="submit" id="_submit" name="_submit" value="Log in" />
</form>

1 个答案:

答案 0 :(得分:4)

From the doc

  返回的Crawler上提供了

selectButton()方法   另一个与按钮匹配的Crawler(input[type=submit],   带有给定文字的input[type=image]或按钮。

您应该传递按钮的文字,所以请尝试:

$form = $crawlerLogin->selectButton('Log in')->form();

希望这个帮助