Skip to content

Commit 6ae338a

Browse 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 ------- ba451ab7b2 improve failure messages of the CrawlerSelectorTextContains constraint
2 parents 37dea6c + b3d4cbb commit 6ae338a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

Test/Constraint/CrawlerSelectorTextContains.php

Lines 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
}

Tests/Test/Constraint/CrawlerSelectorTextContainsTest.php

Lines 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)