弟姊()错误:InvalidArgumentException:当前节点列表为空


siblings() error: InvalidArgumentException: The current node list is empty

我在symfy2单元测试中有一个错误,symfony: 2.7.1

when I use siblings() I have a the error:

InvalidArgumentException:当前节点列表为空。树枝文件:

<h1>ddd</h1>
<p>ahmedghgh</p>
<ul>
    <li>dddd</li>
    <li>eeee</li>
    <li>ffff</li>    
</ul>
</p>bye</p>
<form action ="" method="GET" name ="nameForm">
    <input type="text" value ="name" name="name">
     <input type="submit" value ="send" name="send">
</form>

BasicControllerTest.php

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace TestingSymfony'BasicBundle'Tests'Controller;
use Symfony'Bundle'FrameworkBundle'Test'WebTestCase;
class BasicControllerTest extends WebTestCase {
    public function testHelloContent() {
        $client = static::createClient();
        $crawler = $client->request('GET', '/helloworld');
        $h1 = $crawler->filter('h1')->eq(0);
        $p1 = $crawler->filter('p')->first();

        $ul = $p1->siblings()->eq(0);        
        $l1 = $ul->children()->first();
        $l2 = $ul->children()->eq(1);
        $l3 = $ul->children()->last();
        $p2 = $crawler->filterXPath("//p")->last();
    }
}

一旦我删除兄弟,一切正常,没有错误出现

你在小枝文件中有一个错别字:检查p标签是否正确打开和关闭,如下所示:

<p>bye</p>
不是

</p>bye</p>

希望对您有所帮助

您的filter没有返回任何结果,因为您有一个错字。这就是它坠毁的原因。然而,对于没有任何打字错误的其他人,我就是这样解决这个问题的,通过添加一个try catch。

try {
   $p1 = $crawler->filter('p')->first();
} catch ('InvalidArgumentException $e) {
    // Handle the current node list is empty..
}