12
12
namespace Symfony \Component \DomCrawler \Tests ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
15
16
use Symfony \Component \DomCrawler \Crawler ;
16
17
17
18
abstract class AbstractCrawlerTest extends TestCase
18
19
{
20
+ use ExpectDeprecationTrait;
21
+
19
22
abstract public function getDoctype (): string ;
20
23
21
24
protected function createCrawler ($ node = null , string $ uri = null , string $ baseHref = null )
@@ -409,7 +412,7 @@ public function testFilterXPath()
409
412
$ this ->assertCount (6 , $ crawler ->filterXPath ('//li ' ), '->filterXPath() filters the node list with the XPath expression ' );
410
413
411
414
$ 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 ' );
413
416
}
414
417
415
418
public function testFilterRemovesDuplicates ()
@@ -1082,8 +1085,13 @@ public function testFilteredChildren()
1082
1085
$ this ->assertEquals (1 , $ foo ->children ('.ipsum ' )->count ());
1083
1086
}
1084
1087
1088
+ /**
1089
+ * @group legacy
1090
+ */
1085
1091
public function testParents ()
1086
1092
{
1093
+ $ this ->expectDeprecation ('Since symfony/dom-crawler 5.3: The Symfony\Component\DomCrawler\Crawler::parents() method is deprecated, use ancestors() instead. ' );
1094
+
1087
1095
$ crawler = $ this ->createTestCrawler ()->filterXPath ('//li[1] ' );
1088
1096
$ this ->assertNotSame ($ crawler , $ crawler ->parents (), '->parents() returns a new instance of a crawler ' );
1089
1097
$ this ->assertInstanceOf ('Symfony \\Component \\DomCrawler \\Crawler ' , $ crawler , '->parents() returns a new instance of a crawler ' );
@@ -1102,6 +1110,27 @@ public function testParents()
1102
1110
}
1103
1111
}
1104
1112
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
+
1105
1134
/**
1106
1135
* @dataProvider getBaseTagData
1107
1136
*/
0 commit comments