Skip to content

Commit 1842463

Browse files
committed
deprecate parents() in favor of ancestors()
1 parent 37f69d6 commit 1842463

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.3.0
55
-----
66

7+
* The `parents()` method is deprecated. Use `ancestors()` instead.
78
* Marked the `containsOption()`, `availableOptionValues()`, and `disableValidation()` methods of the
89
`ChoiceFormField` class as internal
910

Crawler.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,27 @@ public function previousAll()
494494
}
495495

496496
/**
497-
* Returns the parents nodes of the current selection.
497+
* Returns the parent nodes of the current selection.
498498
*
499499
* @return static
500500
*
501501
* @throws \InvalidArgumentException When current node is empty
502502
*/
503503
public function parents()
504+
{
505+
@trigger_deprecation('symfony/dom-crawler', '5.3', sprintf('The %s() method is deprecated, use ancestors() instead.', __METHOD__));
506+
507+
return $this->ancestors();
508+
}
509+
510+
/**
511+
* Returns the ancestors of the current selection.
512+
*
513+
* @return static
514+
*
515+
* @throws \InvalidArgumentException When the current node is empty
516+
*/
517+
public function ancestors()
504518
{
505519
if (!$this->nodes) {
506520
throw new \InvalidArgumentException('The current node list is empty.');

Tests/AbstractCrawlerTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Symfony\Component\DomCrawler\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DomCrawler\Crawler;
1617

1718
abstract 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
*/

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1",
2021
"symfony/polyfill-ctype": "~1.8",
2122
"symfony/polyfill-mbstring": "~1.0",
2223
"symfony/polyfill-php80": "^1.15"

0 commit comments

Comments
 (0)