LogicException:所选节点没有表单祖先。 symfony2 phpunit测试错误

时间:2014-06-25 07:56:50

标签: php symfony testing phpunit

当我尝试使用PHPUnit Functional Testing填写表单时,我总是收到以下错误:LogicException: The selected node does not have a form ancestor. error with symfony2 phpunit testing

以下是代码,错误显示在最后一行代码中:

        $editusercrawler = $client->request('GET', '/profile');
    $this->assertTrue($editusercrawler->filter('html:contains("Edit Profile")')->count() > 0);


    // Go To Edit
    $link = $editusercrawler->filter('a:contains("Edit Profile")')->eq(0)->link();
    $editusercrawler = $client->click($link);
    //var_dump($client->getResponse()->getContent());

    // Check if User Edit was called
    $this->assertTrue($editusercrawler->filter('html:contains("Edit User")')->count() > 0);
    //var_dump($client->getResponse()->getContent());
    // Fill out form

    //var_dump($client->getResponse()->getContent());
    $editusercrawler = $client->click($link);
    $editusercrawler = $client->request('GET', '/profile/edit');
    var_dump($client->getResponse()->getContent());

    $editUserForm = $editusercrawler->selectButton('update')->form();

1 个答案:

答案 0 :(得分:2)

当HTML格式不正确时,会出现此问题。 setNode方法具有以下结构(我正在使用Laravel)

protected function setNode(\DOMElement $node)
{
    $this->button = $node;
    if ('button' === $node->nodeName || ('input' === $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
        if ($node->hasAttribute('form')) {
            // if the node has the HTML5-compliant 'form' attribute, use it
            $formId = $node->getAttribute('form');
            $form = $node->ownerDocument->getElementById($formId);
            if (null === $form) {
                throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
            }
            $this->node = $form;
        return;
    }
    // we loop until we find a form ancestor
    do {
        if (null === $node = $node->parentNode) {
            throw new \LogicException('The selected node does not have a form ancestor.');
        }
    } while ('form' !== $node->nodeName);
} elseif ('form' !== $node->nodeName) {
    throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $node->nodeName));
}

$this->node = $node;

}

  • 如您所见,该方法首先检查节点名称是否匹配 按钮/输入,同时通过验证进一步输入输入标签 'type'属性。
  • 接下来,它会检查表单属性。否则会抛出异常。 现在$ this->节点已设置。
  • 这是最后的解释 目前你有$ node(可以使用dd()或var_dump()来查看输出)set,因为将检查下一个parentNode。使用do while循环,它会搜索很长时间,直到nodeName不会被命名为“form”。

如开头所述,您的HTML结构不正确。

无效示例

<form>
<div id="first">
<input type="text" value="Wont check this">
<div/>
</form>
<div id="second">
<input type="submit" value="Working">
</div>

有效范例

<form>
<div id="first">
<input type="text" value="Wont check this">
<div/>
<div id="second">
<input type="submit" value="Working">
</div>
</form>

这是一个简单的解释,为什么它不起作用。在我的情况下,我在bootstrap panel-body div中开始表单,但是在panel-footer div中有提交按钮。