Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ff5db6

Browse filesBrowse files
committed
bug #39856 [DomCrawler] improve failure messages of the CrawlerSelectorTextContains constraint (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [DomCrawler] improve failure messages of the CrawlerSelectorTextContains constraint | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #33551, #37757 | License | MIT | Doc PR | Commits ------- ba451ab improve failure messages of the CrawlerSelectorTextContains constraint
2 parents 7995559 + ba451ab commit 3ff5db6
Copy full SHA for 3ff5db6

File tree

4 files changed

+28
-10
lines changed
Filter options

4 files changed

+28
-10
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function testAssertSelectorTextNotContains()
183183
{
184184
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Bar');
185185
$this->expectException(AssertionFailedError::class);
186-
$this->expectExceptionMessage('matches selector "body > h1" and does not have a node matching selector "body > h1" with content containing "Foo".');
186+
$this->expectExceptionMessage('matches selector "body > h1" and the text "Foo" of the node matching selector "body > h1" does not contain "Foo".');
187187
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
188188
}
189189

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

‎src/Symfony/Bundle/FrameworkBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/browser-kit": "^4.3|^5.0",
4040
"symfony/console": "^4.4.21|^5.0",
4141
"symfony/css-selector": "^3.4|^4.0|^5.0",
42-
"symfony/dom-crawler": "^4.3|^5.0",
42+
"symfony/dom-crawler": "^4.4.20|^5.2.4",
4343
"symfony/dotenv": "^4.3.6|^5.0",
4444
"symfony/polyfill-intl-icu": "~1.0",
4545
"symfony/form": "^4.3.5|^5.0",

‎src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorTextContains.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorTextContains.php
+14-3Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ final class CrawlerSelectorTextContains extends Constraint
1818
{
1919
private $selector;
2020
private $expectedText;
21+
private $hasNode = false;
22+
private $nodeText;
2123

2224
public function __construct(string $selector, string $expectedText)
2325
{
@@ -30,7 +32,11 @@ public function __construct(string $selector, string $expectedText)
3032
*/
3133
public function toString(): string
3234
{
33-
return sprintf('has a node matching selector "%s" with content containing "%s"', $this->selector, $this->expectedText);
35+
if ($this->hasNode) {
36+
return sprintf('the text "%s" of the node matching selector "%s" contains "%s"', $this->nodeText, $this->selector, $this->expectedText);
37+
}
38+
39+
return sprintf('the Crawler has a node matching selector "%s"', $this->selector);
3440
}
3541

3642
/**
@@ -42,10 +48,15 @@ protected function matches($crawler): bool
4248
{
4349
$crawler = $crawler->filter($this->selector);
4450
if (!\count($crawler)) {
51+
$this->hasNode = false;
52+
4553
return false;
4654
}
4755

48-
return false !== mb_strpos($crawler->text(null, true), $this->expectedText);
56+
$this->hasNode = true;
57+
$this->nodeText = $crawler->text(null, true);
58+
59+
return false !== mb_strpos($this->nodeText, $this->expectedText);
4960
}
5061

5162
/**
@@ -55,6 +66,6 @@ protected function matches($crawler): bool
5566
*/
5667
protected function failureDescription($crawler): string
5768
{
58-
return 'the Crawler '.$this->toString();
69+
return $this->toString();
5970
}
6071
}

‎src/Symfony/Component/DomCrawler/Tests/Test/Constraint/CrawlerSelectorTextContainsTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Tests/Test/Constraint/CrawlerSelectorTextContainsTest.php
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ public function testConstraint()
2424
$constraint = new CrawlerSelectorTextContains('title', 'Foo');
2525
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foobar'), '', true));
2626
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
27+
$this->assertFalse($constraint->evaluate(new Crawler('<html><head></head><body>Bar'), '', true));
2728

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

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

36-
$this->fail();
37+
try {
38+
$constraint->evaluate(new Crawler('<html><head></head><body>Bar'));
39+
40+
$this->fail();
41+
} catch (ExpectationFailedException $e) {
42+
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\".\n", TestFailure::exceptionToString($e));
43+
}
3744
}
3845
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.