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

Skip to content

Commit 6cdd8d6

Browse files
committed
improve failure messages of the CrawlerSelectorTextContains constraint
1 parent 00e3356 commit 6cdd8d6

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

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

Lines 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"symfony/browser-kit": "^4.3|^5.0",
3939
"symfony/console": "^4.3.4|^5.0",
4040
"symfony/css-selector": "^3.4|^4.0|^5.0",
41-
"symfony/dom-crawler": "^4.3|^5.0",
41+
"symfony/dom-crawler": "^4.4.20|~5.1.12|^5.2.4",
4242
"symfony/dotenv": "^4.3.6|^5.0",
4343
"symfony/polyfill-intl-icu": "~1.0",
4444
"symfony/form": "^4.3.5|^5.0",

src/Symfony/Component/DomCrawler/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
}

src/Symfony/Component/DomCrawler/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)