1212namespace Symfony \Component \DomCrawler \Tests ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
1516use Symfony \Component \DomCrawler \Crawler ;
1617
1718abstract class AbstractCrawlerTest extends TestCase
1819{
20+ use ExpectDeprecationTrait;
21+
1922 abstract public function getDoctype (): string ;
2023
2124 protected function createCrawler ($ node = null , string $ uri = null , string $ baseHref = null )
@@ -409,7 +412,7 @@ public function testFilterXPath()
409412 $ this ->assertCount (6 , $ crawler ->filterXPath ('//li ' ), '->filterXPath() filters the node list with the XPath expression ' );
410413
411414 $ crawler = $ this ->createTestCrawler ();
412- $ this ->assertCount (3 , $ crawler ->filterXPath ('//body ' )->filterXPath ('//button ' )->parents (), '->filterXpath() preserves parents when chained ' );
415+ $ this ->assertCount (3 , $ crawler ->filterXPath ('//body ' )->filterXPath ('//button ' )->ancestors (), '->filterXpath() preserves ancestors when chained ' );
413416 }
414417
415418 public function testFilterRemovesDuplicates ()
@@ -1082,8 +1085,13 @@ public function testFilteredChildren()
10821085 $ this ->assertEquals (1 , $ foo ->children ('.ipsum ' )->count ());
10831086 }
10841087
1088+ /**
1089+ * @group legacy
1090+ */
10851091 public function testParents ()
10861092 {
1093+ $ this ->expectDeprecation ('Since symfony/dom-crawler 5.3: The Symfony\Component\DomCrawler\Crawler::parents() method is deprecated, use ancestors() instead. ' );
1094+
10871095 $ crawler = $ this ->createTestCrawler ()->filterXPath ('//li[1] ' );
10881096 $ this ->assertNotSame ($ crawler , $ crawler ->parents (), '->parents() returns a new instance of a crawler ' );
10891097 $ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->parents() returns a new instance of a crawler ' );
@@ -1102,6 +1110,27 @@ public function testParents()
11021110 }
11031111 }
11041112
1113+ public function testAncestors ()
1114+ {
1115+ $ crawler = $ this ->createTestCrawler ()->filterXPath ('//li[1] ' );
1116+
1117+ $ nodes = $ crawler ->ancestors ();
1118+
1119+ $ this ->assertNotSame ($ crawler , $ nodes , '->ancestors() returns a new instance of a crawler ' );
1120+ $ this ->assertInstanceOf (Crawler::class, $ nodes , '->ancestors() returns a new instance of a crawler ' );
1121+
1122+ $ this ->assertEquals (3 , $ crawler ->ancestors ()->count ());
1123+
1124+ $ this ->assertEquals (0 , $ this ->createTestCrawler ()->filterXPath ('//html ' )->ancestors ()->count ());
1125+ }
1126+
1127+ public function testAncestorsThrowsIfNodeListIsEmpty ()
1128+ {
1129+ $ this ->expectException (\InvalidArgumentException::class);
1130+
1131+ $ this ->createTestCrawler ()->filterXPath ('//ol ' )->ancestors ();
1132+ }
1133+
11051134 /**
11061135 * @dataProvider getBaseTagData
11071136 */
0 commit comments