Siblings () error: InvalidArgumentException: current node list is empty

I have a bug in symfony2 unit testing, symfony: 2.7.1

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

      

InvalidArgumentException: The current node list is empty. twig file:

<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();
    }
}

      

As soon as I remove the siblings everything is fine and no error appears

+3


source to share


2 answers


You have a typo in your twig files: check that the P tag is open and closed as follows:

<p>bye</p>

      

instead



</p>bye</p>

      

Hope for this help

+1


source


Yours filter

did not return any results because you had a typo. That's why it crashed. However, for others who didn't have typos, how I resolved this issue by adding a catch attempt.



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

      

0


source







All Articles