8000 [DomCrawler] improve failure messages of the CrawlerSelectorTextContains constraint by xabbuh · Pull Request #39856 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DomCrawler] improve failure messages of the CrawlerSelectorTextContains constraint #39856

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
Aug 26, 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
improve failure messages of the CrawlerSelectorTextContains constraint
  • Loading branch information
xabbuh committed Feb 8, 2021
commit ba451ab7b2361dc65b18a85b7e50673e46603360
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function testAssertSelectorTextNotContains()
{
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Bar');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "body > h1" and does not have a node matching selector "body > h1" with content containing "Foo".');
$this->expectExceptionMessage('matches selector "body > h1" and the text "Foo" of the node matching selector "body > h1" does not contain "Foo".');
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
}

Expand All @@ -199,7 +199,7 @@ public function testAssertPageTitleContains()
{
$this->getCrawlerTester(new Crawler('<html><head><title>Foobar'))->assertPageTitleContains('Foo');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "title" and has a node matching selector "title" with content containing "Bar".');
$this->expectExceptionMessage('matches selector "title" and the text "Foo" of the node matching selector "title" contains "Bar".');
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertPageTitleContains('Bar');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"symfony/browser-kit": "^4.3|^5.0",
"symfony/console": "^4.3.4|^5.0",
"symfony/css-selector": "^3.4|^4.0|^5.0",
"symfony/dom-crawler": "^4.3|^5.0",
"symfony/dom-crawler": "^4.4.20|^5.2.4",
"symfony/dotenv": "^4.3.6|^5.0",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/form": "^4.3.5|^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class CrawlerSelectorTextContains extends Constraint
{
private $selector;
private $expectedText;
private $hasNode = false;
private $nodeText;

public function __construct(string $selector, string $expectedText)
{
Expand All @@ -30,7 +32,11 @@ public function __construct(string $selector, string $expectedText)
*/
public function toString(): string
{
return sprintf('has a node matching selector "%s" with content containing "%s"', $this->selector, $this->expectedText);
if ($this->hasNode) {
return sprintf('the text "%s" of the node matching selector "%s" contains "%s"', $this->nodeText, $this->selector, $this->expectedText);
}

return sprintf('the Crawler has a node matching selector "%s"', $this->selector);
}

/**
Expand All @@ -42,10 +48,15 @@ protected function matches($crawler): bool
{
$crawler = $crawler->filter($this->selector);
if (!\count($crawler)) {
$this->hasNode = false;

return false;
}

return false !== mb_strpos($crawler->text(null, true), $this->expectedText);
$this->hasNode = true;
$this->nodeText = $crawler->text(null, true);

return false !== mb_strpos($this->nodeText, $this->expectedText);
}

/**
Expand All @@ -55,6 +66,6 @@ protected function matches($crawler): bool
*/
protected function failureDescription($crawler): string
{
return 'the Crawler '.$this->toString();
return $this->toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ public function testConstraint()
$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));
$this->assertFalse($constraint->evaluate(new Crawler('<html><head></head><body>Bar'), '', 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));

return;
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the text \"Bar\" of the node matching selector \"title\" contains \"Foo\".\n", TestFailure::exceptionToString($e));
}

$this->fail();
try {
$constraint->evaluate(new Crawler('<html><head></head><body>Bar'));

$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\".\n", TestFailure::exceptionToString($e));
}
}
}
0