LogicException:選択したノードにフォームの祖先がありません。symfony2phpunitテストでエラーが発生しました

joelschmid

PHPUnit機能テストを使用してフォームに入力しようとすると、常に次のエラーが発生します。 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();
ピーター

この問題は、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が設定されました。
  • これが最後の説明です。次のparentNodeがチェックされるため、現在$ node(dd()またはvar_dump()を使用して出力を表示できます)が設定されています。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>

それがうまくいかない理由の簡単な説明です。私の場合、ブートストラップのpanel-body div内でフォームを開始していましたが、panel-footerdivに送信ボタンがありました。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ