Skip to content

Commit 47fff15

Browse files
Merge pull request #5 from stevegrunwell/feature/assert-selector-count
Introduce assertSelectorCount() assertion
2 parents 970fb24 + 9b60b38 commit 47fff15

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ These are the assertions made available to PHPUnit via the `MarkupAssertionsTrai
8989

9090
* [`assertContainsSelector()`](#assertcontainsselector)
9191
* [`assertNotContainsSelector()`](#assertnotcontainsselector)
92+
* [`assertSelectorCount()`](#assertselectorcount)
9293
* [`assertHasElementWithAttributes()`](#asserthaselementwithattributes)
9394
* [`assertNotHasElementWithAttributes()`](#assertnothaselementwithattributes)
9495

@@ -131,6 +132,34 @@ This method is the inverse of [`assertContainsSelector()`](#assertcontainsselect
131132
<dd>A message to display if the assertion fails.</dd>
132133
</dl>
133134

135+
### assertSelectorCount()
136+
137+
Assert the number of times an element matching the given selector is found.
138+
139+
<dl>
140+
<dt>(int) $count</dt>
141+
<dd>The number of matching elements expected.</dd>
142+
<dt>(string) $selector</dt>
143+
<dd>A query selector for the element to find.</dd>
144+
<dt>(string) $output</dt>
145+
<dd>The markup to run the assertion against.</dd>
146+
<dt>(string) $message</dt>
147+
<dd>A message to display if the assertion fails.</dd>
148+
</dl>
149+
150+
#### Example
151+
152+
```php
153+
public function testPostList()
154+
{
155+
factory(Post::class, 10)->create();
156+
157+
$response = $this->get('/posts');
158+
159+
$this->assertSelectorCount(10, 'li.post-item', $response->getBody());
160+
}
161+
```
162+
134163
### assertHasElementWithAttributes()
135164

136165
Assert that an element with the given attributes exists in the given markup.

src/MarkupAssertionsTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public function assertNotContainsSelector($selector, $output = '', $message = ''
4242
$this->assertEquals(0, count($results), $message);
4343
}
4444

45+
/**
46+
* Assert the number of times an element matching the given selector is found.
47+
*
48+
* @param int $count The number of matching elements expected.
49+
* @param string $selector A query selector for the element to find.
50+
* @param string $output The markup to run the assertion against.
51+
* @param string $message A message to display if the assertion fails.
52+
*/
53+
public function assertSelectorCount($count, $selector, $output = '', $message = '')
54+
{
55+
$results = $this->executeDomQuery($output, $selector);
56+
57+
$this->assertCount($count, $results, $message);
58+
}
59+
4560
/**
4661
* Assert that an element with the given attributes exists in the given markup.
4762
*

tests/MarkupAssertionsTraitTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function testAssertNotContainsSelector($selector)
5151
);
5252
}
5353

54+
public function testAssertSelectorCount()
55+
{
56+
$this->testcase->assertSelectorCount(
57+
3,
58+
'li',
59+
'<ul><li>1</li><li>2</li><li>3</li></ul>'
60+
);
61+
}
62+
5463
public function testAssertHasElementWithAttributes()
5564
{
5665
$this->testcase->assertHasElementWithAttributes(

0 commit comments

Comments
 (0)