Skip to content

Commit 5786d2c

Browse files
curlycarla2004fabpot
authored andcommitted
[DomCrawler][FrameworkBundle] Add assertSelectorCount
1 parent c079db4 commit 5786d2c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `CrawlerSelectorCount` test constraint
8+
49
6.0
510
---
611

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DomCrawler\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\DomCrawler\Crawler;
16+
17+
final class CrawlerSelectorCount extends Constraint
18+
{
19+
public function __construct(
20+
private readonly int $count,
21+
private readonly string $selector,
22+
) {
23+
}
24+
25+
public function toString(): string
26+
{
27+
return sprintf('selector "%s" count is "%d"', $this->selector, $this->count);
28+
}
29+
30+
/**
31+
* @param Crawler $crawler
32+
*/
33+
protected function matches($crawler): bool
34+
{
35+
return $this->count === \count($crawler->filter($this->selector));
36+
}
37+
38+
/**
39+
* @param Crawler $crawler
40+
*/
41+
protected function failureDescription($crawler): string
42+
{
43+
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)));
44+
}
45+
}

0 commit comments

Comments
 (0)