Skip to content

Commit 1352708

Browse files
SVillettefabpot
authored andcommitted
[DomCrawler][FrameworkBundle] Add assertAnySelectorText*
1 parent 3302e37 commit 1352708

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ CHANGELOG
77
* Add native return type to `Translator` and to `Application::reset()`
88
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false`
99
* Enable `json_decode_detailed_errors` context for Serializer by default if `kernel.debug` is true and the `seld/jsonlint` package is installed
10+
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextContains(string $selector, string $text)`
11+
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextSame(string $selector, string $text)`
12+
* Add `DomCrawlerAssertionsTrait::assertAnySelectorTextNotContains(string $selector, string $text)`
1013

1114
6.3
1215
---

Test/DomCrawlerAssertionsTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public static function assertSelectorTextContains(string $selector, string $text
4747
), $message);
4848
}
4949

50+
public static function assertAnySelectorTextContains(string $selector, string $text, string $message = ''): void
51+
{
52+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
53+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
54+
new DomCrawlerConstraint\CrawlerAnySelectorTextContains($selector, $text)
55+
), $message);
56+
}
57+
5058
public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
5159
{
5260
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
@@ -55,6 +63,14 @@ public static function assertSelectorTextSame(string $selector, string $text, st
5563
), $message);
5664
}
5765

66+
public static function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void
67+
{
68+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
69+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
70+
new DomCrawlerConstraint\CrawlerAnySelectorTextSame($selector, $text)
71+
), $message);
72+
}
73+
5874
public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
5975
{
6076
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
@@ -63,6 +79,14 @@ public static function assertSelectorTextNotContains(string $selector, string $t
6379
), $message);
6480
}
6581

82+
public static function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void
83+
{
84+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
85+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
86+
new LogicalNot(new DomCrawlerConstraint\CrawlerAnySelectorTextContains($selector, $text))
87+
), $message);
88+
}
89+
6690
public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void
6791
{
6892
self::assertSelectorTextSame('title', $expectedTitle, $message);

Tests/Test/WebTestCaseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ public function testAssertSelectorTextNotContains()
208208
$this->getCrawlerTester(new Crawler('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
209209
}
210210

211+
public function testAssertAnySelectorTextContains()
212+
{
213+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo Baz'))->assertAnySelectorTextContains('ul li', 'Foo');
214+
$this->expectException(AssertionFailedError::class);
215+
$this->expectExceptionMessage('matches selector "ul li" and the text of any node matching selector "ul li" contains "Foo".');
216+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextContains('ul li', 'Foo');
217+
}
218+
219+
public function testAssertAnySelectorTextSame()
220+
{
221+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo'))->assertAnySelectorTextSame('ul li', 'Foo');
222+
$this->expectException(AssertionFailedError::class);
223+
$this->expectExceptionMessage('matches selector "ul li" and has at least a node matching selector "ul li" with content "Foo".');
224+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextSame('ul li', 'Foo');
225+
}
226+
227+
public function testAssertAnySelectorTextNotContains()
228+
{
229+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Baz'))->assertAnySelectorTextNotContains('ul li', 'Foo');
230+
$this->expectException(AssertionFailedError::class);
231+
$this->expectExceptionMessage('matches selector "ul li" and the text of any node matching selector "ul li" does not contain "Foo".');
232+
$this->getCrawlerTester(new Crawler('<ul><li>Bar</li><li>Foo'))->assertAnySelectorTextNotContains('ul li', 'Foo');
233+
}
234+
211235
public function testAssertPageTitleSame()
212236
{
213237
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertPageTitleSame('Foo');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"symfony/console": "^5.4.9|^6.0.9|^7.0",
4343
"symfony/clock": "^6.2|^7.0",
4444
"symfony/css-selector": "^5.4|^6.0|^7.0",
45-
"symfony/dom-crawler": "^6.3|^7.0",
45+
"symfony/dom-crawler": "^6.4|^7.0",
4646
"symfony/dotenv": "^5.4|^6.0|^7.0",
4747
"symfony/polyfill-intl-icu": "~1.0",
4848
"symfony/form": "^5.4|^6.0|^7.0",

0 commit comments

Comments
 (0)