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

时间:2015-07-22 10:04:15

标签: php symfony phpunit

我在symfony2单元测试中遇到错误,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();
    }
}

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

2 个答案:

答案 0 :(得分:1)

树枝文件中有拼写错误:检查P标记是否已打开并关闭,如下所示:

</p>bye</p>

而不是

{{1}}

希望这个帮助

答案 1 :(得分:0)

您的filter因为输入错误而没有返回任何结果。这就是崩溃的原因。但是,对于那些没有任何拼写错误的人来说,这就是我通过添加try catch来解决这个问题的方法。

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