From 1d56613ecbe42eaeb900f79b2c5fc136159b4cc1 Mon Sep 17 00:00:00 2001 From: curlycarla2004 Date: Mon, 21 Nov 2022 11:40:30 +0100 Subject: [PATCH] [DomCrawler][FrameworkBundle] Add `assertSelectorCount` --- .../Bundle/FrameworkBundle/CHANGELOG.md | 5 +++ .../Test/DomCrawlerAssertionsTrait.php | 5 +++ .../Tests/Test/WebTestCaseTest.php | 10 +++++ .../Bundle/FrameworkBundle/composer.json | 2 +- src/Symfony/Component/DomCrawler/CHANGELOG.md | 5 +++ .../Test/Constraint/CrawlerSelectorCount.php | 45 +++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorCount.php diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 1d43a095ed1c5..f6cfae42e6ac1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `DomCrawlerAssertionsTrait::assertSelectorCount(int $count, string $selector)` + 6.2 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php index 2a692d6f5a367..e61d1cd77c09f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php @@ -35,6 +35,11 @@ public static function assertSelectorNotExists(string $selector, string $message self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message); } + public static function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void + { + self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorCount($expectedCount, $selector), $message); + } + public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void { self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php index 8c43917df6f5e..ba3b404364342 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php @@ -190,6 +190,16 @@ public function testAssertSelectorNotExists() $this->getCrawlerTester(new Crawler('

'))->assertSelectorNotExists('body > h1'); } + public function testAssertSelectorCount() + { + $this->getCrawlerTester(new Crawler('

Hello

'))->assertSelectorCount(1, 'p'); + $this->getCrawlerTester(new Crawler('

Hello

Foo

'))->assertSelectorCount(2, 'p'); + $this->getCrawlerTester(new Crawler('

This is not a paragraph.

'))->assertSelectorCount(0, 'p'); + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that the Crawler selector "p" was expected to be found 0 time(s) but was found 1 time(s).'); + $this->getCrawlerTester(new Crawler('

Hello

'))->assertSelectorCount(0, 'p'); + } + public function testAssertSelectorTextNotContains() { $this->getCrawlerTester(new Crawler('

Foo'))->assertSelectorTextNotContains('body > h1', 'Bar'); diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 4ab6b2e919e32..2c62c6e3baee5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -39,7 +39,7 @@ "symfony/browser-kit": "^5.4|^6.0", "symfony/console": "^5.4.9|^6.0.9", "symfony/css-selector": "^5.4|^6.0", - "symfony/dom-crawler": "^5.4|^6.0", + "symfony/dom-crawler": "^6.3", "symfony/dotenv": "^5.4|^6.0", "symfony/polyfill-intl-icu": "~1.0", "symfony/form": "^5.4|^6.0", diff --git a/src/Symfony/Component/DomCrawler/CHANGELOG.md b/src/Symfony/Component/DomCrawler/CHANGELOG.md index 1254296490e62..a05fb7f2ebec8 100644 --- a/src/Symfony/Component/DomCrawler/CHANGELOG.md +++ b/src/Symfony/Component/DomCrawler/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Add `CrawlerSelectorCount` test constraint + 6.0 --- diff --git a/src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorCount.php b/src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorCount.php new file mode 100644 index 0000000000000..22ee1db078a2e --- /dev/null +++ b/src/Symfony/Component/DomCrawler/Test/Constraint/CrawlerSelectorCount.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DomCrawler\Test\Constraint; + +use PHPUnit\Framework\Constraint\Constraint; +use Symfony\Component\DomCrawler\Crawler; + +final class CrawlerSelectorCount extends Constraint +{ + public function __construct( + private readonly int $count, + private readonly string $selector, + ) { + } + + public function toString(): string + { + return sprintf('selector "%s" count is "%d"', $this->selector, $this->count); + } + + /** + * @param Crawler $crawler + */ + protected function matches($crawler): bool + { + return $this->count === \count($crawler->filter($this->selector)); + } + + /** + * @param Crawler $crawler + */ + protected function failureDescription($crawler): string + { + return sprintf('the Crawler selector "%s" was expected to be found %d time(s) but was found %d time(s)', $this->selector, $this->count, \count($crawler->filter($this->selector))); + } +}