Skip to content
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

[DomCrawler] compare CrawlerSelector constraints against all matches #36062

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,7 +47,14 @@ protected function matches($crawler): bool
return false;
}

return $this->expectedText === trim($crawler->attr($this->attribute));
foreach ($crawler->getIterator() as $node) {
$attrValue = $node->hasAttribute($this->attribute) ? trim($node->getAttribute($this->attribute)) : null;
if ($this->expectedText === $attrValue) {
return true;
}
}

return false;
}

/**
Expand Down
Expand Up @@ -45,7 +45,13 @@ protected function matches($crawler): bool
return false;
}

return false !== mb_strpos($crawler->text(null, false), $this->expectedText);
foreach ($crawler->getIterator() as $node) {
if (false !== mb_strpos($node->nodeValue, $this->expectedText)) {
return true;
}
}

return false;
}

/**
Expand Down
Expand Up @@ -45,7 +45,13 @@ protected function matches($crawler): bool
return false;
}

return $this->expectedText === trim($crawler->text(null, false));
foreach ($crawler->getIterator() as $node) {
if ($this->expectedText === trim($node->nodeValue)) {
return true;
}
}

return false;
}

/**
Expand Down
Expand Up @@ -21,14 +21,15 @@ class CrawlerSelectorAttributeValueSameTest extends TestCase
{
public function testConstraint(): void
{
$constraint = new CrawlerSelectorAttributeValueSame('input[name="username"]', 'value', 'Fabien');
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username" value="Fabien">'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
$constraint = new CrawlerSelectorAttributeValueSame('input[name^="username"]', 'value', 'Fabien');
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username1" value="Fabien"><input type="text" name="username2" value="Kim">'), '', true));
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username1" value="Kim"><input type="text" name="username2" value="Fabien">'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username1" value="Kim"><input type="text" name="username2" value="Jean">'), '', true));

try {
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"input[name=\"username\"]\" with attribute \"value\" of value \"Fabien\".\n", TestFailure::exceptionToString($e));
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"input[name^=\"username\"]\" with attribute \"value\" of value \"Fabien\".\n", TestFailure::exceptionToString($e));

return;
}
Expand Down
Expand Up @@ -21,14 +21,15 @@ class CrawlerSelectorTextContainsTest extends TestCase
{
public function testConstraint(): void
{
$constraint = new CrawlerSelectorTextContains('title', 'Foo');
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foobar'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
$constraint = new CrawlerSelectorTextContains('table td', 'Foo');
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><table><tr><td>Bar</td></tr><tr><td>Foobar</td></tr>'), '', true));
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><table><tr><td>Foobar</td></tr><tr><td>Bar</td></tr>'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><body><table><tr><td>Fuubar</td></tr><tr><td>Bar</td></tr>'), '', true));

try {
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\" with content containing \"Foo\".\n", TestFailure::exceptionToString($e));
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"table td\" with content containing \"Foo\".\n", TestFailure::exceptionToString($e));

return;
}
Expand Down
Expand Up @@ -21,14 +21,15 @@ class CrawlerSelectorTextSameTest extends TestCase
{
public function testConstraint(): void
{
$constraint = new CrawlerSelectorTextSame('title', 'Foo');
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foo'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
$constraint = new CrawlerSelectorTextSame('table td', 'Foo');
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><table><tr><td>Foo</td></tr><tr><td>Foobar</td></tr>'), '', true));
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><table><tr><td>Foobar</td></tr><tr><td>Foo</td></tr>'), '', true));
$this->assertFalse($constraint->evaluate(new Crawler('<html><body><table><tr><td>Foobar</td></tr><tr><td>Bar</td></tr>'), '', true));

try {
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\" with content \"Foo\".\n", TestFailure::exceptionToString($e));
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"table td\" with content \"Foo\".\n", TestFailure::exceptionToString($e));

return;
}
Expand Down