Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DomCrawler] deprecate parents() in favor of ancestors() #39684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-5.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Asset

* Deprecated `RemoteJsonManifestVersionStrategy`, use `JsonManifestVersionStrategy` instead.

DomCrawler
----------

* Deprecated the `parents()` method, use `ancestors()` instead.

Form
----

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ DependencyInjection
* The `ref()` function from the PHP-DSL has been removed, use `service()` instead.
* Removed `Definition::setPrivate()` and `Alias::setPrivate()`, use `setPublic()` instead

DomCrawler
----------

* Removed the `parents()` method, use `ancestors()` instead.

Dotenv
------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.3.0
-----

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

Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,27 @@ public function previousAll()
}

/**
* Returns the parents nodes of the current selection.
* Returns the parent nodes of the current selection.
*
* @return static
*
* @throws \InvalidArgumentException When current node is empty
*/
public function parents()
{
@trigger_deprecation('symfony/dom-crawler', '5.3', sprintf('The %s() method is deprecated, use ancestors() instead.', __METHOD__));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the @ isnt needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in e970027
good catch, thank you!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @chalasr!


return $this->ancestors();
}

/**
* Returns the ancestors of the current selection.
*
* @return static
*
* @throws \InvalidArgumentException When the current node is empty
*/
public function ancestors()
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
Expand Down
31 changes: 30 additions & 1 deletion src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
namespace Symfony\Component\DomCrawler\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DomCrawler\Crawler;

abstract class AbstractCrawlerTest extends TestCase
{
use ExpectDeprecationTrait;

abstract public function getDoctype(): string;

protected function createCrawler($node = null, string $uri = null, string $baseHref = null)
Expand Down Expand Up @@ -409,7 +412,7 @@ public function testFilterXPath()
$this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');

$crawler = $this->createTestCrawler();
$this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
$this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->ancestors(), '->filterXpath() preserves ancestors when chained');
}

public function testFilterRemovesDuplicates()
Expand Down Expand Up @@ -1082,8 +1085,13 @@ public function testFilteredChildren()
$this->assertEquals(1, $foo->children('.ipsum')->count());
}

/**
* @group legacy
*/
public function testParents()
{
$this->expectDeprecation('Since symfony/dom-crawler 5.3: The Symfony\Component\DomCrawler\Crawler::parents() method is deprecated, use ancestors() instead.');

$crawler = $this->createTestCrawler()->filterXPath('//li[1]');
$this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
Expand All @@ -1102,6 +1110,27 @@ public function testParents()
}
}

public function testAncestors()
{
$crawler = $this->createTestCrawler()->filterXPath('//li[1]');

$nodes = $crawler->ancestors();

$this->assertNotSame($crawler, $nodes, '->ancestors() returns a new instance of a crawler');
$this->assertInstanceOf(Crawler::class, $nodes, '->ancestors() returns a new instance of a crawler');

$this->assertEquals(3, $crawler->ancestors()->count());

$this->assertEquals(0, $this->createTestCrawler()->filterXPath('//html')->ancestors()->count());
}

public function testAncestorsThrowsIfNodeListIsEmpty()
{
$this->expectException(\InvalidArgumentException::class);

$this->createTestCrawler()->filterXPath('//ol')->ancestors();
}

/**
* @dataProvider getBaseTagData
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.15"
Expand Down